Skip to content

Commit 217dbbf

Browse files
authored
[kotlin] add additionalEnumTypeAnnotations option (#24987)
* [kotlin] add additionalEnumTypeAnnotations option AbstractKotlinCodegen only implemented additionalModelTypeAnnotations, and the kotlin-server jaxrs-spec enum_class template included that partial, so an annotation that is only valid on classes was also emitted on generated enums and the output did not compile. Add additionalEnumTypeAnnotations to AbstractKotlinCodegen, mirroring AbstractJavaCodegen, and let the jaxrs-spec enum template use it instead. Fixes #24978 * update ktorm-schema generator doc
1 parent 8932249 commit 217dbbf

12 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎docs/generators/kotlin-misk.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2525
|actionRequestContentType|Request ContentType for Action| |@RequestContentType|
2626
|actionRequestContentTypePrefix|Request ContentType Prefix for Action| |MediaTypes|
2727
|addModelMoshiJsonAnnotation|Add a Moshi JSON adapter annotation to all model classes| |true|
28+
|additionalEnumTypeAnnotations|Additional annotations for enum type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2829
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2930
|artifactId|Generated artifact id (name of jar).| |null|
3031
|artifactVersion|Generated artifact's package version.| |1.0.0|

‎docs/generators/kotlin-server.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
1818

1919
| Option | Description | Values | Default |
2020
| ------ | ----------- | ------ | ------- |
21+
|additionalEnumTypeAnnotations|Additional annotations for enum type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2122
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2223
|artifactId|Generated artifact id (name of jar).| |kotlin-server|
2324
|artifactVersion|Generated artifact's package version.| |1.0.0|

‎docs/generators/kotlin-spring.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
1818

1919
| Option | Description | Values | Default |
2020
| ------ | ----------- | ------ | ------- |
21+
|additionalEnumTypeAnnotations|Additional annotations for enum type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2122
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2223
|annotationLibrary|Select the complementary documentation annotation library.|<dl><dt>**none**</dt><dd>Do not annotate Model and Api with complementary annotations.</dd><dt>**swagger1**</dt><dd>Annotate Model and Api using the Swagger Annotations 1.x library.</dd><dt>**swagger2**</dt><dd>Annotate Model and Api using the Swagger Annotations 2.x library.</dd></dl>|swagger2|
2324
|apiPackage|api package for generated code| |org.openapitools.api|

‎docs/generators/kotlin-vertx.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
1818

1919
| Option | Description | Values | Default |
2020
| ------ | ----------- | ------ | ------- |
21+
|additionalEnumTypeAnnotations|Additional annotations for enum type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2122
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2223
|artifactId|Generated artifact id (name of jar).| |null|
2324
|artifactVersion|Generated artifact's package version.| |1.0.0|

‎docs/generators/kotlin-wiremock.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
1818

1919
| Option | Description | Values | Default |
2020
| ------ | ----------- | ------ | ------- |
21+
|additionalEnumTypeAnnotations|Additional annotations for enum type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2122
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2223
|artifactId|Generated artifact id (name of jar).| |null|
2324
|artifactVersion|Generated artifact's package version.| |1.0.0|

‎docs/generators/kotlin.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
1818

1919
| Option | Description | Values | Default |
2020
| ------ | ----------- | ------ | ------- |
21+
|additionalEnumTypeAnnotations|Additional annotations for enum type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2122
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2223
|artifactId|Generated artifact id (name of jar).| |kotlin-client|
2324
|artifactVersion|Generated artifact's package version.| |1.0.0|

‎docs/generators/ktorm-schema.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
1919
| Option | Description | Values | Default |
2020
| ------ | ----------- | ------ | ------- |
2121
|addSurrogateKey|Adds the surrogate key for all models that don't already have a primary key (named by the above convention)| |false|
22+
|additionalEnumTypeAnnotations|Additional annotations for enum type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2223
|additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
2324
|artifactId|Generated artifact id (name of jar).| |ktorm|
2425
|artifactVersion|Generated artifact's package version.| |1.0.0|

‎modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
5454
public static final String MODEL_MUTABLE = "modelMutable";
5555
public static final String MODEL_MUTABLE_DESC = "Create mutable models";
5656
public static final String ADDITIONAL_MODEL_TYPE_ANNOTATIONS = "additionalModelTypeAnnotations";
57+
public static final String ADDITIONAL_ENUM_TYPE_ANNOTATIONS = "additionalEnumTypeAnnotations";
5758

5859
public static final String JAVAX_PACKAGE = "javaxPackage";
5960
public static final String USE_JAKARTA_EE = "useJakartaEe";
@@ -126,6 +127,8 @@ public enum KotlinEnumNamingType {
126127
private final Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
127128
@Getter @Setter
128129
protected List<String> additionalModelTypeAnnotations = new LinkedList<>();
130+
@Getter @Setter
131+
protected List<String> additionalEnumTypeAnnotations = new LinkedList<>();
129132
@Getter
130133
@Setter
131134
protected Map<String, List<String>> schemaImplements = new HashMap<>();
@@ -326,6 +329,7 @@ public AbstractKotlinCodegen() {
326329

327330
cliOptions.add(CliOption.newBoolean(MODEL_MUTABLE, MODEL_MUTABLE_DESC, false));
328331
cliOptions.add(CliOption.newString(ADDITIONAL_MODEL_TYPE_ANNOTATIONS, "Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)"));
332+
cliOptions.add(CliOption.newString(ADDITIONAL_ENUM_TYPE_ANNOTATIONS, "Additional annotations for enum type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)"));
329333
cliOptions.add(CliOption.newBoolean(IMPLICIT_HEADERS, "Skip header parameters in the generated API methods.", implicitHeaders));
330334
}
331335

@@ -487,6 +491,13 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
487491
}
488492
}
489493

