swagger-api / swagger-api/swagger-codegen
[JAVA Spring] Generating code with primitive boolean wrappers do not validate for null
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the Spring generator entry point, DefaultGenerator configuration, and the generated model shown in the report; reproduce the issue with the supplied Swagger definition and useBeanValidation enabled. Trace the model getter template or generator logic responsible for Boolean properties. Done means generated wrapper booleans use validation-compatible accessors and the behavior is covered by an appropriate generator test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100