Swagger-codegen: [JAVA Spring] Generating code with primitive boolean wrappers do not validate for null

Created on 8 Feb 2018  路  3Comments  路  Source: swagger-api/swagger-codegen

Description & Steps to reproduce

When generating Java models with swagger-codegen, booleans are converted into wrapper classes. E.g. a boolean called foo will generate a Boolean wrapper object.

E.g. the swagger file:

swagger: "2.0"
info:
  title: "Test"
  version: "2"
host: "petstore.swagger.io"
basePath: "/v2"
schemes:
- "http"
paths:
  /test:
    post:
      tags:
      - "pet"
      summary: "Test"
      parameters:
      - in: "body"
        name: "body"
        description: "Pet object that needs to be added to the store"
        required: true
        schema:
          $ref: "#/definitions/test"
      responses:
        200:
          description: ""
definitions:
  test:
    type: "boolean"

will generate the following model:

/**
 * TestModel
 */
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2018-02-08T10:30:55.500+01:00")

public class TestModel   {
  @JsonProperty("someBoolean")
  private Boolean someBoolean = null;

  public TestModel someBoolean(Boolean someBoolean) {
    this.someBoolean = someBoolean;
    return this;
  }

  /**
   * Get someBoolean
   * @return someBoolean
  **/
  @ApiModelProperty(value = "")


  public Boolean isSomeBoolean() {
    return someBoolean;
  }

  public void setSomeBoolean(Boolean someBoolean) {
    this.someBoolean = someBoolean;
  }

 //equals, hashocde and toString here
}

As mentioned in this reply, if the Boolean is not a primitive, Hibernator Validator only inspects it if the getter is named "getSomeBoolean" not "isSomeBoolean" that is only the convention with primitives. Thus Hibernate ignores it.

In practice this means that any object with booleans, the boolean is ignored for validation, which means that submitting "null" or omitting the object does not fail validation.

Swagger-codegen version

2.3.1

Command line used for generation

I'm not quite sure what the proper CLI command used is, as we use it via [gradle]https://github.com/thebignet/swagger-codegen-gradle-plugin-example) - but our gradle task is as follows:

task generateApi {
    inputs.file(swaggerInput)
    outputs.dir(generatedSourcesPath)
    doLast {
        def config = new CodegenConfigurator()
        config.setInputSpec(swaggerInput.path)
        config.setOutputDir(generatedSourcesPath.path)
        config.setLang('spring')
        //The documentation for the properties below can be found by downloading swagger-codegen-cli.jar
        //and running java -jar swagger-codegen-cli.jar config-help -l spring
        config.setAdditionalProperties([
                'invokerPackage': 'com.stibosystems.ziggy.mainbackend.api',
                'modelPackage': 'com.stibosystems.ziggy.mainbackend.api.model',
                'apiPackage': 'com.stibosystems.ziggy.mainbackend.api',
                'sourceFolder': '.', //This is relative to the output dir
                //We don't care about the timestamp, but need the @Generated annotation so error prone knows not to check generated files
                'hideGenerationTimestamp': false,
                'dateLibrary': 'java8',
                'java8': true,
                'interfaceOnly': true,
                'useBeanValidation': true,
                'withXml': false,
                'useTags': true
        ])
        new DefaultGenerator().opts(config.toClientOptInput()).generate()
Suggest a fix/enhancement

I suggest changing the getter name, or adding two getters and deprecating the older one.

Bug Java Spring

Most helpful comment

Hi is this issue fixed or still exists?

All 3 comments

@GeeWee you change the getter naming convention by customizing the template. Please refer to the following for more information:

https://github.com/swagger-api/swagger-codegen/pull/7344
https://github.com/swagger-api/swagger-codegen/issues/7261

Hi is this issue fixed or still exists?

How to achieve this?

  • Required boolean field generate to primitive boolean, while non-mandatory to wrapper Boolean?

Or how to force the Boolean wrapper getter method to getSomeBool instead of isSomeBool ?
@wing328 you linked to PR https://github.com/swagger-api/swagger-codegen/pull/7344 that allows users to customize is/get/has for Boolean.
Can you give an example how to actually use it please? Seems not easily configurable, instead one have to write own template. Can you link to such template or give some tips?

EDIT:
Thank you @wing328 , Your comment in the PR helped me achieve this, edited {{#isBoolean}}get{{/isBoolean}} in pojo.mustache.

Now I just have to figure out if his issue can be helped by editing templates:
https://github.com/swagger-api/swagger-codegen/issues/7980 , https://github.com/swagger-api/swagger-codegen/issues/8001 , https://github.com/swagger-api/swagger-codegen/issues/8044~~
EDIT:
All fixed thanks

Was this page helpful?
0 / 5 - 0 ratings