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

Determining whether a property is required/mandatory #1568

Closed
evantoli opened this issue Mar 22, 2022 · 3 comments
Closed

Determining whether a property is required/mandatory #1568

evantoli opened this issue Mar 22, 2022 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@evantoli
Copy link

evantoli commented Mar 22, 2022

Feature Description

JSON properties are incorrectly specified as required if they have been annotated with both @Null and any of @NotNull, @NotEmpty and @NotBlank.

This situation occurs when using Java constraint validation groups along with Spring to set the validation groups to constrain whether a property is required or not for certain scenarios.

For example, when a client must supply some value on create but is not permitted to change that value later on update.

To Reproduce

Using:

  • spring-boot-dependencies 2.5.4
  • springdoc-openapi-ui 1.6.6

For example, in a model class declare properties with annotations like:

public class SomeModel {

  @Null(
      groups = GroupA.class,
      message = "Must not be provided in scenario A")
  @NotBlank(
      groups = GroupB.class,
      message = "Must be provided in scenario B")
  @Schema(
      description = """
          Some description of this property.
          
          Notes:
          
          - Must not be provided in scenario A
          - Must be provided in scenario B""",
      required = false)
  private String someProperty;
  ...

For the above code, SpringDoc will generate an OpenAPI spec with something like the following:

"components": {
  "schemas": {
    "SomeModel": {
      "required": [
        "someProperty"
       ],

Expected behaviour

Do not specify that a JSON property is required if it has been annotated with both @Null and any of @NotNull, @NotEmpty and @NotBlank.

@bnasslahsen
Copy link
Contributor

@evantoli,

Duplicate of #1374 and #1486
There is no plan to support groups.
Feel free to propose a PR for it and we will review it.

@bnasslahsen bnasslahsen changed the title Bug when determining whether a property is required/mandatory Determining whether a property is required/mandatory Mar 22, 2022
@bnasslahsen bnasslahsen added the duplicate This issue or pull request already exists label Mar 22, 2022
@evantoli
Copy link
Author

evantoli commented Mar 23, 2022

Hi @bnasslahsen,

I have a PR that I can submit. What is the process?

I don't have permission to push my branch and I can't see any advice in the process in the CONTRIBUTING.adoc file.

BTW, I fully appreciate that you're not going to support validation groups. And my ticket isn't really about validation groups.

My desired behaviour is that the set of require properties should not contain any properties that may have contradictory validation annotations associated with them. Perhaps I didn't quite make that as clear as I could have.

Also, thanks so much for responding to this ticket so quickly. Much appreciated 🙏

@JamesPace-Carta
Copy link

Is it possible to disable the handling of the validation annotations?

We have a lot of code that uses groups and the swagger output it currently generates is incorrect. I can't seem to be able to provide an inline schema for the request, so I'm pretty much stuck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants