Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #14280
Closed
Open
Issue created Dec 18, 2022 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[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 as required but without readOnly flag
  • name -> declared as required and as readOnly
  • surname -> not required neither readOnly
{
  "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.

Assignee
Assign to
Time tracking