diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/enum_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/enum_class.mustache index 2f24a1de76b..6203806394d 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/enum_class.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/enum_class.mustache @@ -4,6 +4,11 @@ */ enum class {{classname}}(val value: {{dataType}}) { {{#allowableValues}}{{#enumVars}} + {{#enumDescription}} + /** + * {{.}} + */ + {{/enumDescription}} {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/enumVars}}{{/allowableValues}} } diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/enum_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/enum_class.mustache index 005b9daa596..8cd1d96e8d3 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/enum_class.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/enum_class.mustache @@ -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}}) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinServerCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinServerCodegenTest.java index 42f4777d7dc..939d840118e 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinServerCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinServerCodegenTest.java @@ -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(); diff --git a/modules/openapi-generator/src/test/resources/3_0/enum-description.yaml b/modules/openapi-generator/src/test/resources/3_0/enum-description.yaml new file mode 100644 index 00000000000..8f69c9898c1 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/enum-description.yaml @@ -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