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

Ignoring @Parameter(required = false) ? #2827

Closed
KneupnerTrackunit opened this issue Dec 19, 2024 · 5 comments
Closed

Ignoring @Parameter(required = false) ? #2827

KneupnerTrackunit opened this issue Dec 19, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@KneupnerTrackunit
Copy link

Describe the bug

I have this declaration line:

    @Parameter(description = "The number of individual objects that are returned in each page.", in = QUERY, allowEmptyValue = true, required = false, schema = @Schema(defaultValue = "200", requiredMode = Schema.RequiredMode.NOT_REQUIRED, nullable = true, minimum = "1", maximum = "200"))
    @Max(value = 200L)
    @Min(value = 1L)
    private Integer limit = MAX_LIMIT;

and it results in

{
          "allowEmptyValue" : true,
          "description" : "The number of individual objects that are returned in each page.",
          "in" : "query",
          "name" : "limit",
          "required" : true,
          "schema" : {
            "type" : "string",
            "maximum" : 200,
            "minimum" : 1,
          }
}

but it should result in:

{
          "allowEmptyValue" : true,
          "description" : "The number of individual objects that are returned in each page.",
          "in" : "query",
          "name" : "limit",
          "required" : false,
          "schema" : {
            "type" : "string",
            "default" : "200",
            "maximum" : 200,
            "minimum" : 1,
          }
}

(created via springdoc-openapi-maven-plugin).

To Reproduce
Steps to reproduce the behavior:

  • What version of spring-boot you are using? 3.4.0
  • What modules and versions of springdoc-openapi are you using? 2.7.0
  • What is the actual and the expected result using OpenAPI Description (yml or json)? JSON
  • Provide with a sample code (HelloController) or Test that reproduces the problem - will be added within a few hours

Expected behavior

I would like to have no changes to previous version, i.e. return a required = false and add the default = 200 back in.

Screenshots
This is the diff:
image

Additional context
Add any other context about the problem here.

@uc4w6c
Copy link
Collaborator

uc4w6c commented Dec 29, 2024

Please provide a Minimal, Reproducible Example - with HelloController that reproduces the issue.

Follow code returned the value required: false.

  @GetMapping
  public String index(@ParameterObject IntegerMaxRequest request) {
    return null;
  }

  public class IntegerMaxRequest {
      @Parameter(description = "The number of individual objects that are returned in each page.",
          in = QUERY, allowEmptyValue = true,
          required = false,
          schema = @Schema(defaultValue = "200",
              requiredMode = Schema.RequiredMode.NOT_REQUIRED,
              nullable = true,
              minimum = "1",
              maximum = "200"))
      @Max(value = 200L)
      @Min(value = 1L)
      private Integer limit = Integer.MAX_VALUE;

    public Integer getLimit() {
      return limit;
    }
  }

@uc4w6c uc4w6c closed this as completed Dec 29, 2024
@KneupnerTrackunit
Copy link
Author

issueReport.zip
Apologies for being delayed. But here is the example.

@uc4w6c uc4w6c reopened this Dec 29, 2024
@uc4w6c
Copy link
Collaborator

uc4w6c commented Dec 30, 2024

If the @NotNull annotation is applied, it takes precedence over the @parameter annotation.

AbstractRequestService

However, I’m sorry, but I don't have time to take this issue.
So if you’re interested, please feel free to create a PR or you will remove NotNull annotation.

Follow code is minimum.

  @GetMapping
  public String index(@ParameterObject IntegerMaxRequest request) {
    return null;
  }

  public class IntegerMaxRequest {
      @Parameter(description = "The number of individual objects that are returned in each page.",
          in = QUERY, allowEmptyValue = true,
          required = false,
          schema = @Schema(defaultValue = "200",
              requiredMode = Schema.RequiredMode.NOT_REQUIRED,
              nullable = true,
              minimum = "1",
              maximum = "200"))
      @Max(value = 200L)
      @Min(value = 1L)
      private Integer limit = Integer.MAX_VALUE;

    @NotNull
    public Integer getLimit() {
      return limit;
    }
  }

@KneupnerTrackunit
Copy link
Author

KneupnerTrackunit commented Dec 30, 2024

Thanks! I will have a look, but it will be at least a week myself.

@bnasslahsen
Copy link
Contributor

bnasslahsen commented Dec 30, 2024

@KneupnerTrackunit,

This PR #2817 fixes also this issue!

@bnasslahsen bnasslahsen added the bug Something isn't working label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants