You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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: trueschema:
$ref: "#/definitions/test"responses:
200:
description: ""definitions:
test:
type: "boolean"
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.
task generateApi {
inputs.file(swaggerInput)
outputs.dir(generatedSourcesPath)
doLast {
def config =newCodegenConfigurator()
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
])
newDefaultGenerator().opts(config.toClientOptInput()).generate()
Suggest a fix/enhancement
I suggest changing the getter name, or adding two getters and deprecating the older one.
The text was updated successfully, but these errors were encountered:
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
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
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:
will generate the following model:
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:
Suggest a fix/enhancement
I suggest changing the getter name, or adding two getters and deprecating the older one.
The text was updated successfully, but these errors were encountered: