Skip to content

Commit

Permalink
Set default style based on the in parameter and set default explode b…
Browse files Browse the repository at this point in the history
…ased on the selected style (OpenAPITools#20127)
  • Loading branch information
CaptainAye committed Nov 24, 2024
1 parent 1ba18a0 commit 9420304
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5201,20 +5201,28 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
parameterSchema = null;
}

String defaultStyle = null;

if (parameter instanceof QueryParameter || "query".equalsIgnoreCase(parameter.getIn())) {
codegenParameter.isQueryParam = true;
codegenParameter.isAllowEmptyValue = parameter.getAllowEmptyValue() != null && parameter.getAllowEmptyValue();
defaultStyle = Parameter.StyleEnum.FORM.toString();
} else if (parameter instanceof PathParameter || "path".equalsIgnoreCase(parameter.getIn())) {
codegenParameter.required = true;
codegenParameter.isPathParam = true;
defaultStyle = Parameter.StyleEnum.SIMPLE.toString();
} else if (parameter instanceof HeaderParameter || "header".equalsIgnoreCase(parameter.getIn())) {
codegenParameter.isHeaderParam = true;
defaultStyle = Parameter.StyleEnum.SIMPLE.toString();
} else if (parameter instanceof CookieParameter || "cookie".equalsIgnoreCase(parameter.getIn())) {
codegenParameter.isCookieParam = true;
defaultStyle = Parameter.StyleEnum.FORM.toString();
} else {
LOGGER.warn("Unknown parameter type: {}", parameter.getName());
}

codegenParameter.style = parameter.getStyle() == null ? defaultStyle : parameter.getStyle().toString();

if (parameterSchema == null) {
LOGGER.error("Not handling {} as Body Parameter at the moment", parameter);
finishUpdatingParameter(codegenParameter, parameter);
Expand Down Expand Up @@ -5249,9 +5257,10 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
codegenParameter.isMatrix = Parameter.StyleEnum.MATRIX == parameter.getStyle();
}

// the default value is false
// the default value is true if style is set to form, false otherwise
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#user-content-parameterexplode
codegenParameter.isExplode = parameter.getExplode() != null && parameter.getExplode();
var isExplodeDefault = Parameter.StyleEnum.FORM.toString().equals(codegenParameter.style);
codegenParameter.isExplode = parameter.getExplode() != null ? parameter.getExplode() : isExplodeDefault;

// TODO revise collectionFormat, default collection format in OAS 3 appears to multi at least for query parameters
// https://swagger.io/docs/specification/serialization/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,22 @@ public void shouldProperlyExplodeRestTemplateQueryParameters_issue907() {
+ " objectParam.getSomeInteger()));");
}

@Test
public void shouldProperlyExplodeRestTemplateArrayQueryParameters_issue20127() {
final Map<String, File> files = generateFromContract(
"src/test/resources/3_0/exploded-query-param-array.yaml",
RESTTEMPLATE
);

JavaFileAssert.assertThat(files.get("DefaultApi.java"))
.printFileContent()
.assertMethod("getSomeValueWithHttpInfo")
.bodyContainsLines(
"localVarQueryParams.putAll(apiClient.parameterToMultiValueMap("
+ "ApiClient.CollectionFormat.valueOf(\"multi\".toUpperCase(Locale.ROOT)),"
+ " \"QueryArray\", queryArray));");
}

@Test
public void shouldProperlyExplodeWebClientQueryParameters() {
final Map<String, File> files = generateFromContract(
Expand All @@ -1936,6 +1952,22 @@ public void shouldProperlyExplodeWebClientQueryParameters() {
+ " objectParam.getSomeInteger()));");
}

@Test
public void shouldProperlyExplodeWebClientArrayQueryParameters_issue20127() {
final Map<String, File> files = generateFromContract(
"src/test/resources/3_0/exploded-query-param-array.yaml",
JavaClientCodegen.WEBCLIENT
);

JavaFileAssert.assertThat(files.get("DefaultApi.java"))
.printFileContent()
.assertMethod("getSomeValueRequestCreation")
.bodyContainsLines(
"queryParams.putAll(apiClient.parameterToMultiValueMap("
+ "ApiClient.CollectionFormat.valueOf(\"multi\".toUpperCase(Locale.ROOT)),"
+ " \"QueryArray\", queryArray));");
}

private static Map<String, File> generateFromContract(final String pathToSpecification, final String library) {
return generateFromContract(pathToSpecification, library, new HashMap<>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ paths:
type: array
items:
type: string
- name: QueryArray
in: query
schema:
type: array
items:
type: string
responses:
'200':
description: Some return value
Expand Down

0 comments on commit 9420304

Please sign in to comment.