feat: x-enum-description support added to kotlin-server code generator (#19041)

This commit is contained in:
Dieter Eickstaedt 2024-07-01 14:16:26 +02:00 committed by GitHub
parent 21d3cfe924
commit c9da04cb65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 79 additions and 0 deletions

View File

@ -4,6 +4,11 @@
*/
enum class {{classname}}(val value: {{dataType}}) {
{{#allowableValues}}{{#enumVars}}
{{#enumDescription}}
/**
* {{.}}
*/
{{/enumDescription}}
{{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}{{/allowableValues}}
}

View File

@ -13,6 +13,12 @@ import kotlinx.serialization.Serializable
{{>additionalModelTypeAnnotations}}
{{#nonPublicApi}}internal {{/nonPublicApi}}enum class {{classname}}(val value: {{{dataType}}}) {
{{#allowableValues}}{{#enumVars}}
{{#enumDescription}}
/**
* {{.}}
*/
{{/enumDescription}}
@JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}})
{{#kotlinx_serialization}}
@SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}})

View File

@ -23,6 +23,38 @@ import static org.openapitools.codegen.languages.features.BeanValidationFeatures
public class KotlinServerCodegenTest {
@Test
public void enumDescription() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
KotlinServerCodegen codegen = new KotlinServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(LIBRARY, JAXRS_SPEC);
new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/enum-description.yaml"))
.config(codegen))
.generate();
String outputPath = output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/server";
Path petApi = Paths.get(outputPath + "/models/Type.kt");
assertFileNotContains(
petApi,
"import jakarta.ws.rs.*",
"import jakarta.ws.rs.core.Response",
"@jakarta.annotation.Generated(value = arrayOf(\"org.openapitools.codegen.languages.KotlinServerCodegen\")"
);
// assert, that all enum values have a description comment
assertFileContains(
petApi,
"Pegasi b is a gas giant exoplanet that orbits a G-type star",
"Mercury is the first planet from the Sun and the smallest in the Solar System",
"The planet we all live on"
);
}
@Test
public void javaxImports() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();

View File

@ -0,0 +1,36 @@
openapi: 3.0.0
info:
title: Sample API
description: API description in Markdown.
version: 1.0.0
paths:
/ponies:
get:
summary: Returns all animals.
description: Optional extended description in Markdown.
responses:
200:
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pony'
components:
schemas:
Pony:
type: object
properties:
type:
$ref: '#/components/schemas/Type'
Type:
type: string
enum:
- Earth
- Pegasi
- Mercury
x-enum-descriptions:
- The planet we all live on
- Pegasi b is a gas giant exoplanet that orbits a G-type star
- Mercury is the first planet from the Sun and the smallest in the Solar System