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

Make it possible to declare parameter type in annotation definition #2825

Open
kkochanski opened this issue Dec 19, 2024 · 0 comments
Open

Comments

@kkochanski
Copy link

I'm trying to create a public API using:

  • Spring Boot v3.4.0
  • Springdoc OpenAPI v2.7.0
  • Hibernate Validators v8.0.1

Request definition.

@PostMapping(path = "/process", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ResponseStatus(value = HttpStatus.OK)
public Response process(
    @Boolean @RequestParam(name = "is_dummy", defaultValue = "false") String isDummy,
    @Integer @Min(10) @RequestParam(name = "age") String age
) {
}

For that definition, library will output specification below.

"paths": {
    "/process": {
        "post": {
            "parameters": [
                {
                    "name": "is_dummy",
                    "in": "query",
                    "required": false,
                    "schema": {
                        "type": "string",
                        "default": "false"
                    }
                },
                {
                    "name": "age",
                    "in": "query",
                    "required": true,
                    "schema": {
                        "minimum": 10,
                        "type": "string"
                    }
                }
            ],
        }
    }
}

Library takes parameter type from it's Java definition.

There's a Spring Boot integration issue with Hibernate validators related to types validation. Before running validators, Spring is changing request default parameter type which is String to expected by Controller. Like it was done in given code.

For instance, we got that declaration.

@Min(10) @RequestParam(name = "age") int age

Then we're doing request where age gets some String value. That causes exception below.

org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Method parameter 'age': Failed to convert value of type 'java.lang.String' to required type 'int'; For input string: "asdas"

I can try to create @Integer validator, but it won't be reached, since I will get exception above first.

It means that every Controller parameter should be a String and then every validator should validate a type.

The disadvantage is that Swagger documentation won't be properly crafted. Every parameter will be marked as a String one, when in reality it's not.

I imagine it should be possible to provide an expected type in annotation as well. Like ...

@Parameter(name="age", type"integer")
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