Skip to content

Commit

Permalink
feat: [TSI-2083] enable format_options argument for java-client (#426)
Browse files Browse the repository at this point in the history
* TSI-2083 update java client generation file

* tsi-2083 update test file

* testing out changes with different string formation

* Add typecasting workaround to test build

* Updated assertion to match encoded params

* try improving gradle test output

* try improving gradle test output

* try improving gradle test output

* Remove redundant line

* Adds ability to parse and embed nested params

* Add deep object directive

---------

Co-authored-by: Mladen Jablanovic <[email protected]>
  • Loading branch information
hahmed-dev and jablan authored Oct 30, 2023
1 parent 7634985 commit faa8cb3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
with:
distribution: temurin
java-version: 11
- name: Run Gradle Tests
- name: Run gradle test
uses: gradle/gradle-build-action@v2
with:
gradle-version: 7.1.1
build-root-directory: ./clients/java
arguments: test
arguments: test --info
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ public void localeDownloadTest() throws ApiException, IOException, InterruptedEx

mockBackend.enqueue(mockResponse);


Map<String, String> nestedFormatOptionsMap = new HashMap<>();
nestedFormatOptionsMap.put("nested_option", "sub_option");

Map<String, Object> formatOptionsMap = new HashMap<>();
formatOptionsMap.put("omit_separator_space", "true");
formatOptionsMap.put("fallback_language", "en");
formatOptionsMap.put("more_options", nestedFormatOptionsMap);

String projectId = "MY_PROJECT_ID";
String id = "MY_ID";
String xPhraseAppOTP = null;
Expand All @@ -151,7 +160,7 @@ public void localeDownloadTest() throws ApiException, IOException, InterruptedEx
Boolean includeTranslatedKeys = null;
Boolean keepNotranslateTags = null;
Boolean convertEmoji = null;
Object formatOptions = null;
Object formatOptions = formatOptionsMap;
String encoding = null;
Boolean skipUnverifiedTranslations = null;
Boolean includeUnverifiedTranslations = null;
Expand All @@ -164,7 +173,8 @@ public void localeDownloadTest() throws ApiException, IOException, InterruptedEx
Assert.assertEquals("Correct file contents", fileContents, body);

RecordedRequest recordedRequest = mockBackend.takeRequest();
Assert.assertEquals("Request path", "//projects/MY_PROJECT_ID/locales/MY_ID/download", recordedRequest.getPath());
// for some reason with deep nested query params, ordering of query params change
Assert.assertEquals("Request path", "//projects/MY_PROJECT_ID/locales/MY_ID/download?format_options%5Bomit_separator_space%5D=true&format_options%5Bmore_options%5D%5Bnested_option%5D=sub_option&format_options%5Bfallback_language%5D=en", recordedRequest.getPath());
}

/**
Expand Down
4 changes: 3 additions & 1 deletion doc/compiled.json
Original file line number Diff line number Diff line change
Expand Up @@ -7450,7 +7450,9 @@
"schema": {
"type": "object",
"properties": {}
}
},
"style": "deepObject",
"explode": true
},
{
"description": "Enforces a specific encoding on the file contents. Valid options are \"UTF-8\", \"UTF-16\" and \"ISO-8859-1\".",
Expand Down
8 changes: 7 additions & 1 deletion openapi-generator/templates/java/build.gradle.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ if(hasProperty('target') && target == 'android') {
main = System.getProperty('mainClass')
classpath = sourceSets.main.runtimeClasspath
}

task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
Expand Down Expand Up @@ -177,6 +177,12 @@ dependencies {
testCompile "junit:junit:$junit_version"
}

test {
testLogging {
outputs.upToDateWhen {false}
showStandardStreams = true
}
}

publishing {
publications {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,27 @@ public class ApiClient {
}
}
public List<Pair> mappedParameterToPairs(String name, Object parameter){
List<Pair> params = new ArrayList<Pair>();
if(parameter instanceof Map){
Map<String, Object> mappedParameter = (Map<String, Object>) parameter;
for(Map.Entry<String, Object> entry : mappedParameter.entrySet()){
String key = name + "[" + entry.getKey() + "]";
Object value = entry.getValue();
{{! TODO check why nested params ordering is different }}
params.addAll(parsedMappedParams(key, value, new ArrayList<Pair>()));
}
}
return params;
}
/**
* Formats the specified query parameter to a list containing a single {@code Pair} object.
*
Expand Down Expand Up @@ -1445,4 +1466,21 @@ public class ApiClient {
throw new AssertionError(e);
}
}

private List<Pair> parsedMappedParams(String key, Object value, List<Pair> params){
if(value instanceof Map){
Map<String, Object> mappedValue = (Map<String, Object>) value;
for(Map.Entry<String, Object> entry : mappedValue.entrySet()){
String nestedKey = key + "[" + entry.getKey() + "]";
parsedMappedParams(nestedKey, entry.getValue(), params);
}
}
else{
params.add(new Pair(key, parameterToString(value)));
}

return params;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ public class {{classname}} {
{{javaUtilPrefix}}List<Pair> localVarCollectionQueryParams = new {{javaUtilPrefix}}ArrayList<Pair>();
{{#queryParams}}
if ({{paramName}} != null) {
{{#isDeepObject}}
localVarQueryParams.addAll(localVarApiClient.mappedParameterToPairs("{{baseName}}", {{paramName}}));
{{/isDeepObject}}
{{^isDeepObject}}
{{#collectionFormat}}localVarCollectionQueryParams.addAll(localVarApiClient.parameterToPairs("{{{collectionFormat}}}", {{/collectionFormat}}{{^collectionFormat}}localVarQueryParams.addAll(localVarApiClient.parameterToPair({{/collectionFormat}}"{{baseName}}", {{paramName}}));
{{/isDeepObject}}
}

{{/queryParams}}
Expand Down
2 changes: 2 additions & 0 deletions paths/locales/download.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ parameters:
schema:
type: object
properties: {}
style: deepObject
explode: true
- description: Enforces a specific encoding on the file contents. Valid options are "UTF-8", "UTF-16" and "ISO-8859-1".
example:
name: encoding
Expand Down

0 comments on commit faa8cb3

Please sign in to comment.