Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA Spring] Generating code with primitive boolean wrappers do not validate for null #7617

Open
GeeWee opened this issue Feb 8, 2018 · 3 comments

Comments

@GeeWee
Copy link

GeeWee commented Feb 8, 2018

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.

@GeeWee GeeWee changed the title [JAVA Spring] Generating code with primitive wrappers do not validate for null [JAVA Spring] Generating code with primitive boolean wrappers do not validate for null Feb 8, 2018
@wing328
Copy link
Contributor

wing328 commented Feb 12, 2018

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

#7344
#7261

@svkcemk
Copy link

svkcemk commented Feb 23, 2018

Hi is this issue fixed or still exists?

@FatCash
Copy link

FatCash commented Apr 25, 2018

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 #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:
#7980 , #8001 , #8044

EDIT:
All fixed thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants