You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.3info:
title: Example APIversion: 1.0.0description: Simple API to demonstrate issue with nullable and optional fields in Kotlin code generationpaths:
/examples:
post:
summary: Create an example resourcerequestBody:
required: truecontent:
application/json:
schema:
$ref: '#/components/schemas/ExampleDto'responses:
'200':
description: Successful creationcomponents:
schemas:
ExampleDto:
type: objectproperties:
name:
type: stringdescription: "A name field that is optional but not nullable"nullable: falserequired: []
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
The text was updated successfully, but these errors were encountered:
Description
In the generated Kotlin models, fields marked as
required: false
andnullable: false
are mapped toType? = 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
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
ornullable: true
, use@JsonInclude(JsonInclude.Include.ALWAYS)
to ensure null values are included when neededThe text was updated successfully, but these errors were encountered: