Skip to content

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

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

Closed
dkotvan opened this issue Feb 26, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@dkotvan
Copy link

dkotvan commented Feb 26, 2020

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

@bnasslahsen
Copy link
Collaborator

Hi @dkotvan,

You will be able to use ParameterCustomizer, and override @NotNull behaviour with v1.2.33, as follow:

@Component
public class ParameterCustomizer implements org.springdoc.core.customizers.ParameterCustomizer {
	@Override
	public Parameter customize(Parameter parameterModel, java.lang.reflect.Parameter parameter, HandlerMethod handlerMethod) {
		NotNull annotation = parameter.getAnnotation(NotNull.class);
		if (annotation != null) {
			parameterModel.required(false);
		}
		return parameterModel;
	}
}

@dkotvan
Copy link
Author

dkotvan commented Feb 28, 2020

Thanks, @bnasslahsen

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
@springdoc springdoc locked as too heated and limited conversation to collaborators Dec 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants