Skip to content

Commit 7e97427

Browse files
[bug][kotlin-spring] fix allowableValues quotes in documentation anno… (#21700)
* [bug][kotlin-spring] fix allowableValues quotes in documentation annotations for path parameters * Remove unused imports in KotlinSpringServerCodegenTest * Fix Swagger1 annotation * Streamline Swagger1 annotation * Remove extra line break --------- Co-authored-by: Chris Gual <[email protected]>
1 parent bb6acc1 commit 7e97427

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isPathParam}}{{#useBeanValidation}}{{>beanValidationPathParams}}{{/useBeanValidation}}{{#swagger2AnnotationLibrary}}@Parameter(description = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}{{#defaultValue}}, schema = Schema(allowableValues = [{{#enumVars}}"{{#lambdaEscapeInNormalString}}{{{value}}}{{/lambdaEscapeInNormalString}}"{{^-last}}, {{/-last}}{{/enumVars}}]{{^isContainer}}, defaultValue = {{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{{defaultValue}}}{{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{/isContainer}}){{/defaultValue}}{{/allowableValues}}{{#allowableValues}}{{^defaultValue}}, schema = Schema(allowableValues = [{{#enumVars}}"{{#lambdaEscapeInNormalString}}{{{value}}}{{/lambdaEscapeInNormalString}}"{{^-last}}, {{/-last}}{{/enumVars}}]){{/defaultValue}}{{/allowableValues}}{{^allowableValues}}{{#defaultValue}}{{^isContainer}}, schema = Schema(defaultValue = {{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{{defaultValue}}}{{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}){{/isContainer}}{{/defaultValue}}{{/allowableValues}}){{/swagger2AnnotationLibrary}}{{#swagger1AnnotationLibrary}}@ApiParam(value = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}, allowableValues = "{{#enumVars}}{{#lambda.escapeDoubleQuote}}{{{value}}}{{/lambda.escapeDoubleQuote}}{{^-last}}, {{/-last}}{{/enumVars}}"{{/allowableValues}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}){{/swagger1AnnotationLibrary}} @PathVariable("{{baseName}}") {{{paramName}}}: {{>optionalDataType}}{{/isPathParam}}
1+
{{#isPathParam}}{{#useBeanValidation}}{{>beanValidationPathParams}}{{/useBeanValidation}}{{#swagger2AnnotationLibrary}}@Parameter(description = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}{{#defaultValue}}, schema = Schema(allowableValues = [{{#values}}"{{{.}}}"{{^-last}}, {{/-last}}{{/values}}]{{^isContainer}}, defaultValue = {{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{{defaultValue}}}{{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{/isContainer}}){{/defaultValue}}{{/allowableValues}}{{#allowableValues}}{{^defaultValue}}, schema = Schema(allowableValues = [{{#values}}"{{{.}}}"{{^-last}}, {{/-last}}{{/values}}]){{/defaultValue}}{{/allowableValues}}{{^allowableValues}}{{#defaultValue}}{{^isContainer}}, schema = Schema(defaultValue = {{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{{defaultValue}}}{{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}){{/isContainer}}{{/defaultValue}}{{/allowableValues}}){{/swagger2AnnotationLibrary}}{{#swagger1AnnotationLibrary}}@ApiParam(value = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}, allowableValues = "{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}){{/swagger1AnnotationLibrary}} @PathVariable("{{baseName}}") {{{paramName}}}: {{>optionalDataType}}{{/isPathParam}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.swagger.v3.parser.core.models.ParseOptions;
88
import java.util.HashMap;
99
import java.util.function.Consumer;
10+
import java.util.stream.Stream;
1011
import org.apache.commons.io.FileUtils;
1112
import org.assertj.core.api.Assertions;
1213
import org.jetbrains.annotations.NotNull;
@@ -15,13 +16,14 @@
1516
import org.openapitools.codegen.DefaultGenerator;
1617
import org.openapitools.codegen.TestUtils;
1718
import org.openapitools.codegen.config.CodegenConfigurator;
18-
import org.openapitools.codegen.java.assertions.JavaFileAssert;
1919
import org.openapitools.codegen.kotlin.KotlinTestUtils;
2020
import org.openapitools.codegen.languages.KotlinSpringServerCodegen;
2121
import org.openapitools.codegen.languages.features.CXFServerFeatures;
22+
import org.openapitools.codegen.languages.features.DocumentationProviderFeatures;
2223
import org.openapitools.codegen.languages.features.DocumentationProviderFeatures.AnnotationLibrary;
2324
import org.openapitools.codegen.languages.features.DocumentationProviderFeatures.DocumentationProvider;
2425
import org.testng.Assert;
26+
import org.testng.annotations.DataProvider;
2527
import org.testng.annotations.Test;
2628

2729
import java.io.File;
@@ -1232,6 +1234,79 @@ public void testValidationsInQueryParams_issue21238_Api_Delegate() throws IOExce
12321234
"@NotNull", "@Valid", "@Pattern(regexp=\"^[a-zA-Z0-9]+[a-zA-Z0-9\\\\.\\\\-_]*[a-zA-Z0-9]+$\")");
12331235
}
12341236

1237+
@DataProvider
1238+
public Object[][] issue17997DocumentationProviders() {
1239+
return new Object[][]{
1240+
{DocumentationProviderFeatures.DocumentationProvider.SPRINGDOC.name(),
1241+
(Consumer<Path>) outputPath ->
1242+
assertFileContains(
1243+
outputPath,
1244+
"allowableValues = [\"0\", \"1\"], defaultValue = \"0\"",
1245+
"@PathVariable"
1246+
),
1247+
(Consumer<Path>) outputPath ->
1248+
assertFileContains(
1249+
outputPath,
1250+
"allowableValues = [\"sleeping\", \"awake\"]", "@PathVariable",
1251+
"@PathVariable"
1252+
)
1253+
},
1254+
{DocumentationProviderFeatures.DocumentationProvider.SPRINGFOX.name(),
1255+
(Consumer<Path>) outputPath ->
1256+
assertFileContains(
1257+
outputPath,
1258+
"allowableValues = \"0, 1\", defaultValue = \"0\"",
1259+
"@PathVariable"
1260+
),
1261+
(Consumer<Path>) outputPath ->
1262+
assertFileContains(
1263+
outputPath,
1264+
"allowableValues = \"sleeping, awake\"", "@PathVariable",
1265+
"@PathVariable"
1266+
)
1267+
}
1268+
};
1269+
}
1270+
1271+
@Test(dataProvider = "issue17997DocumentationProviders")
1272+
public void testDocumentationAnnotationInPathParams_Issue17997(
1273+
String documentProvider,
1274+
Consumer<Path> intEnumAssertFunction,
1275+
Consumer<Path> stringEnumAssertFunction
1276+
) throws IOException {
1277+
Map<String, Object> additionalProperties = new HashMap<>();
1278+
additionalProperties.put(DOCUMENTATION_PROVIDER, documentProvider);
1279+
1280+
Map<String, String> generatorPropertyDefaults = new HashMap<>();
1281+
generatorPropertyDefaults.put(CodegenConstants.MODEL_TESTS, "false");
1282+
generatorPropertyDefaults.put(CodegenConstants.MODEL_DOCS, "false");
1283+
generatorPropertyDefaults.put(CodegenConstants.APIS, "true");
1284+
1285+
Map<String, File> files = generateFromContract(
1286+
"src/test/resources/3_0/issue_6762.yaml",
1287+
additionalProperties,
1288+
generatorPropertyDefaults
1289+
);
1290+
1291+
Stream.of(
1292+
"ZebrasApiController.kt",
1293+
"GiraffesApiController.kt"
1294+
).forEach(filename -> {
1295+
File file = files.get(filename);
1296+
assertThat(file).isNotNull();
1297+
intEnumAssertFunction.accept(file.toPath());
1298+
});
1299+
1300+
Stream.of(
1301+
"BearsApiController.kt",
1302+
"CamelsApiController.kt"
1303+
).forEach(filename -> {
1304+
File file = files.get(filename);
1305+
assertThat(file).isNotNull();
1306+
stringEnumAssertFunction.accept(file.toPath());
1307+
});
1308+
}
1309+
12351310
private Map<String, File> generateFromContract(String url) throws IOException {
12361311
return generateFromContract(url, new HashMap<>(), new HashMap<>());
12371312
}

modules/openapi-generator/src/test/resources/3_0/issue_6762.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ paths:
1313
operationId: getGiraffes
1414
parameters:
1515
- $ref: '#/components/parameters/refStatus'
16+
responses:
17+
"204":
18+
description: Successful response
1619
/zebras/{status}:
1720
get:
1821
operationId: getZebras
@@ -24,11 +27,17 @@ paths:
2427
type: integer
2528
enum: [0,1]
2629
default: 0
30+
responses:
31+
"204":
32+
description: Successful response
2733
/bears/{refCondition}:
2834
get:
2935
operationId: getBears
3036
parameters:
3137
- $ref: '#/components/parameters/refCondition'
38+
responses:
39+
"204":
40+
description: Successful response
3241
/camels/{condition}:
3342
get:
3443
operationId: getCamels
@@ -41,6 +50,9 @@ paths:
4150
enum:
4251
- sleeping
4352
- awake
53+
responses:
54+
"204":
55+
description: Successful response
4456
components:
4557
parameters:
4658
refStatus:
@@ -58,4 +70,4 @@ components:
5870
type: string
5971
enum:
6072
- sleeping
61-
- awake
73+
- awake

0 commit comments

Comments
 (0)