swagger-api / swagger-api/swagger-core
Enhancement: change `ModelResolver` to use `"nullable": true` for "non"-required properties based on default property inclusion of object mapper
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 7.5k
- Forks
- 2.3k
- Avg merge
- 18h 1m
- Merged PRs (30d)
- 10
Description
As part of my SpringBoot application that uses the springdoc-openapi library (using swagger-core under the hood) to generate OpenAPI specs, I would like automatically generate .nullable(true) for "non"-required properties.
This because by (Jackson) ObjectMapper is configured with:
.setSerializationInclusion(JsonInclude.Include.ALWAYS)
This causes Jackson includes null property values when it serializes JSON as opposed to omitted them.
But this means that every "non"-required property in de OpenAPI spec should also have "nullable": true.
However, the ModelResolver currently only adds a "nullable": true (based on what I digested from reading its source-code) in case nullable = true was specified to an @Schema(..) annotation.
But since I already use @NotNull annotations, I do not also want to also use @Schema(..) annotations.
What if we would change the ModelResolver to automatically adds nullable = true for "non"-required properties based on the "default property inclusion" in object mapper?
So that in case of JsonInclude.Include.ALWAYS the ModelResolver automatically adds nullable = true for "non"-required properties.
The "default property inclusion" could easily be determined using the (overloaded) getDefaultPropertyInclusion method of the SerializationConfig config (that can be retrieved calling .getSerializationConfig() on the object mapper.
References:
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 reading ModelResolver and the SerializationConfig methods available through the ObjectMapper, especially getSerializationConfig() and getDefaultPropertyInclusion(). Determine how non-required properties are resolved, then verify that schemas generated with JsonInclude.Include.ALWAYS mark those properties nullable without requiring @Schema annotations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100