How to make an oleore generator using swagger codegen

Introduction

It's been a while since swagger became a hot topic. I use it very conveniently at work. While writing the API definition with swagger-editor, you can actually skip the request and check it, or ask another engineer to hit the API to check it. Since postman etc. can directly import swagger files, you can create simple management commands by swaggar-> postman-> curl generation.

However, when I try to use it for implementation, there are times when there is no generator for the web framework I am using, or a different http client library is used, so it is a little insufficient to use for my environment. think.

This is an introduction to how to use the code generator for such people.

swagger-codegen

Swagger Official java code generator. It has a Plagger implementation and can generate both clients and servers.

The code template engine is mustache.

mustache There are compatible implementations in various languages and swagger-codegen uses jmustache. Example of outputting "No repos: (" if there is repos in Map and the name of the member of repos is not output

python


{{#repos}}{{name}}{{/repos}}
{{^repos}}No repos :({{/repos}}

Generator template generation

swagger-codegen is a maven project and you need to have maven installed to build. First check out and build.

python


git clone https://github.com/swagger-api/swagger-codegen
cd swagger-codegen
mvn

Then generate a generator template.

python


java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar meta \
  -o output/myLibrary -n myClientCodegen -p com.my.company.codegen

The template maven project is output to output / myLibrary. It is a simple implementation that inherits the DefaultCodegen class.

The directory structure is as follows

python


src
└── main
    ├── java
    │   └── com
    │       └── my
    │           └── company
    │               └── codegen
    │                   └── MyclientcodegenGenerator.java
    └── resources
        ├── META-INF
        │   └── services
        │       └── io.swagger.codegen.CodegenConfig
        └── myClientCodegen (Template file)
            ├── api.mustache
            ├── model.mustache
            └── myFile.mustache

You can change the output of the fixed form by rewriting the template. If you want to make camel case a snake case, you will write it in java.

Build and run it as a trial. For some reason some guys haven't generated a template file so I'll add that too

python


cd output/myLibrary
touch src/main/resources/myClientCodegen/myFile.mustache
mvn install
cd ../../
java -cp output/myLibrary/target/myClientCodegen-swagger-codegen-1.0.0.jar:modules/swagger-codegen-cli/target/swagger-codegen-cli.jar   io.swagger.codegen.SwaggerCodegen generate -l myClientCodegen  -i http://petstore.swagger.io/v2/swagger.json   -o myClient

This will generate a library in the myClient directory.

Let's have a comfortable swgger life!

Recommended Posts

How to make an oleore generator using swagger codegen
How to make an app using Tensorflow with Android Studio
How to make an image posted using css look like an icon
How to make asynchronous pagenations using Kaminari
How to make an crazy Android music player
How to create an oleore certificate (SSL certificate, self-signed certificate)
How to make shaded-jar
How to make an image partially transparent in Processing
How to make an almost static page with rails
How to create an application
Java --How to make JTable
How to handle an instance
[Rails] How to make seed
How to authorize using graphql-ruby
[2020 version] How to send an email using Android Studio Javamail
What is an immutable object? [Explanation of how to make]
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
How to make a Java container
How to make a JDBC driver
How to insert an external library
How to publish an application using AWS (3) EC2 instance environment construction
How to make a splash screen
How to make a Jenkins plugin
I tried to make it an arbitrary URL using routing nesting
How to make a Maven project
How to make a Java array
How to make a groundbreaking diamond using Java for statement wwww
[Android] How to make Dialog Fragment
How to build CloudStack using Docker
How to build an environment for any version of Ruby using rbenv
[Swift] How to set an image in the background without using UIImageView.
How to make an app with a plugin mechanism [C # and Java]
How to crop an image with libGDX
Make an executable jar using Android Studio
How to execute a contract using web3j
How to make a Java calendar Summary
[Rails] How to upload images using Carrierwave
How to blur an image (super easy)
I want to make an ios.android app
[Java] How to calculate age using LocalDate
How to publish an application on Heroku
How to make a Discord bot (Java)
How to define an inner class bean
[Swift5] How to implement animation using "lottie-ios"
How to implement image posting using rails
How to write an RSpec controller test
[Rails] How to handle data using enum
How to insert icons using Font awesome
[Swift] How to generate an ID to uniquely identify a certain thing (using UUID)
How to make a judgment method to search for an arbitrary character in an array
How to create an Excel form using a template file with Spring MVC
How to output Excel and PDF using Excella
How to execute and mock methods using JUnit
[Rails] How to create a graph using lazy_high_charts
How to make rbenv recognize OpenSSL on WSL
How to write an if statement to improve readability-java
How to use an array for HashMap keys
How to make Spring Boot Docker Image smaller
How to delete a controller etc. using a command
How to play audio and music using javascript
[Ethereum] How to execute a contract using web3j-Part 2-