Skip to content
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

Automatically add required if a field is @notNull or @NotBlank #2817

Open
lako12 opened this issue Dec 17, 2024 · 0 comments
Open

Automatically add required if a field is @notNull or @NotBlank #2817

lako12 opened this issue Dec 17, 2024 · 0 comments

Comments

@lako12
Copy link

lako12 commented Dec 17, 2024

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

@lako12 lako12 changed the title Aggiunge in automatico required se un campo è @notNull oppure @NotBlank Automatically add required if a field is @notNull or @NotBlank Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant