Description
I have a project with Swagger 3, I noticed a different behavior compared to Swagger 2 and I think it's a bug because with a structure of a certain type, I can make the call to the API, but Swagger doesn't allow me to do it.
I have this API:
@PostMapping(path = "/testOne", consumes = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<String> testOne(@ParameterObject @Valid ClassOne test) {
return ResponseEntity.ok("ok");
}
@Data
public class ClassOne {
@Valid
private ClassTwo description;
}
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ClassTwo {
@NotNull
private String name;
}
As you can see, ClassOne
has an object with the @Valid
annotation and I am not obliged to pass it (because maybe I want to pass other attributes that I have not added in this example). However, the @Valid
annotation ensures that if I pass any attribute that is inside that class, then it must respect the rules that I indicate inside (@NotNull etc.). The problem is that on the swagger it says required, even if I do not indicate required and therefore I cannot make the call to the API. This is therefore a problem as I said before, because I may want to send other attributes of ClassOne
.
This problem occurs only in the case of @PraameterObject
, while if I use @RequestBody
, it is not mandatory.
In my opinion swagger should not automatically add requird if an attribute has the annotation @NotNull
or @NotBlank
, it should refer to what the user indicates with the annotation @Schema
attached is a zip with an example.
demo.zip