OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.59k stars 6.52k forks source link

[BUG] Kotlin generated code makes non-nullable shema fields nullable #14893

Open mobilekosmos opened 1 year ago

mobilekosmos commented 1 year ago

Bug Report Checklist

Description

When I use the open-api-generator all of the model fields are ? nullable, all classes look like this:

data class PagedResultDtoOfStoredDeviceFilterWithoutTermDto (

    @Json(name = "currentPage")
    val currentPage: kotlin.Int? = null,

    @Json(name = "totalPages")
    val totalPages: kotlin.Int? = null,

    @Json(name = "pageSize")
    val pageSize: kotlin.Int? = null,

    @Json(name = "totalCount")
    val totalCount: kotlin.Int? = null,

    @Json(name = "items")
    val items: kotlin.collections.List<StoredDeviceFilterWithoutTermDto>? = null

)

Despite those fields are not all nullable in the swagger.json.

openapi-generator version

6.3.0

OpenAPI declaration file content or url

swagger.json

Generation Details
java -jar openapi-generator-cli.jar generate ^
     -g kotlin ^
     --library jvm-retrofit2 ^
     -i "https://app-hsm-clse-stage.azurewebsites.net/swagger/standardV1/swagger.json" ^
     -p groupId=com.mycompany ^
     -p artifactId=mycompany-myproduct-client-kotlin ^
     -p artifactVersion=1.0.0 ^
     -p basePackage=com.mycompany.myproduct.networking ^
     -p packageName=com.mycompany.myproduct.networking ^
     -p configPackage=com.mycompany.myproduct.networking.config ^
     -p apiPackage=com.mycompany.myproduct.networking.api ^
     -p modelPackage=com.mycompany.myproduct.networking.model ^
     -p sourceFolder=src/main/java ^
     -p dateLibrary=java8 ^
     -p java8=true ^
     -p useRxJava3=true ^

pause
Steps to reproduce

Run above batch file. Check all model classes. For example IDs are never nullable in our swagger.json, the generated code is always nullable, no matter which one.

Related issues/PRs
Suggest a fix

On SO somebody suggested a workaround, but I strongly think this is a bug.

o-nix commented 1 year ago

#

By default, all object properties are optional. You can specify the required properties in the required list

mobilekosmos commented 1 year ago

But the specification also has the nullable attribute for each data type, which is being ignored.

mobilekosmos commented 1 year ago

I understand now that a property which is not required must be handled as nullable on the client side, but maybe if this works as designed we could make a feature request out of this? We need to be able to configure this in the generator with a flag, I'm surprised I'm the first one to come up with this?