Skip to content

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

Open
@GeeWee

Description

@GeeWee
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions