[BUG][KOTLIN] Wrong Nullability for required and readOnly properties
Created by: AdRyAniP
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Generated properties using kotlin-spring
generator are not correctly generated when the property is required
but marked as readOnly
.
If a property is only required
the generator generates it with non-nullable type which is correct but if the property is marked as readOnly
the type changes to the nullable version which is wrong.
openapi-generator version
Maven plugin versions tested: 5.4.0, 4.3.1, and 6.1.2 (Latest)
OpenAPI declaration file content or URL
Here is an example. Pay attention to the three different fields:
-
id
-> declared asrequired
but withoutreadOnly
flag -
name
-> declared asrequired
and asreadOnly
-
surname
-> notrequired
neitherreadOnly
{
"swagger": "2.0",
"info": {
"description": "Service",
"version": "0.0.1",
"title": "service"
},
"basePath": "/service",
"tags": [
{
"name": "service"
}
],
"paths": {
},
"securityDefinitions": {
"basicAuth": {
"type": "basic"
}
},
"definitions": {
"ExampleDTO" : {
"type" : "object",
"required" : [ "id", "name" ],
"properties" : {
"id" : {
"type" : "string"
},
"name" : {
"type" : "string",
"readOnly" : true
},
"surname" : {
"type" : "string"
}
}
}
}
}
Generation Details
Maven configuration:
<execution>
<id>testservice</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.build.directory}/swagger/test.json</inputSpec>
<generateApis>false</generateApis>
<modelPackage>com.test.model</modelPackage>
<generatorName>kotlin-spring</generatorName>
<skipValidateSpec>true</skipValidateSpec>
<configOptions>
<enumPropertyNaming>UPPERCASE</enumPropertyNaming>
<useTags>true</useTags>
<interfaceOnly>true</interfaceOnly>
<library>spring-boot</library>
<reactive>true</reactive>
<dateLibrary>java8</dateLibrary>
</configOptions>
</configuration>
</execution>
Steps to reproduce
Copy the input file (Swagger definition) into the directory defined in the inputSpec
and Run mvn clean compile
The generated code is:
/**
*
* @param id
* @param name
* @param surname
*/
data class ExampleDTO(
@field:JsonProperty("id", required = true) val id: kotlin.String,
@field:JsonProperty("name", required = true) val name: kotlin.String? = null,
@field:JsonProperty("surname") val surname: kotlin.String? = null
) {
}
As you can see the field name
is declared as nullable
with a default value when it should be non-nullable
. Expected:
@field:JsonProperty("name", required = true) val name: kotlin.String,
Related issues/PRs
Suggest a fix
Don't change a field's nullability behavior just because the readOnly
flag is activated.