Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][KOTLIN-SPRING] Fields marked as required: false and nullable: false are serialised as null in request body #21026

Open
lvdkleij opened this issue Apr 3, 2025 · 0 comments

Comments

@lvdkleij
Copy link

lvdkleij commented Apr 3, 2025

Description

In the generated Kotlin models, fields marked as required: false and nullable: false are mapped to Type? = null, which is fine. However, because of this, the field ends up being included in the JSON request body as "field": null, even though the OpenAPI schema explicitly states that null is not allowed.

This leads to invalid requests being sent to APIs that correctly reject null values for non-nullable fields. Fields that are not required and not nullable should simply be omitted from the request body if they are null.

openapi-generator version

gradle plugin: org.openapi.generator 7.12.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Example API
  version: 1.0.0
  description: Simple API to demonstrate issue with nullable and optional fields in Kotlin code generation

paths:
  /examples:
    post:
      summary: Create an example resource
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ExampleDto'
      responses:
        '200':
          description: Successful creation

components:
  schemas:
    ExampleDto:
      type: object
      properties:
        name:
          type: string
          description: "A name field that is optional but not nullable"
          nullable: false
      required: []
Suggest a fix

Consider adding @JsonInclude(JsonInclude.Include.NON_NULL) at the class level to omit null fields by default.
Then, for fields where required: true or nullable: true, use @JsonInclude(JsonInclude.Include.ALWAYS) to ensure null values are included when needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant