mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-10-14 00:13:50 +00:00
[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 <cgual@omnidian.com>
This commit is contained in:
parent
bb6acc132a
commit
7e974272be
@ -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}}
|
{{#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}}
|
@ -7,6 +7,7 @@ import io.swagger.v3.oas.models.servers.Server;
|
|||||||
import io.swagger.v3.parser.core.models.ParseOptions;
|
import io.swagger.v3.parser.core.models.ParseOptions;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.stream.Stream;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.assertj.core.api.Assertions;
|
import org.assertj.core.api.Assertions;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@ -15,13 +16,14 @@ import org.openapitools.codegen.CodegenConstants;
|
|||||||
import org.openapitools.codegen.DefaultGenerator;
|
import org.openapitools.codegen.DefaultGenerator;
|
||||||
import org.openapitools.codegen.TestUtils;
|
import org.openapitools.codegen.TestUtils;
|
||||||
import org.openapitools.codegen.config.CodegenConfigurator;
|
import org.openapitools.codegen.config.CodegenConfigurator;
|
||||||
import org.openapitools.codegen.java.assertions.JavaFileAssert;
|
|
||||||
import org.openapitools.codegen.kotlin.KotlinTestUtils;
|
import org.openapitools.codegen.kotlin.KotlinTestUtils;
|
||||||
import org.openapitools.codegen.languages.KotlinSpringServerCodegen;
|
import org.openapitools.codegen.languages.KotlinSpringServerCodegen;
|
||||||
import org.openapitools.codegen.languages.features.CXFServerFeatures;
|
import org.openapitools.codegen.languages.features.CXFServerFeatures;
|
||||||
|
import org.openapitools.codegen.languages.features.DocumentationProviderFeatures;
|
||||||
import org.openapitools.codegen.languages.features.DocumentationProviderFeatures.AnnotationLibrary;
|
import org.openapitools.codegen.languages.features.DocumentationProviderFeatures.AnnotationLibrary;
|
||||||
import org.openapitools.codegen.languages.features.DocumentationProviderFeatures.DocumentationProvider;
|
import org.openapitools.codegen.languages.features.DocumentationProviderFeatures.DocumentationProvider;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
|
import org.testng.annotations.DataProvider;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -1232,6 +1234,79 @@ public class KotlinSpringServerCodegenTest {
|
|||||||
"@NotNull", "@Valid", "@Pattern(regexp=\"^[a-zA-Z0-9]+[a-zA-Z0-9\\\\.\\\\-_]*[a-zA-Z0-9]+$\")");
|
"@NotNull", "@Valid", "@Pattern(regexp=\"^[a-zA-Z0-9]+[a-zA-Z0-9\\\\.\\\\-_]*[a-zA-Z0-9]+$\")");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DataProvider
|
||||||
|
public Object[][] issue17997DocumentationProviders() {
|
||||||
|
return new Object[][]{
|
||||||
|
{DocumentationProviderFeatures.DocumentationProvider.SPRINGDOC.name(),
|
||||||
|
(Consumer<Path>) outputPath ->
|
||||||
|
assertFileContains(
|
||||||
|
outputPath,
|
||||||
|
"allowableValues = [\"0\", \"1\"], defaultValue = \"0\"",
|
||||||
|
"@PathVariable"
|
||||||
|
),
|
||||||
|
(Consumer<Path>) outputPath ->
|
||||||
|
assertFileContains(
|
||||||
|
outputPath,
|
||||||
|
"allowableValues = [\"sleeping\", \"awake\"]", "@PathVariable",
|
||||||
|
"@PathVariable"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
{DocumentationProviderFeatures.DocumentationProvider.SPRINGFOX.name(),
|
||||||
|
(Consumer<Path>) outputPath ->
|
||||||
|
assertFileContains(
|
||||||
|
outputPath,
|
||||||
|
"allowableValues = \"0, 1\", defaultValue = \"0\"",
|
||||||
|
"@PathVariable"
|
||||||
|
),
|
||||||
|
(Consumer<Path>) outputPath ->
|
||||||
|
assertFileContains(
|
||||||
|
outputPath,
|
||||||
|
"allowableValues = \"sleeping, awake\"", "@PathVariable",
|
||||||
|
"@PathVariable"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(dataProvider = "issue17997DocumentationProviders")
|
||||||
|
public void testDocumentationAnnotationInPathParams_Issue17997(
|
||||||
|
String documentProvider,
|
||||||
|
Consumer<Path> intEnumAssertFunction,
|
||||||
|
Consumer<Path> stringEnumAssertFunction
|
||||||
|
) throws IOException {
|
||||||
|
Map<String, Object> additionalProperties = new HashMap<>();
|
||||||
|
additionalProperties.put(DOCUMENTATION_PROVIDER, documentProvider);
|
||||||
|
|
||||||
|
Map<String, String> generatorPropertyDefaults = new HashMap<>();
|
||||||
|
generatorPropertyDefaults.put(CodegenConstants.MODEL_TESTS, "false");
|
||||||
|
generatorPropertyDefaults.put(CodegenConstants.MODEL_DOCS, "false");
|
||||||
|
generatorPropertyDefaults.put(CodegenConstants.APIS, "true");
|
||||||
|
|
||||||
|
Map<String, File> files = generateFromContract(
|
||||||
|
"src/test/resources/3_0/issue_6762.yaml",
|
||||||
|
additionalProperties,
|
||||||
|
generatorPropertyDefaults
|
||||||
|
);
|
||||||
|
|
||||||
|
Stream.of(
|
||||||
|
"ZebrasApiController.kt",
|
||||||
|
"GiraffesApiController.kt"
|
||||||
|
).forEach(filename -> {
|
||||||
|
File file = files.get(filename);
|
||||||
|
assertThat(file).isNotNull();
|
||||||
|
intEnumAssertFunction.accept(file.toPath());
|
||||||
|
});
|
||||||
|
|
||||||
|
Stream.of(
|
||||||
|
"BearsApiController.kt",
|
||||||
|
"CamelsApiController.kt"
|
||||||
|
).forEach(filename -> {
|
||||||
|
File file = files.get(filename);
|
||||||
|
assertThat(file).isNotNull();
|
||||||
|
stringEnumAssertFunction.accept(file.toPath());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private Map<String, File> generateFromContract(String url) throws IOException {
|
private Map<String, File> generateFromContract(String url) throws IOException {
|
||||||
return generateFromContract(url, new HashMap<>(), new HashMap<>());
|
return generateFromContract(url, new HashMap<>(), new HashMap<>());
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,9 @@ paths:
|
|||||||
operationId: getGiraffes
|
operationId: getGiraffes
|
||||||
parameters:
|
parameters:
|
||||||
- $ref: '#/components/parameters/refStatus'
|
- $ref: '#/components/parameters/refStatus'
|
||||||
|
responses:
|
||||||
|
"204":
|
||||||
|
description: Successful response
|
||||||
/zebras/{status}:
|
/zebras/{status}:
|
||||||
get:
|
get:
|
||||||
operationId: getZebras
|
operationId: getZebras
|
||||||
@ -24,11 +27,17 @@ paths:
|
|||||||
type: integer
|
type: integer
|
||||||
enum: [0,1]
|
enum: [0,1]
|
||||||
default: 0
|
default: 0
|
||||||
|
responses:
|
||||||
|
"204":
|
||||||
|
description: Successful response
|
||||||
/bears/{refCondition}:
|
/bears/{refCondition}:
|
||||||
get:
|
get:
|
||||||
operationId: getBears
|
operationId: getBears
|
||||||
parameters:
|
parameters:
|
||||||
- $ref: '#/components/parameters/refCondition'
|
- $ref: '#/components/parameters/refCondition'
|
||||||
|
responses:
|
||||||
|
"204":
|
||||||
|
description: Successful response
|
||||||
/camels/{condition}:
|
/camels/{condition}:
|
||||||
get:
|
get:
|
||||||
operationId: getCamels
|
operationId: getCamels
|
||||||
@ -41,6 +50,9 @@ paths:
|
|||||||
enum:
|
enum:
|
||||||
- sleeping
|
- sleeping
|
||||||
- awake
|
- awake
|
||||||
|
responses:
|
||||||
|
"204":
|
||||||
|
description: Successful response
|
||||||
components:
|
components:
|
||||||
parameters:
|
parameters:
|
||||||
refStatus:
|
refStatus:
|
||||||
@ -58,4 +70,4 @@ components:
|
|||||||
type: string
|
type: string
|
||||||
enum:
|
enum:
|
||||||
- sleeping
|
- sleeping
|
||||||
- awake
|
- awake
|
Loading…
x
Reference in New Issue
Block a user