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

Open
#4,383 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

backlog
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.