When generating the documentation It is not marking non nullable types as required. As I was not able to any information if kotlin is supported, I would like to know if kotlin is not supported or am I missing something?
I could not get the fields to show up as required with @Nonnull or @Schema(required=true) as well.
Kotlin is supported, but to my knowledge, there is no special rule for non-nullable Kotlin types. This means that @RequestParam is the one influencing the fact that a given variable is required or not.
The following works as expected:
suspend fun keys(@PathVariable redisInstance: RedisInstance, @RequestParam(required = false) test: String?)
If no @RequestParam is used in the previous example, the variable is marked as required.
I am actually referring to the object used with @RequestBody, not a paremeter. Sorry I should have been more specific. I wanted to set the field of a class as required.
I expected this to work but It didn't mark the field as required.
data class RegisterUserDTO(
@Schema(required=true)
val email: String,
@Schema(required=true)
val password: String,
@Schema(required=true)
val name: String
)
fun register(@RequestBody user : RegisterUserDTO )
Oh sorry, I thought you were talking about query parameters.
To make that work, you must use @field:Schema instead of @Schema. You must use this field: prefix if you define classes via constructor declaration and want to apply a given annotation to one of the attributes. Take a look at https://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets.
Thank you @joaodias14 for the support.
Hi @springdoc and @joaodias14
A follow up question, we have kind of a similar issue.
We have following classes:
data class RegistrationForm(
@field:Schema(required = true) val businessName: String,
@field:Schema(required = true) val address: Address,
)
data class Address(
@field:Schema(required = true) val street: String,
@field:Schema(required = true) val streetNumber: String,
@field:Schema(required = true) val postCode: String,
@field:Schema(required = true) val city: String
)
Setting the required property works for all fields, except for the address field - see the attached screenshot of the swagger ui
Do you have an idea what could cause this and how to fix it? Thanks in advance
Hi @WtfJoke, can you please provide the corresponding api-docs?
But my guess is that this is a Swagger UI issue.
"RegistrationForm": {
"required": [
"businessName"
],
"type": "object",
"properties": {
"businessName": {
"type": "string"
},
"address": {
"$ref": "#/components/schemas/Address"
}
}
Shorted api-docs looks like this. As you can see only businessName is marked as required and address not.
Its not Swagger-UI issue since we generate Typescript/Kotlin-Code out of swagger and its marked as Nullable/Not required.
Hi @WtfJoke, this is a Swagger issue --> https://github.com/swagger-api/swagger-core/issues/3259.
It seems there is a hacky solution if you really need it --> https://stackoverflow.com/questions/51671883/swagger-ignores-schema-properties-for-referenced-schemas.
Thanks @joaodias14, I actually wanted to try the requiredProperties after christmas to check if the same problem occurs. Couldnt imagine that its a swagger issue.
Thank you guys. After these discussions, we can close this issue.
And if required by @WtfJoke feedback, it can be reopened
Hi @WtfJoke, this is a Swagger issue --> swagger-api/swagger-core#3259.
It seems there is a hacky solution if you really need it --> https://stackoverflow.com/questions/51671883/swagger-ignores-schema-properties-for-referenced-schemas.
Another solution - using javax.validation.NotNull annotation
Just wanted to let you know, that replacing @field:Schema(required = true) on field level replacing with @Schema(requiredProperties = ["businessName", "address"]) on class-level works as intended:

Thanks for your help guys!
@joaodias14 Using @field:Schema(required = true) on every non-nullable field in model classes makes the model look very verbose. Can I automate this? Can I configure this in the OpenAPI bean? What I'd like to do is for every class having a non-nullable field, the generated yaml would have the required field. Looks like it might be possible if I use OpenAPI.components, but the Schema object does not hold the class information.
Hi @VedavyasBhatJupiter, I don't really know the inner workings of the model classes processing. But I would say that most probably there is the need to add this behaviour to springdoc so that a not nullable kotlin attribute is considered required without the need of any annotation.
Feel free to contribute with your proposal of implementation.
Hi again @VedavyasBhatJupiter, it seems that there is already a PR that might just do what you need --> https://github.com/springdoc/springdoc-openapi/pull/413.
Hey @joaodias14 , the PR has been closed (and is by a colleague of mine). That won't work because the Kotlin annotation does not have runtime retention so it won't be read by springdoc-openapi
Most helpful comment
@joaodias14 Using
@field:Schema(required = true)on every non-nullable field in model classes makes the model look very verbose. Can I automate this? Can I configure this in theOpenAPIbean? What I'd like to do is for every class having a non-nullable field, the generated yaml would have the required field. Looks like it might be possible if I useOpenAPI.components, but theSchemaobject does not hold the class information.