-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
[BUG] [Spring] [Spring-Boot] Exploded query parameters are not correctly serialized in controller #14860
Comments
One solution is to "explode" the object in the method signature, so instead of default ResponseEntity<SomeReturnValue> search(
@NotNull @Parameter(name = "object-param", description = "", required = true, in = ParameterIn.QUERY) @Valid SearchObjectParamParameter objectParam,
... becomes default ResponseEntity<SomeReturnValue> search(
@Parameter(name = "kebab-param", description = "", in = ParameterIn.QUERY) @Valid @RequestParam(value = "kebab-param", required = false) String kebabParam
@Parameter(name = "snake_param", description = "", in = ParameterIn.QUERY) @Valid @RequestParam(value = "snake_param", required = false) String snakeParam
... But don't know if this is the right approach for Spring Boot, or if there is a better one. Suggestions welcome. |
thanks for reporting the issue. cc @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) |
Probably make sense to explode object into parameters. There is no may to map Object with |
Yeah, I was coming to the conclusion that there was a sane way to map an Object to |
@GregDThomas that's exactly the reason, why I would prefer to go with config option and by default leave as it is. I believe there are already users, which are fine with current behavior. Current behavior works pretty fine, when request parameters name has exactly same names as generate model's fields. Example /api/search:
get:
operationId: Search
parameters:
- name: regularParam
in: query
required: false
schema:
type: string
- name: objectParam
in: query
required: true
schema:
type: object
properties:
someField1:
type: string
someField2:
type: string
someCamelString:
type: string
responses:
'200':
description: Some description.
content:
application/json:
schema:
$ref: '#/components/schemas/SomeReturnValue' and generated model would be public class ObjectParam {
private String someField1;
private String someField2;
private String someCamelString;
} In above case current generator's behavior is perfectly fine |
More research over lunch has turned up https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/method/support/HandlerMethodArgumentResolver.html which should allow a customer parameter annotation to be used - I may experiment with that to see if I can get it working ... |
I saw that thing. To be honest I would prefer to avoid this. Think about cases, when in single model you are trying to deal with list of custom models /api/search:
get:
operationId: Search
parameters:
- name: regularParam
in: query
required: false
schema:
type: string
- name: objectParam
in: query
required: true
schema:
type: object
properties:
someField1:
type: string
listField:
type: array
items:
type: object
properties:
id:
type: integer
name:
type: string
statuses:
type: string
enum: [enum1, enum2, enum3]
responses:
'200':
description: Some description.
content:
application/json:
schema:
$ref: '#/components/schemas/SomeReturnValue'
Handling such thing would be a real pain .. What your thoughts on this? |
I suspect there's no good way to handle that, tbh. But I think;
gives 80% of the benefit of exploded query params for 20% of the effort. |
@GregDThomas did you find a workaround for this? we're also facing this issue. In generated model - it also has annotation with correct case - snake_case here. It still expects
|
No; AFAICT you'll either need your field names to be camel case, otherwise requires a change to the generator code which I've yet to get time to take a look at. |
Bug Report Checklist
Description
When generating code with an exploded query parameter, the
spring
generator with thespring-boot
library does not correctly resolve the supplied parameters. Some experimentation suggests that the serialization is using the name of the field in the Java object, not the name of the field in the OpenAPI specification.openapi-generator version
6.4.0
OpenAPI declaration file content or url
https://github.com/GregDThomas/openapi-generator-spring-boot-example/blob/main/specs/explode-query-parameter.yaml
Generation Details
Using the OpenAPI Generator Gradle Plugin with
Steps to reproduce
See the sample project at https://github.com/GregDThomas/openapi-generator-spring-boot-example and more specifically the tests in ExampleControllerTest
Specifically -
someCamelString
) are correctly serialized.--
some-kebab-string
/someKebabString
--
some_snake_string
/someSnakeString
Related issues/PRs
Suggest a fix
The text was updated successfully, but these errors were encountered: