[BUG][JAVA] @Size not generated in the method parameters which are treated as headers
Created by: Chismur
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
I'm trying to generate some javax.validation annotations in my generated Spring API files. In the parameters section I use reference to the object. When I try to add a size validation on a parameter by adding minItems
or maxItems
for example, the generated method does not contain the expected javax.validation @Size
annotation
Actual:
@ApiOperation(value = "someValue", nickname = "someNickName", notes = "someNotes", tags={ "someTag", })
@ApiResponses(value = {
@ApiResponse(code = 202, message = "success") })
@RequestMapping(value = "/someUrl/v1/smth",
consumes = { "application/json" },
method = RequestMethod.POST)
default ResponseEntity<Void> someMethod(@ApiParam(value = "someValueHeaderOne" ,required=true) @RequestHeader(value="HeaderOne", required=true) String headerOne,@ApiParam(value = "someValueHeaderTwo" ,required=true) @RequestHeader(value="HeaderTwo", required=true) UUID headerTwo, @ApiParam(value = "Request Body" ,required=true ) @Valid @RequestBody SomeBody someBody) {
return getDelegate().someMethod(headerOne, headerTwo, someBody);
Expected:
@ApiOperation(value = "someValue", nickname = "someNickName", notes = "someNotes", tags={ "someTag", })
@ApiResponses(value = {
@ApiResponse(code = 202, message = "success") })
@RequestMapping(value = "/someUrl/v1/smth",
consumes = { "application/json" },
method = RequestMethod.POST)
default ResponseEntity<Void> someMethod(@ApiParam(value = "someValueHeaderOne" ,required=true) @RequestHeader(value="HeaderOne", required=true) @Size (min=1, max=10) String headerOne,@ApiParam(value = "someValueHeaderTwo" ,required=true) @RequestHeader(value="HeaderTwo", required=true) @Size (min=1, max=10) String headerTwo, @ApiParam(value = "Request Body" ,required=true ) @Valid @RequestBody SomeBody someBody) {
return getDelegate().someMethod(headerOne, headerTwo, someBody);
When I try this by manually modifying the generated interface, the validation works great and throw errors when I send an empty header
openapi-generator version
4.1.3 (maven plugin). I also tried the 4.3.1 version and the latest version, the result is the same
OpenAPI declaration file content or url
{
"openapi": "3.0.1",
"info": {
"title": "title",
"description": "description",
"version": "1.0.0",
"contact": {
"name": "name",
"url": "url",
"email": "email.com"
}
},
"paths": {
"/someUrl/v1/smth": {
"post": {
"tags": [
"someMethod"
],
"summary": "summary",
"description": "description",
"operationId": "sm",
"parameters": [
{
"$ref": "#/components/parameters/HeaderOne"
},
{
"$ref": "#/components/parameters/HeaderTwo"
}
],
"requestBody": {
"description": "Request Body",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SomeBody"
}
}
},
"required": true
},
"responses": {
"202": {
"description": "success"
}
}
}
}
},
"components": {
"schemas": {
"SomeBody": {
"type": "object",
"required": [
"id"
],
"properties": {
"id": {
"type": "string",
"example": "123",
"description": "description"
}
}
}
},
"parameters": {
"HeaderOne": {
"name": "HeaderOne",
"in": "header",
"description": "description",
"required": true,
"schema": {
"type": "string",
"minLength": 1,
"maxLength": 10
}
},
"HeaderTwo": {
"name": "HeaderTwo",
"in": "header",
"description": "description",
"required": true,
"schema": {
"type": "string",
"minLength": 1,
"maxLength": 10
}
}
}
}
}
Generation Details
<execution>
<id>generate-sources-client</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/conf/swagger/smth.json</inputSpec>
<generatorName>spring</generatorName>
<generateApiTests>false</generateApiTests>
<generateApiDocumentation>false</generateApiDocumentation>
<generateModelTests>false</generateModelTests>
<generateModelDocumentation>false</generateModelDocumentation>
<apiPackage>${my.package}.api.smth</apiPackage>
<modelPackage>${my.package}.api.smth.model</modelPackage>
<library>spring-boot</library>
<configOptions>
<java8>true</java8>
<library>resttemplate</library>
<library>spring-boot</library>
<delegatePattern>true</delegatePattern>
<dateLibrary>java8-offsetdatetime</dateLibrary>
<performBeanValidation>true</performBeanValidation>
<useBeanValidation>true</useBeanValidation>
<useTags>true</useTags>
</configOptions>
<environmentVariables>
<supportingFiles>ApiUtil.java</supportingFiles>
</environmentVariables>
</configuration>
</execution>
Related issues/PRs
This could be related to this issue - https://github.com/OpenAPITools/openapi-generator/issues/4122