OpenAPITools / OpenAPITools/openapi-generator

[BUG] [kotlin-spring] readOnly properties nullable

Open
#16,088 1 comment 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

In #14280 a "bug" was reported, that required readOnly values are generated as nullable. This "bug" was "fixed" in #14509.

However, this was not a bug. Like @zaenk pointed out this is necessary to use the resource as request and as response body. (see this comment)

Now the generated code leads to deserialization errors in a request because the readOnly properties are not given.

[http-nio-80-ex] DefaultHandlerExcept WARN  - Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Instantiation of [simple type, class org.openapitools.model.Example] value failed for JSON property id due to missing (therefore NULL) value for creator parameter id which is a non-nullable type] [mvc.support.DefaultHandlerExceptionResolver]
openapi-generator version

since 6.5.0

Generated code for 6.4.0

data class Example(

    @Schema(example = "null", required = true, readOnly = true, description = "")
    @get:JsonProperty("id", required = true) val id: kotlin.String? = null,

    @Schema(example = "null", required = true, description = "")
    @get:JsonProperty("name", required = true) val name: kotlin.String
)

Generated code since 6.5.0

data class Example(

    @Schema(example = "null", required = true, readOnly = true, description = "")
    @get:JsonProperty("id", required = true) val id: kotlin.String,

    @Schema(example = "null", required = true, description = "")
    @get:JsonProperty("name", required = true) val name: kotlin.String
)

--> the id is no longer nullable with default value null.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
    version: 1.0.0
    title: Example
paths:
  /example:
    post:
      requestBody:
        content:
            application/json:
                schema:
                    $ref: '#/components/schemas/example'
      responses:
        200:
          description: OK
          content:
            application/json:
                schema:
                    $ref: '#/components/schemas/example'
components:
    schemas:
        example:
          required:
            - id
            - name
          properties:
            id:
              type: string
              readOnly: true
            name:
              type: string
Generation Details

Gradle Config:

openApiGenerate {
    generatorName.set("kotlin-spring")
    additionalProperties.put("useSpringBoot3", "true")

    inputSpec.set(openApiDefinitionFile)
    outputDir.set(openApiGeneratedSourcesDir)
}
Steps to reproduce
  1. Use the OpenAPI example from above as input file for the generator
  2. Start the Server
  3. Make a Post-Request to /example that contains only the necessary properties:
    {
      "name": "John"
    }
    
  4. The request will fail.
Related issues/PRs
  • #14280
  • #14509
Suggest a fix

revert #14509

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the kotlin-spring generator change in #14509 and the OpenAPI declaration in this issue. Reproduce generation with the provided schema and send a request containing only name. Done means the request succeeds without a missing non-null id error while the generated response still represents the readOnly property correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.