[JAVA] What to do if you don't like the code generated by swagger-codegen-cli

trouble

I used Swagger-codegen-cli to generate java-client code with Swagger Spec provided by an external service. When I upgraded the version of swagger-codegen-cli and recreated it, the getter of Boolean changed to ʻisXXXinstead ofgetXXX`.

Status

--I used v2.2.3 of swagger-codegen-cli before, but the new Swagger Spec doesn't generate it correctly. (Gets a build error) --Changed to v2.2.3-> v2.3.1 (current stable)

Customize template

Add -t when executing

#Example
# -t:Specify the folder where the template is located.Priority is given to the template file that exists in template.
java -jar swagger-codegen-cli.jar generate -l java -t ./template -o swagger-java-client

The location of the original template file is on github at swagger-codegen / modules / swagger-codegen / src / main / resources / Java /

The template file I want to overwrite this time is pojo.mustache. Looking at the getter part


  public {{{datatypeWithEnum}}} {{#isBoolean}}is{{/isBoolean}}{{getter}}() {
    return {{name}};
  }
  {{^isReadOnly}}

There is a place where it is, so fix it as follows

{{#isBoolean}}
  public {{{datatypeWithEnum}}} get{{getter}}() {
    return {{name}};
  }
{{/isBoolean}}

{{!Get is set without writing get except for Boolean}}
{{^isBoolean}}
  public {{{datatypeWithEnum}}} {{getter}}() {
    return {{name}};
  }
{{/isBoolean}}
  {{^isReadOnly}}

Summary

This time, I'm having trouble with the generated code, so I overwrote the template. You can also customize it to your liking.

Supplement

In v2.3.1, there is no choice but to overwrite with the template file, but in the next version, config may be able to determine the Boolean prefix. (As of April 13, 2018)

Recommended Posts

What to do if you don't like the code generated by swagger-codegen-cli
What to do if you don't see the test code error message in the terminal console
What to do if you can't activate the select box created by bootstrap-select
What to do if you can't use the rails command
What to do if you install Ubuntu
What to do if you forget the root password in CentOS7
What to do if you cannot roll back the migration (UnknownMigrationVersionError)
What to do if the server tomcat dies
What to do if you can't install the plugin from the Eclipse marketplace
What to do if you get the error message unrecognized selector send to instance "***"
What to do if you get To install the missing version, run `gem install bundler: 2.1.4`
What to do if the debug gem installation fails
What to do if the Rails server can't start
What to do if you accidentally create a model
What to do if you can't get the text of an element in Selenium
What to do if you get the error Couldn't find Item without an ID
What to do if you cannot execute with the command "Java package name / class name"
What to do if you get a java.io.IOException in GlassFish
# What to do if you accidentally do rails db: migrate: drop
What to do if the adb command cannot be executed
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
What to do if you change the Listen Address from the settings screen with Mattermost docker
What to do if you installed Ruby with rbenv but the version does not change
[Rails] What to do if you can't get an error message with the errors method
What to do if you get a "302" error in your controller unit test code in Rails
What to do if you get an error in Basic authentication during Rails test code
[Ubuntu 20.04] What to do if the external monitor is not recognized
What to do if you get a gcc error in Docker
What to do if validation doesn't work with the update action
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if Cloud9 is full in the Rails tutorial
What to do if you get a DISPLAY error in gym.render ()
What to do if you get angry with OpenSSL with pyenv install
[Rails] What to do if you can't get parameters with form_with
What to do if you get a groovy warning in Thymeleaf Layout
What to do if you get an error during rails db: reset
What to do if the background image is not applied after deployment
What to do if the prefix c is not bound in JSP
[Composer] [Laravel] What to do if you cannot install due to insufficient memory
What to do if you get an uninitialized constant Likes Controller error
What to do if you get an error when you hit Heroku logs
What to do if you get a MiniMagick vulnerability alert on GitHub
What to do if you get an error with bundle install by entering gem'bcrypt' in your Gemfile
What to do if the image posted by refile disappears after setting a 404 error page in Rails
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do if you get the error Too long with no output (exceeded 10m0s) on CircleCI
[Rails / Docker] What to do if access is denied by the browser (localhost: 3000) after adding a gem
[Gradle] [checkstyle] What to do if the active setting of Checkstyle is removed by "Refresh Gradle project" from Eclipse
What to do if the build fails with the error "Module compiled with Swift x.x.x cannot be imported by the Swift x.x.x compiler"
What to do if you get a SQLite3 :: BusyException: database is locked error
What to do if you get Could not locate Gemfile or .bundle / directory
What to do if the changes are not reflected in the jar manifest file
What to do if you get an "A server is already running." Error when you try to start the rails server
What to do if you have enabled Use the WSL2 based engine in Docker Desktop with insufficient WSL2 installation
What to do if mvn archetype: generate fails
What to do if you get an error on heroku rake db: migrate
What to do if SPA URL loading cannot be detected by WebView shouldOverrideUrlLoading
What do you use when converting to String?
What to do if the rails server doesn't run out on AWS cloud9
How to translate the error message into Japanese (What to do if you cannot log in for some reason)