494+
if (!additionalEnumTypeAnnotations.isEmpty()) {
495+
for (String modelName : objs.keySet()) {
496+
Map<String, Object> models = (Map<String, Object>) objs.get(modelName);
497+
models.put(ADDITIONAL_ENUM_TYPE_ANNOTATIONS, additionalEnumTypeAnnotations);
498+
}
499+
}
500+
490501
return objs;
491502
}
492503

@@ -640,6 +651,10 @@ public void processOpts() {
640651
String additionalAnnotationsList = additionalProperties.get(ADDITIONAL_MODEL_TYPE_ANNOTATIONS).toString();
641652
this.setAdditionalModelTypeAnnotations(Arrays.asList(SPLIT_ON_SEMICOLON_OR_NEWLINE_REGEX.split(additionalAnnotationsList.trim())));
642653
}
654+
if (additionalProperties.containsKey(ADDITIONAL_ENUM_TYPE_ANNOTATIONS)) {
655+
String additionalAnnotationsList = additionalProperties.get(ADDITIONAL_ENUM_TYPE_ANNOTATIONS).toString();
656+
this.setAdditionalEnumTypeAnnotations(Arrays.asList(SPLIT_ON_SEMICOLON_OR_NEWLINE_REGEX.split(additionalAnnotationsList.trim())));
657+
}
643658
if (additionalProperties.containsKey(SCHEMA_IMPLEMENTS)) {
644659
this.setSchemaImplements(getPropertyAsStringListMap(SCHEMA_IMPLEMENTS));
645660
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{#additionalEnumTypeAnnotations}}{{{.}}}
2+
{{/additionalEnumTypeAnnotations}}

‎modules/openapi-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/enum_class.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlinx.serialization.Serializable
1010
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
1111
*/
1212
{{#kotlinx_serialization}}@Serializable{{/kotlinx_serialization}}
13-
{{>additionalModelTypeAnnotations}}
13+
{{>additionalEnumTypeAnnotations}}
1414

1515
{{#nonPublicApi}}internal {{/nonPublicApi}}enum class {{classname}}(val value: {{{dataType}}}) {
1616
{{#allowableValues}}{{#enumVars}}

0 commit comments

Comments
 (0)