OpenAPITools / OpenAPITools/openapi-generator
[BUG] [kotlin-spring] readOnly properties nullable
Nobody has claimed this yet.
- 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
- Use the OpenAPI example from above as input file for the generator
- Start the Server
- Make a Post-Request to
/examplethat contains only the necessary properties:{ "name": "John" } - The request will fail.
Related issues/PRs
- #14280
- #14509
Suggest a fix
revert #14509
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
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