Skip to content

Allow @Schema annotation to set an attribute as not required even if it's annotated with @NotNull #459

Closed
@dkotvan

Description

@dkotvan

Is your feature request related to a problem? Please describe.
In our APIs we have parameters which are not required but if they are sent they should not be null. We need they to not be required but not being nullable.

Example ( in Kotlin ):

class PostRequest(
     ..... 
    @field:Schema(required = false, description = COLLATERALS_DESCRIPTION, defaultValue = "[]")
    @field:NotNull
    val collaterals: List<CollateralRequest> = listOf()
)

In this example the field collaterals is initialized with an empty array if it was not present in the request body but if the client send it it should not be null.

The generated OpenAPI is something like this:

schema:
  components:
    PostRequest:
      type: object
      required:
        - collaterals
      properties:
        collaterals:
          $ref: "#schema/components/#CollateralRequest"

But it should be:

schema:
  components:
    PostRequest:
      type: object
      properties:
        collaterals:
          $ref: "#schema/components/#CollateralRequest"

Because we put required = false at the schema annotation.

Describe the solution you'd like
I would like that the required attribute of the schema annotation had precedence before the @NotNull or even that the non-nullable types in Kotlin

Describe alternatives you've considered
I didn't found an alternative

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions