swagger-api / swagger-api/swagger-core
Setting example value 'null' to fields in json request body with @Schema, not "null" string without model converter
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 7.5k
- Forks
- 2.3k
- Avg merge
- 18h 1m
- Merged PRs (30d)
- 10
Description
Hello,
While I was making an API document, I struggled to set value of fields to 'null' in json request body.
I used @Schema annotation because I was in spring boot and kotlin.
At first, I thought that setting "null" in an attribute like defaultValue or example would be helpful.
But it showed "null" string.
So I searched it on the Internet because I thought many people would have faced same issue with me.
With help of Generative AI, I finally found that ModelConverter could solve my issue and solved it.
Below is the way I solved.
@Configuration
class SwaggerConfiguration {
@Bean
fun customModelConverter(): CustomModelConverter {
return CustomModelConverter()
}
}
class CustomModelConverter : ModelConverter {
override fun resolve(type: AnnotatedType, context: ModelConverterContext, chain: Iterator<ModelConverter>, ): Schema<*>? {
var schema: Schema<*>? = null
while (chain.hasNext()) {
schema = chain.next().resolve(type, context, chain)
if (schema.example == "null") {
schema.example = null
}
}
return schema
}
}
But It seems that it can be solved in easier way.
For example, in JUnit, @CSVSource can pass argument as null with nullValues attribute.
Below is the sample code, which converts "null" string to null.
@CsvSource(value= {"null"}, nullValues={"null"})
Or it can be set to null if nullable is true and example is not defined.
I'm curious that this issue is being considered to be solved
or many people are satisfied with using model converter
or there is another way to handle it in easier.
Thank you.
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 tracing how @Schema example and defaultValue values flow through ModelConverter resolution and into the generated request-body schema. Compare the current "null" string behavior with the requested JSON null representation, and add coverage showing the expected result without requiring a custom converter.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin, spring-boot
- Domain
- api, backend-api-design
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100