OpenAPITools / OpenAPITools/openapi-generator
[BUG][KOTLIN-SPRING] Fields marked as required: false and nullable: false are serialised as null in request body
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Use the supplied OpenAPI 3.0.3 declaration with the org.openapi.generator 7.12.0 Gradle plugin to reproduce the generated Kotlin model and request-body serialization. Inspect how optional, non-nullable fields are represented and serialized; done means a null field is omitted while the required or nullable cases described in the issue retain their intended behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin, openapi, spring
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100