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 11a7cb4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -915,16 +915,27 @@ public String toEnumVarName(String value, String datatype) {
}

public boolean specVersionGreaterThanOrEqualTo310(OpenAPI openAPI) {
String originalSpecVersion = getOriginalSpecVersion(openAPI);
return getMajorSpecVersion(originalSpecVersion) == 3 && getMinorSpecVersion(originalSpecVersion) >= 1;
}

private String getOriginalSpecVersion(OpenAPI openAPI) {
String originalSpecVersion;
String xOriginalSwaggerVersion = "x-original-swagger-version";
if (openAPI.getExtensions() != null && !openAPI.getExtensions().isEmpty() && openAPI.getExtensions().containsValue(xOriginalSwaggerVersion)) {
originalSpecVersion = (String) openAPI.getExtensions().get(xOriginalSwaggerVersion);
} else {
originalSpecVersion = openAPI.getOpenapi();
}
Integer specMajorVersion = Integer.parseInt(originalSpecVersion.substring(0, 1));
Integer specMinorVersion = Integer.parseInt(originalSpecVersion.substring(2, 3));
return specMajorVersion == 3 && specMinorVersion >= 1;
return originalSpecVersion;
}

private Integer getMajorSpecVersion(String originalSpecVersion) {
return Integer.parseInt(originalSpecVersion.substring(0, 1));
}

private Integer getMinorSpecVersion(String originalSpecVersion) {
return Integer.parseInt(originalSpecVersion.substring(2, 3));
}

/**
Expand Down Expand Up @@ -5201,16 +5212,22 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
parameterSchema = null;
}

String defaultV3Style = null;

if (parameter instanceof QueryParameter || "query".equalsIgnoreCase(parameter.getIn())) {
codegenParameter.isQueryParam = true;
codegenParameter.isAllowEmptyValue = parameter.getAllowEmptyValue() != null && parameter.getAllowEmptyValue();
defaultV3Style = Parameter.StyleEnum.FORM.toString();
} else if (parameter instanceof PathParameter || "path".equalsIgnoreCase(parameter.getIn())) {
codegenParameter.required = true;
codegenParameter.isPathParam = true;
defaultV3Style = Parameter.StyleEnum.SIMPLE.toString();
} else if (parameter instanceof HeaderParameter || "header".equalsIgnoreCase(parameter.getIn())) {
codegenParameter.isHeaderParam = true;
defaultV3Style = Parameter.StyleEnum.SIMPLE.toString();
} else if (parameter instanceof CookieParameter || "cookie".equalsIgnoreCase(parameter.getIn())) {
codegenParameter.isCookieParam = true;
defaultV3Style = Parameter.StyleEnum.FORM.toString();
} else {
LOGGER.warn("Unknown parameter type: {}", parameter.getName());
}
Expand Down Expand Up @@ -5247,11 +5264,15 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
codegenParameter.style = parameter.getStyle().toString();
codegenParameter.isDeepObject = Parameter.StyleEnum.DEEPOBJECT == parameter.getStyle();
codegenParameter.isMatrix = Parameter.StyleEnum.MATRIX == parameter.getStyle();
} else if (openAPI != null && getMajorSpecVersion(getOriginalSpecVersion(openAPI)) >= 3) {
// Properly sets the default style for oas V3
codegenParameter.style = defaultV3Style;
}

// 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();
boolean isFormStyle = Parameter.StyleEnum.FORM.toString().equals(codegenParameter.style);
codegenParameter.isExplode = parameter.getExplode() != null ? parameter.getExplode() : isFormStyle;

// 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 Expand Up @@ -7105,18 +7126,23 @@ public static Set<String> getProducesInfo(final OpenAPI openAPI, final Operation
}

protected String getCollectionFormat(Parameter parameter) {
if (Parameter.StyleEnum.FORM.equals(parameter.getStyle())) {
String style = parameter.getStyle() != null ? parameter.getStyle().toString() : null;
return getCollectionFormat(style, parameter.getExplode());
}

private String getCollectionFormat(String style, Boolean explode) {
if (Parameter.StyleEnum.FORM.toString().equals(style)) {
// Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#style-values
if (Boolean.TRUE.equals(parameter.getExplode())) { // explode is true (default)
if (Boolean.TRUE.equals(explode)) { // explode is true (default)
return "multi";
} else {
return "csv";
}
} else if (Parameter.StyleEnum.SIMPLE.equals(parameter.getStyle())) {
} else if (Parameter.StyleEnum.SIMPLE.toString().equals(style)) {
return "csv";
} else if (Parameter.StyleEnum.PIPEDELIMITED.equals(parameter.getStyle())) {
} else if (Parameter.StyleEnum.PIPEDELIMITED.toString().equals(style)) {
return "pipes";
} else if (Parameter.StyleEnum.SPACEDELIMITED.equals(parameter.getStyle())) {
} else if (Parameter.StyleEnum.SPACEDELIMITED.toString().equals(style)) {
return "ssv";
} else {
return null;
Expand Down Expand Up @@ -8424,23 +8450,7 @@ protected static boolean isJsonVendorMimeType(String mime) {
* @return string for a collectionFormat.
*/
protected String getCollectionFormat(CodegenParameter codegenParameter) {
if ("form".equals(codegenParameter.style)) {
// Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#style-values
if (codegenParameter.isExplode) {
return "multi";
} else {
return "csv";
}
} else if ("simple".equals(codegenParameter.style)) {
return "csv";
} else if ("pipeDelimited".equals(codegenParameter.style)) {
return "pipes";
} else if ("spaceDelimited".equals(codegenParameter.style)) {
return "ssv";
} else {
// Doesn't map to any of the collectionFormat strings
return null;
}
return getCollectionFormat(codegenParameter.style, codegenParameter.isExplode);
}

private CodegenComposedSchemas getComposedSchemas(Schema schema) {
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 11a7cb4

Please sign in to comment.