Code Monkey home page Code Monkey logo

swagger-multi-file-yaml's Introduction

Swagger-mutlifile-yaml-merger

Write swagger specification file in multiple files and merge into one file for code generation. (c) Copyright 2018 Devesh Pujari. This code is available under the Apache License, version 2: http://www.apache.org/licenses/LICENSE-2.0.html

Problem

Swagger provides a way to define the APIs in yaml file and using this api the skeleton code can be generated. This is a single file. This file can become very big as more APIs are added in the spec file by team members. This provide a way to distribute the yaml files in different directory and then merge the yaml file into one big yaml file. Then the generated yaml file can be used to generate the code or load into the swagger editor.

Example

Look at the sample swagger spec files in the directory test/resources/swagger/

The swagger specification main file is test/resources/swagger/index.yaml . From this file references are added to different files using JSON references ( $ref). $ref can point to local file or external file. If $ref starts with # then it is local ref. If it starts with ./ then it is external file.

$ref: "#/definitions/ErrorResponse"  ( This points to the local file )
$ref: ./info/index.yaml  ( this points to the external file in the info directory)

if you run this :

mvn clean install

Then the generated yaml spec file will be generated in the path - target/generated-sources/genapi.yaml Use this file to generated the code or load into the swagger online editor.

Adding to your project

Download this code and add the code as a module in your project. In your main project module add the below plugin to generate yaml file. Add the following properties. swagger-src-api-file - Source main index file with full path. swagger-gen-api-file - generated yaml file name with full path

   <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <executions>
         <execution>
            <id>merge-yaml</id>
            <phase>generate-sources</phase>
            <goals>
               <goal>java</goal>
            </goals>
            <configuration>
               <skip />
               <includePluginDependencies>true</includePluginDependencies>
               <includeProjectDependencies>false</includeProjectDependencies>
               <mainClass>com.swagger.yaml.Main</mainClass>
               <commandlineArgs>${swagger-src-api-file} ${swagger-gen-api-file}</commandlineArgs>
            </configuration>
         </execution>
      </executions>
      <dependencies>
         <dependency>
            <groupId>com.swagger.yaml</groupId>
            <artifactId>multifile-yaml-merger</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
         </dependency>
      </dependencies>
   </plugin>

Once the yaml file is generated then that file can be used to generate the code using the swagger codegen.

swagger-multi-file-yaml's People

Contributors

dpujari avatar deveshpujari avatar

Watchers

James Cloos avatar Patrick Johnston avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.