OpenAPITools / OpenAPITools/openapi-generator
[BUG][Kotlin] Nullable attribute is ignored if field is not required
Open
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
When generating model with fields not required nullable attribute is ignored
openapi-generator version
5.3.0
OpenAPI declaration file content or url
components:
schemas:
Cat:
type: object
xml:
name: cat
properties:
id:
type: integer
format: int64
nullable: false
name:
type: boolean
nullable: true
Generation Details
Steps to reproduce
Generate this class. Expected result:
data class Cat (
@SerializedName("id")
val id: kotlin.Long,
@SerializedName("name")
val name: kotlin.Boolean? = null
)
actual:
data class Cat (
@SerializedName("id")
val id: kotlin.Long? = null,
@SerializedName("name")
val name: kotlin.Boolean? = null
)
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/4530
Suggest a fix
Copy the solution from /data_class_req_var.mustache to data_class_opt_var.mustache
{{#isNullable}}?{{/isNullable}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}
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 with data_class_req_var.mustache and data_class_opt_var.mustache, then generate the Kotlin model from the OpenAPI schema in the issue. The optional nullable field should preserve its nullable marker while the non-required id remains nullable as shown in the expected output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100