Skip to content

Fix behaviuor of required flag for schema class fields. #2185

Closed
@mikehalmamoj

Description

@mikehalmamoj

Describe the bug

In schema generated from Kotlin classes the required flag defaults to false even if the type is non-nullable. This contradicts the openapi-generator which uses the required flag to determine whether a generated property is nullable.

To Reproduce
Steps to reproduce the behavior:

  @Schema
  val id: Long,

This will generate a field that is not required. It should be required as it is non-nullable.

  • What version of spring-boot you are using?
    3.0.5
  • What modules and versions of springdoc-openapi are you using?
    org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.4

Expected behavior
When deciding if a field is required we need to consider the Schema annotation required attribute, the type of the field and whether there is a default value.

If there is a schema required value then respect that.
If not then required is true for non-nullable and false for nullable.
Except non-nullable with a default value for which required = false.

I would suggest the following test cases:

required? true true true true false false false false false false false true
schema required value true true true true false false false false none none none none
nullable true true false false true true false false true true false false
default value true false true false true false true false true false true false

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions