swagger-core
swagger-core copied to clipboard
Ignore @NotNull annotations
In ModelResolver, the list NOT_NULL_ANNOTATIONS is defined and contains the annotations that implies that the field is required.
Is there a way to disable this behavior, or remove some annotations from the list ? Or to ignore the annotation on some attributes ?
The assumption that a field annotated @NotNull should always be declared as required = true is incorrect and we should be able to disable this behavior. It can cause problem both for incoming data structure and for outgoing data structure.
Example of problems for incoming data structure
@Schema(description = "Additional properties")
@JsonProperty("additionalProperties")
@JsonSetter(nulls = Nulls.AS_EMPTY)
@NotNull
Map<String, String> additionalProperties
The parameter additionalProperties should be marked as required = false, even with the @NotNull annotation : when the additionalProperties attribute is not provided, the @JsonSetter annotation will ensure that the parameter is properly initialized with an empty Map. So on the Java side, it's clearly a non-nullable attribute, but on the openapi spec side it should be required = false.
Example of problems for outgoing data structure
@Schema(description = "Additional properties")
@JsonProperty("additionalProperties")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@NotNull
Map<String, String> additionalProperties
The parameter additionalProperties should be marked as required = false, even with the @NotNull annotation : when the additionalProperties parameter is an empty map, the @JsonInclude annotation will ensure that the attribute is not added to the payload. So on the Java side, it's a non-nullable attribute, but on the openapi spec side it should be required = false.
One idea to allow disabling the NOT_NULL_ANNOTATIONS on a given attribute :
- Add a attribute
forceUseRequiredon@Schemaannotation, with a default value asfalse - Ignore the
NOT_NULL_ANNOTATIONSwhenforceUseRequiredis equal totrue
I can submit a simple PR for this proposal.
Both examples in the issue description could then be easily fixed :
@Schema(description = "Additional properties", forceUseRequired = true)
@JsonProperty("additionalProperties")
@JsonSetter(nulls = Nulls.AS_EMPTY)
@NotNull
Map<String, String> additionalProperties
@Schema(description = "Additional properties", forceUseRequired = true)
@JsonProperty("additionalProperties")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@NotNull
Map<String, String> additionalProperties
Thanks for looking into it, and yes a PR is welcome
A possible alternative to your proposal is still using a new @Schema field but also deprecating required, similar to what done with AccessMode
This would have the advantage of having a single field to define the behavior, something like:
RequiredMode requiredMode() default RequiredMode.AUTO;
with
enum RequiredMode {
AUTO,
REQUIRED,
FORCED_REQUIRED
NOT_REQUIRED,
}
Hi @frantuma, thanks for the comments.
I've submitted a PR #4216 with the additional property forceUseRequired.
For your proposal with requiredMode, I'm not sure I understand all the possible values :
AUTOwill let the library decide based on its heuristicsNOT_REQUIREDwill force the result to berequired = falseregardless of heuristicsREQUIREDorFORCED_REQUIREDwill force the result to berequired = trueregardless of heuristics => what's the difference between the 2 options ? or why there's aFORCED_REQUIRED, but not aFORCED_NOT_REQUIRED?
@nvervelle, sorry the above was confusing and with an invalid value.. idea is the following:
AUTOwill let the library decide based on its heuristicsNOT_REQUIREDwill force the result to be required = false regardless of heuristicsREQUIREDwill will force the result to be required = true
Hi @frantuma, thanks for the clarification.
I've submitted a second PR #4221 implementing your suggestion.
Hi @frantuma , any hope of validating the PR ? https://github.com/swagger-api/swagger-core/pull/4221
Thanks a lot! merged #4286 which includes #4221