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

[BUG] [Java Resttemplate] Incorrect handling of free form query parameters #8352

Closed
gpspatacean opened this issue Jan 6, 2021 · 5 comments
Closed

Comments

@gpspatacean
Copy link

Description:
Currently, when using free form query parameters, the URI is not built correctly.

Steps to reproduce:
Input spec file:

openapi: 3.0.0
info:
  title: test
  version: 0.0.1
servers: 
  - url: "http://localhost"
paths:
  /some/endpoint: 
    get: 
      parameters: 
        - in: "query"
          name: "fixed"
          schema: 
            type: "string"
        - in: "query"
          name: "free-form"
          schema:
            type: "object"
          style: "form"
      responses: 
        200:
          description: "test"

Command to generate client files:
java -jar modules\openapi-generator-cli\target\openapi-generator-cli.jar generate -i spec.yml -o work -g java --library resttemplate

Client code ( usage of generated API ):

        ApiClient apiClient = new ApiClient();
        DefaultApi defaultApi = new DefaultApi(apiClient);
        Map<String, Object> freeFormQueryParams = new HashMap<>();
        freeFormQueryParams.put("this", "first");
        freeFormQueryParams.put("that", "second");
        freeFormQueryParams.put("other", "third");
        defaultApi.someEndpointGet("fixedValue", freeFormQueryParams);

Expected result of URI built for the endpoint:
http://localhost/some/endpoint?fixed=fixedValue&this=first&that=second&other=third

Actual result:
http://localhost/some/endpoint?fixed=fixedValue&free-form=%7Bthat%3Dsecond%2C+other%3Dthird%2C+this%3Dfirst%7D

Tested with master as of today.

I've added a patch that addresses this, I'll open a PR soon, too.

free-form-params.txt

@auto-labeler
Copy link

auto-labeler bot commented Jan 6, 2021

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@RomainPruvostMHH
Copy link
Contributor

Hello,
I get exactly the same problem with the generator "webclient". With the same fix purposed by @gspatace in the mustache file https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache, it works well.

To explain my problem a little :
I have a openapi v3 definition :

"/documents/search": {
			"get": {
				"operationId": "searchByParameters",
				"parameters": [
					{
						"name": "family",
						"in": "query",
						"required": true,
						"schema": {
							"type": "string"
						}
					},
					{
						"name": "metadata",
						"in": "query",
						"schema": {
							"type": "object",
							"additionalProperties": {
								"type": "string"
							}
						},
						"style": "form",
						"explode": true
					}
				]

The generated java code is :

Flux<Document> searchByParameters(String family, Map<String, String> metadata)

And when I test my code with a unit test :

        @Test
	void testSearchByParameters() throws Exception {
		final Map<String, String> params = new HashMap<>();
		params.put("id", "400824298");
		params.put("label", "Description");

		StepVerifier.create(documentsApi.searchByParameters("myFamily", params))
				.expectNextMatches(result -> result instanceof Document && result.getMetaData()
						.equals(params))
				.verifyComplete();
	}

I get the error below :
java.lang.IllegalArgumentException: Map has no value for 'id=400824298, label=Description' at org.springframework.web.util.UriComponents$MapTemplateVariables.getValue(UriComponents.java:345) at org.springframework.web.util.HierarchicalUriComponents$QueryUriTemplateVariables.getValue(HierarchicalUriComponents.java:1058) at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:262) at org.springframework.web.util.HierarchicalUriComponents.lambda$expandQueryParams$5(HierarchicalUriComponents.java:450) at java.base/java.util.Map.forEach(Map.java:661) at org.springframework.web.util.HierarchicalUriComponents.expandQueryParams(HierarchicalUriComponents.java:446) at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:435) at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:52) at org.springframework.web.util.UriComponents.expand(UriComponents.java:161) at org.springframework.web.util.DefaultUriBuilderFactory$DefaultUriBuilder.build(DefaultUriBuilderFactory.java:384) at org.springframework.web.util.DefaultUriBuilderFactory.expand(DefaultUriBuilderFactory.java:148) at org.springframework.web.reactive.function.client.DefaultWebClient$DefaultRequestBodyUriSpec.uri(DefaultWebClient.java:182) at org.springframework.web.reactive.function.client.DefaultWebClient$DefaultRequestBodyUriSpec.uri(DefaultWebClient.java:151) at fr.si2m.pulse.api.client.document.util.ApiClient.prepareRequest(ApiClient.java:543)

And with the fix, the call is done perfectly with my list of exploded query params :
GET /documents/search?family=myFamily&id=400824298&label=Description

Hope this help you to fix it.
Kind regards,

RomainPruvostMHH added a commit to RomainPruvostMHH/openapi-generator that referenced this issue Sep 19, 2021
wing328 pushed a commit that referenced this issue Sep 21, 2021
… free form query parameters (#10428)

* [Java][RestTemplate][WebClient] fix issue #8352 handling of free-form
query parameters with Java

* replace tab by spaces

* commit samples files generated by the generate-samples.sh script
@wing328 wing328 closed this as completed Sep 21, 2021
@wing328
Copy link
Member

wing328 commented Sep 21, 2021

Thanks for the fix by @RomainPruvostMHH

@gspatace, can you please give it a try with the latest master?

@gpspatacean
Copy link
Author

@wing328 Yes, sure, I'll test this on latest master, but by looking at the changes - it should work - they are exactly what I suggested initially in #8353 . I'll decline that PR once I've confirmed.

@gpspatacean
Copy link
Author

@wing328 it works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants