Skip to content

Commit

Permalink
Issue 582: Replace query parameters encoder (#583)
Browse files Browse the repository at this point in the history
Co-authored-by: Charles Dubois <103174266+CharlesDuboisSAP@users.noreply.github.com>
  • Loading branch information
mvigenin and CharlesDuboisSAP authored Sep 11, 2024
1 parent e8464dd commit e7dd195
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

package com.sap.cloud.sdk.services.openapi.apiclient;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
Expand Down Expand Up @@ -40,6 +38,7 @@
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
Expand Down Expand Up @@ -699,14 +698,7 @@ public <T> T invokeAPI(
//encode the query parameters in case they contain unsafe characters
for( final List<String> values : queryParams.values() ) {
if( values != null ) {
for( int i = 0; i < values.size(); i++ ) {
try {
values.set(i, URLEncoder.encode(values.get(i), "utf8"));
}
catch( final UnsupportedEncodingException e ) {
throw new OpenApiRequestException(e);
}
}
values.replaceAll(queryParam -> UriUtils.encodeQueryParam(queryParam, "utf8"));
}
}
builder.queryParams(queryParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package com.sap.cloud.sdk.services.openapi.apiclient;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -20,6 +21,8 @@
import org.springframework.test.web.client.ExpectedCount;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.client.response.MockRestResponseCreators;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

Expand Down Expand Up @@ -89,6 +92,35 @@ void testApiClientWithoutExplicitRestTemplateDoesNotConsiderGetters()
server.verify();
}

@Test
void testApiClientWithQueryParams()
{
final String filterQueryValue = "emails.value eq \"my.email@test.com\"";
final String expectedFilterQuery = "emails.value%20eq%20%22my.email@test.com%22";
final String filterQueryParam = "filter";
MultiValueMap<String, String> execQueryParams = new LinkedMultiValueMap<>();
List<String> values = new ArrayList<>();
values.add(filterQueryValue);
execQueryParams.put("filter", values);

final ApiClient apiClient = new ApiClient().setBasePath(BASE_PATH);
final RestTemplate restTemplate = apiClient.getRestTemplate();
final MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build();

server
.expect(
ExpectedCount.once(),
requestTo(BASE_PATH + RELATIVE_PATH + "?" + filterQueryParam + "=" + expectedFilterQuery))
.andExpect(method(HttpMethod.GET))
.andExpect(queryParam(filterQueryParam, expectedFilterQuery))
.andRespond(MockRestResponseCreators.withSuccess(SUCCESS_BODY, MediaType.TEXT_PLAIN));

final MyTestOpenApiService myTestOpenApiService = new MyTestOpenApiService(apiClient);
myTestOpenApiService.invokeApiEndpoint(HttpMethod.GET, null, execQueryParams);

server.verify();
}

private static class MyDto
{
@JsonProperty( "Return" )
Expand All @@ -112,25 +144,28 @@ private static class MyTestOpenApiService extends AbstractOpenApiService
super(apiClient);
}

void invokeApiEndpoint()
void invokeApiEndpoint( @Nullable Object body )
{
invokeApiEndpoint(null);
invokeApiEndpoint(HttpMethod.POST, body, null);
}

void invokeApiEndpoint( @Nullable Object body )
void invokeApiEndpoint(
HttpMethod method,
@Nullable Object body,
@Nullable MultiValueMap<String, String> queryParams )
{
assertThat(apiClient.getBasePath()).isEqualTo(BASE_PATH);

final ParameterizedTypeReference<String> returnType = new ParameterizedTypeReference<String>()
final ParameterizedTypeReference<String> returnType = new ParameterizedTypeReference<>()
{
};

final String s =
apiClient
.invokeAPI(
UriComponentsBuilder.fromPath(RELATIVE_PATH).toUriString(),
HttpMethod.POST,
null,
method,
queryParams,
body,
new HttpHeaders(),
null,
Expand Down
2 changes: 1 addition & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@

### 🐛 Fixed Issues

-
- OpenAPI QueryParameters are now encoded

0 comments on commit e7dd195

Please sign in to comment.