Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting example value 'null' to fields in json request body with @Schema, not "null" string without model converter #4767

Open
infomuscle opened this issue Oct 25, 2024 · 0 comments

Comments

@infomuscle
Copy link

infomuscle commented Oct 25, 2024

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.

@infomuscle infomuscle changed the title Setting example value 'null' to request body with @Schema, not "null" string without model converter Setting example value 'null' to fields in json request body with @Schema, not "null" string without model converter Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant