From 5b4e3114bb1eb790f860a7b7e0156c12b5499ef9 Mon Sep 17 00:00:00 2001 From: altro3 Date: Mon, 16 Sep 2024 21:47:22 +0700 Subject: [PATCH] Added support `additionalEnumTypeAnnotations`, `additionalModelTypeAnnotations`, `additionalOneOfTypeAnnotations` properties to generator. Added ability to set `additionalProperties` by gradle / maven plugin --- .../AbstractMicronautJavaCodegen.java | 16 +++++ .../AbstractMicronautKotlinCodegen.java | 46 +++++++++++++ .../generator/JavaMicronautClientCodegen.java | 12 +--- .../KotlinMicronautClientCodegen.java | 12 +--- .../MicronautCodeGeneratorEntryPoint.java | 66 ++++++++++++++++++- .../MicronautCodeGeneratorOptionsBuilder.java | 36 ++++++++++ .../io/micronaut/openapi/generator/Utils.java | 19 ++++++ .../JavaMicronautClientCodegenTest.java | 38 +++++++++++ .../KotlinMicronautClientCodegenTest.java | 38 +++++++++++ .../3_0/oneof-with-discriminator.yml | 6 ++ 10 files changed, 269 insertions(+), 20 deletions(-) diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java index 28803a499f..bf36a0d80d 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java @@ -85,6 +85,7 @@ import static io.micronaut.openapi.generator.Utils.isDateType; import static io.micronaut.openapi.generator.Utils.normalizeExtraAnnotations; import static io.micronaut.openapi.generator.Utils.processGenericAnnotations; +import static io.micronaut.openapi.generator.Utils.readListOfStringsProperty; import static org.openapitools.codegen.CodegenConstants.API_PACKAGE; import static org.openapitools.codegen.CodegenConstants.INVOKER_PACKAGE; import static org.openapitools.codegen.CodegenConstants.MODEL_PACKAGE; @@ -385,6 +386,21 @@ public void setGeneratedAnnotation(boolean generatedAnnotation) { @Override public void processOpts() { + + // need it to add ability to set List in `additionalModelTypeAnnotations` property + if (additionalProperties.containsKey(ADDITIONAL_MODEL_TYPE_ANNOTATIONS)) { + setAdditionalModelTypeAnnotations(readListOfStringsProperty(ADDITIONAL_MODEL_TYPE_ANNOTATIONS, additionalProperties)); + additionalProperties.remove(ADDITIONAL_MODEL_TYPE_ANNOTATIONS); + } + if (additionalProperties.containsKey(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS)) { + setAdditionalOneOfTypeAnnotations(readListOfStringsProperty(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS, additionalProperties)); + additionalProperties.remove(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS); + } + if (additionalProperties.containsKey(ADDITIONAL_ENUM_TYPE_ANNOTATIONS)) { + setAdditionalEnumTypeAnnotations(readListOfStringsProperty(ADDITIONAL_ENUM_TYPE_ANNOTATIONS, additionalProperties)); + additionalProperties.remove(ADDITIONAL_ENUM_TYPE_ANNOTATIONS); + } + super.processOpts(); // Get properties diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java index ec5312da13..e71694a447 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java @@ -64,12 +64,14 @@ import java.io.File; import java.security.SecureRandom; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; @@ -88,6 +90,7 @@ import static io.micronaut.openapi.generator.Utils.isDateType; import static io.micronaut.openapi.generator.Utils.normalizeExtraAnnotations; import static io.micronaut.openapi.generator.Utils.processGenericAnnotations; +import static io.micronaut.openapi.generator.Utils.readListOfStringsProperty; import static org.openapitools.codegen.CodegenConstants.API_PACKAGE; import static org.openapitools.codegen.CodegenConstants.INVOKER_PACKAGE; import static org.openapitools.codegen.CodegenConstants.MODEL_PACKAGE; @@ -132,6 +135,8 @@ public abstract class AbstractMicronautKotlinCodegen parameterMappings = new ArrayList<>(); protected List responseBodyMappings = new ArrayList<>(); protected Map allModels = new HashMap<>(); + protected List additionalOneOfTypeAnnotations = new LinkedList<>(); + protected List additionalEnumTypeAnnotations = new LinkedList<>(); private final Logger log = LoggerFactory.getLogger(getClass()); @@ -280,6 +287,8 @@ protected AbstractMicronautKotlinCodegen() { cliOptions.add(new CliOption(OPT_TITLE, "Client service name").defaultValue(title)); cliOptions.add(new CliOption(OPT_APPLICATION_NAME, "Micronaut application name (Defaults to the " + CodegenConstants.ARTIFACT_ID + " value)").defaultValue(appName)); + cliOptions.add(CliOption.newString(ADDITIONAL_ENUM_TYPE_ANNOTATIONS, "Additional annotations for enum type (class level annotations)")); + cliOptions.add(CliOption.newString(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS, "Additional annotations for oneOf interfaces (class level annotations). List separated by semicolon(;) or new line (Linux or Windows)")); cliOptions.add(CliOption.newBoolean(OPT_USE_PLURAL, "Whether or not to use plural for request body parameter name", plural)); cliOptions.add(CliOption.newBoolean(OPT_FLUX_FOR_ARRAYS, "Whether or not to use Flux instead Mono> for arrays in generated code", fluxForArrays)); cliOptions.add(CliOption.newBoolean(OPT_GENERATED_ANNOTATION, "Generate code with \"@Generated\" annotation", generatedAnnotation)); @@ -457,6 +466,21 @@ public void setKsp(boolean ksp) { @Override public void processOpts() { + + // need it to add ability to set List in `additionalModelTypeAnnotations` property + if (additionalProperties.containsKey(ADDITIONAL_MODEL_TYPE_ANNOTATIONS)) { + setAdditionalModelTypeAnnotations(readListOfStringsProperty(ADDITIONAL_MODEL_TYPE_ANNOTATIONS, additionalProperties)); + additionalProperties.remove(ADDITIONAL_MODEL_TYPE_ANNOTATIONS); + } + if (additionalProperties.containsKey(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS)) { + setAdditionalOneOfTypeAnnotations(readListOfStringsProperty(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS, additionalProperties)); + additionalProperties.remove(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS); + } + if (additionalProperties.containsKey(ADDITIONAL_ENUM_TYPE_ANNOTATIONS)) { + setAdditionalEnumTypeAnnotations(readListOfStringsProperty(ADDITIONAL_ENUM_TYPE_ANNOTATIONS, additionalProperties)); + additionalProperties.remove(ADDITIONAL_ENUM_TYPE_ANNOTATIONS); + } + super.processOpts(); // Get properties @@ -1554,6 +1578,20 @@ public String toVarName(String name) { public Map postProcessAllModels(Map objs) { objs = super.postProcessAllModels(objs); + if (!additionalOneOfTypeAnnotations.isEmpty()) { + for (String modelName : objs.keySet()) { + Map models = objs.get(modelName); + models.put(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS, additionalOneOfTypeAnnotations); + } + } + + if (!additionalEnumTypeAnnotations.isEmpty()) { + for (String modelName : objs.keySet()) { + Map models = objs.get(modelName); + models.put(ADDITIONAL_ENUM_TYPE_ANNOTATIONS, additionalEnumTypeAnnotations); + } + } + var isServer = isServer(); for (ModelsMap models : objs.values()) { @@ -2230,6 +2268,14 @@ public void setUseEnumCaseInsensitive(boolean useEnumCaseInsensitive) { this.useEnumCaseInsensitive = useEnumCaseInsensitive; } + public void setAdditionalOneOfTypeAnnotations(List additionalOneOfTypeAnnotations) { + this.additionalOneOfTypeAnnotations = additionalOneOfTypeAnnotations; + } + + public void setAdditionalEnumTypeAnnotations(List additionalEnumTypeAnnotations) { + this.additionalEnumTypeAnnotations = additionalEnumTypeAnnotations; + } + @Override public void postProcess() { // disable output donation suggestion diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientCodegen.java index e1f913af19..bf0d5539fd 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientCodegen.java @@ -26,13 +26,13 @@ import org.openapitools.codegen.model.OperationMap; import org.openapitools.codegen.model.OperationsMap; -import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Map; import static io.micronaut.openapi.generator.Utils.processMultipartBody; +import static io.micronaut.openapi.generator.Utils.readListOfStringsProperty; /** * The generator for creating Micronaut clients. @@ -182,14 +182,8 @@ public void processOpts() { writePropertyBack(AUTHORIZATION_FILTER_PATTERN, this.authorizationFilterPattern); } - Object additionalClientAnnotations = additionalProperties.get(ADDITIONAL_CLIENT_TYPE_ANNOTATIONS); - if (additionalClientAnnotations != null) { - if (additionalClientAnnotations instanceof @SuppressWarnings("rawtypes") List additionalClientAnnotationsAsList) { - //noinspection unchecked - additionalClientTypeAnnotations = additionalClientAnnotationsAsList; - } else { - additionalClientTypeAnnotations = Arrays.asList(additionalClientAnnotations.toString().trim().split("\\s*(;|\\r?\\n)\\s*")); - } + if (additionalProperties.containsKey(ADDITIONAL_CLIENT_TYPE_ANNOTATIONS)) { + setAdditionalClientTypeAnnotations(readListOfStringsProperty(ADDITIONAL_CLIENT_TYPE_ANNOTATIONS, additionalProperties)); } writePropertyBack(ADDITIONAL_CLIENT_TYPE_ANNOTATIONS, additionalClientTypeAnnotations); diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegen.java index d1b74df91c..b46cbe24b3 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegen.java @@ -26,13 +26,13 @@ import org.openapitools.codegen.model.OperationMap; import org.openapitools.codegen.model.OperationsMap; -import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Map; import static io.micronaut.openapi.generator.Utils.processMultipartBody; +import static io.micronaut.openapi.generator.Utils.readListOfStringsProperty; /** * The generator for creating Micronaut clients. @@ -180,14 +180,8 @@ public void processOpts() { writePropertyBack(AUTHORIZATION_FILTER_PATTERN, this.authorizationFilterPattern); } - Object additionalClientAnnotations = additionalProperties.get(ADDITIONAL_CLIENT_TYPE_ANNOTATIONS); - if (additionalClientAnnotations != null) { - if (additionalClientAnnotations instanceof @SuppressWarnings("rawtypes") List additionalClientAnnotationsAsList) { - //noinspection unchecked - additionalClientTypeAnnotations = additionalClientAnnotationsAsList; - } else { - additionalClientTypeAnnotations = Arrays.asList(additionalClientAnnotations.toString().trim().split("\\s*(;|\\r?\\n)\\s*")); - } + if (additionalProperties.containsKey(ADDITIONAL_CLIENT_TYPE_ANNOTATIONS)) { + setAdditionalClientTypeAnnotations(readListOfStringsProperty(ADDITIONAL_CLIENT_TYPE_ANNOTATIONS, additionalProperties)); } writePropertyBack(ADDITIONAL_CLIENT_TYPE_ANNOTATIONS, additionalClientTypeAnnotations); diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java index a1f3113fd3..20189cf73a 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java @@ -123,12 +123,16 @@ private void configureOptions() { if (options == null) { return; } + if (options.additionalProperties != null && !options.additionalProperties.isEmpty()) { + codeGenerator.additionalProperties().putAll(options.additionalProperties); + } if (options.modelPackage != null) { codeGenerator.setModelPackage(options.modelPackage); } if (options.apiPackage != null) { codeGenerator.setApiPackage(options.apiPackage); } + if ((options.lang == null || options.lang == GeneratorLanguage.JAVA) && codeGenerator instanceof AbstractMicronautJavaCodegen javaCodeGen) { if (options.invokerPackage != null) { @@ -187,6 +191,16 @@ private void configureOptions() { javaCodeGen.setImplicitHeadersRegex(options.implicitHeadersRegex); } + if (options.additionalEnumTypeAnnotations != null && !options.additionalEnumTypeAnnotations.isEmpty()) { + javaCodeGen.setAdditionalEnumTypeAnnotations(options.additionalEnumTypeAnnotations); + } + if (options.additionalModelTypeAnnotations != null && !options.additionalModelTypeAnnotations.isEmpty()) { + javaCodeGen.setAdditionalModelTypeAnnotations(options.additionalModelTypeAnnotations); + } + if (options.additionalOneOfTypeAnnotations != null && !options.additionalOneOfTypeAnnotations.isEmpty()) { + javaCodeGen.setAdditionalOneOfTypeAnnotations(options.additionalOneOfTypeAnnotations); + } + javaCodeGen.setUseOneOfInterfaces(options.useOneOfInterfaces); javaCodeGen.setReactive(options.reactive); javaCodeGen.setGenerateHttpResponseAlways(options.generateHttpResponseAlways); @@ -257,6 +271,17 @@ private void configureOptions() { if (options.implicitHeadersRegex != null && !options.implicitHeadersRegex.isBlank()) { kotlinCodeGen.setImplicitHeadersRegex(options.implicitHeadersRegex); } + + if (options.additionalEnumTypeAnnotations != null && !options.additionalEnumTypeAnnotations.isEmpty()) { + kotlinCodeGen.setAdditionalEnumTypeAnnotations(options.additionalEnumTypeAnnotations); + } + if (options.additionalModelTypeAnnotations != null && !options.additionalModelTypeAnnotations.isEmpty()) { + kotlinCodeGen.setAdditionalModelTypeAnnotations(options.additionalModelTypeAnnotations); + } + if (options.additionalOneOfTypeAnnotations != null && !options.additionalOneOfTypeAnnotations.isEmpty()) { + kotlinCodeGen.setAdditionalOneOfTypeAnnotations(options.additionalOneOfTypeAnnotations); + } + kotlinCodeGen.setUseOneOfInterfaces(options.useOneOfInterfaces); kotlinCodeGen.setReactive(options.reactive); kotlinCodeGen.setGenerateHttpResponseAlways(options.generateHttpResponseAlways); @@ -539,6 +564,10 @@ private static class DefaultOptionsBuilder implements MicronautCodeGeneratorOpti private SerializationLibraryKind serializationLibraryKind = SerializationLibraryKind.MICRONAUT_SERDE_JACKSON; private DateTimeFormat dateTimeFormat = DateTimeFormat.ZONED_DATETIME; private GeneratorLanguage lang = GeneratorLanguage.JAVA; + private List additionalEnumTypeAnnotations; + private List additionalModelTypeAnnotations; + private List additionalOneOfTypeAnnotations; + private Map additionalProperties; @Override public MicronautCodeGeneratorOptionsBuilder withLang(GeneratorLanguage lang) { @@ -738,6 +767,30 @@ public MicronautCodeGeneratorOptionsBuilder withUseOneOfInterfaces(boolean useOn return this; } + @Override + public MicronautCodeGeneratorOptionsBuilder withAdditionalEnumTypeAnnotations(List additionalEnumTypeAnnotations) { + this.additionalEnumTypeAnnotations = additionalEnumTypeAnnotations; + return this; + } + + @Override + public MicronautCodeGeneratorOptionsBuilder withAdditionalModelTypeAnnotations(List additionalModelTypeAnnotations) { + this.additionalModelTypeAnnotations = additionalModelTypeAnnotations; + return this; + } + + @Override + public MicronautCodeGeneratorOptionsBuilder withAdditionalOneOfTypeAnnotations(List additionalOneOfTypeAnnotations) { + this.additionalOneOfTypeAnnotations = additionalOneOfTypeAnnotations; + return this; + } + + @Override + public MicronautCodeGeneratorOptionsBuilder withAdditionalProperties(Map additionalProperties) { + this.additionalProperties = additionalProperties; + return this; + } + private Options build() { return new Options( lang, @@ -775,7 +828,12 @@ private Options build() { generateSwaggerAnnotations, testFramework, serializationLibraryKind, - dateTimeFormat); + dateTimeFormat, + additionalEnumTypeAnnotations, + additionalModelTypeAnnotations, + additionalOneOfTypeAnnotations, + additionalProperties + ); } } } @@ -832,7 +890,11 @@ private record Options( boolean generateSwaggerAnnotations, TestFramework testFramework, SerializationLibraryKind serializationLibraryKind, - MicronautCodeGeneratorOptionsBuilder.DateTimeFormat dateTimeFormat + MicronautCodeGeneratorOptionsBuilder.DateTimeFormat dateTimeFormat, + List additionalEnumTypeAnnotations, + List additionalModelTypeAnnotations, + List additionalOneOfTypeAnnotations, + Map additionalProperties ) { } diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java index 2532a7a287..c21083a608 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java @@ -289,6 +289,42 @@ public interface MicronautCodeGeneratorOptionsBuilder { */ MicronautCodeGeneratorOptionsBuilder withUseOneOfInterfaces(boolean useOneOfInterfaces); + /** + * Additional annotations for enum type (class level annotations). + * + * @param additionalEnumTypeAnnotations additional annotations for enum type (class level annotations). + * + * @return this builder + */ + MicronautCodeGeneratorOptionsBuilder withAdditionalEnumTypeAnnotations(List additionalEnumTypeAnnotations); + + /** + * Additional annotations for model type (class level annotations). + * + * @param additionalModelTypeAnnotations additional annotations for model type (class level annotations). + * + * @return this builder + */ + MicronautCodeGeneratorOptionsBuilder withAdditionalModelTypeAnnotations(List additionalModelTypeAnnotations); + + /** + * Additional annotations for oneOf interfaces (class level annotations). + * + * @param additionalOneOfTypeAnnotations additional annotations for oneOf interfaces (class level annotations). + * + * @return this builder + */ + MicronautCodeGeneratorOptionsBuilder withAdditionalOneOfTypeAnnotations(List additionalOneOfTypeAnnotations); + + /** + * Additional generator properties. + * + * @param additionalProperties additional generator properties. + * + * @return this builder + */ + MicronautCodeGeneratorOptionsBuilder withAdditionalProperties(Map additionalProperties); + /** * The possible date-time formatting configurations. */ diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/Utils.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/Utils.java index 30ae1a38da..9e961dfbc6 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/Utils.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/Utils.java @@ -25,7 +25,9 @@ import org.openapitools.codegen.CodegenProperty; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -367,4 +369,21 @@ private static String normalizeExtraAnnotation(String prefix, String annotationS public static boolean isDateType(String type) { return DATE_TIME_TYPES.contains(type); } + + public static List readListOfStringsProperty(String property, Map additionalProperties) { + var additionalAnnotations = additionalProperties.get(property); + if (additionalAnnotations == null) { + return Collections.emptyList(); + } + List additionalOneOfTypeAnnotationsList; + if (additionalAnnotations instanceof Collection additionalAnnotationsCol) { + additionalOneOfTypeAnnotationsList = new ArrayList<>(additionalAnnotationsCol.size()); + for (var el : additionalAnnotationsCol) { + additionalOneOfTypeAnnotationsList.add(el.toString()); + } + } else { + additionalOneOfTypeAnnotationsList = Arrays.asList(additionalAnnotations.toString().trim().split("\\s*(;|\\r?\\n)\\s*")); + } + return additionalOneOfTypeAnnotationsList; + } } diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java index 60e8da69a5..00ee64e869 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java @@ -8,6 +8,7 @@ import org.openapitools.codegen.CodegenConstants; import java.util.List; +import java.util.Map; import static java.util.stream.Collectors.groupingBy; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -816,4 +817,41 @@ public static StringEnum fromValue(String value) { } """); } + + @Test + void testAdditionalAnnotations() { + + var codegen = new JavaMicronautClientCodegen(); + codegen.setAdditionalClientTypeAnnotations(List.of("@java.io.MyAnnotation1")); + codegen.setAdditionalModelTypeAnnotations(List.of("@java.io.MyAnnotation2")); + codegen.setAdditionalOneOfTypeAnnotations(List.of("@java.io.MyAnnotation3")); + codegen.setAdditionalEnumTypeAnnotations(List.of("@java.io.MyAnnotation4")); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/oneof-with-discriminator.yml", CodegenConstants.APIS, CodegenConstants.MODELS); + String path = outputPath + "src/main/java/org/openapitools/"; + + assertFileContains(path + "api/SubjectsApi.java", "@java.io.MyAnnotation1"); + assertFileContains(path + "model/Person.java", "@java.io.MyAnnotation2"); + assertFileContains(path + "model/Subject.java", "@java.io.MyAnnotation3"); + assertFileContains(path + "model/PersonSex.java", "@java.io.MyAnnotation4"); + } + + @Test + void testAdditionalAnnotations2() { + + var codegen = new JavaMicronautClientCodegen(); + codegen.additionalProperties().putAll(Map.of( + "additionalClientTypeAnnotations", List.of("@java.io.MyAnnotation1"), + "additionalModelTypeAnnotations", List.of("@java.io.MyAnnotation2"), + "additionalOneOfTypeAnnotations", List.of("@java.io.MyAnnotation3"), + "additionalEnumTypeAnnotations", "@java.io.MyAnnotation41;@java.io.MyAnnotation42;\n@java.io.MyAnnotation43;" + )); + codegen.processOpts(); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/oneof-with-discriminator.yml", CodegenConstants.APIS, CodegenConstants.MODELS); + String path = outputPath + "src/main/java/org/openapitools/"; + + assertFileContains(path + "api/SubjectsApi.java", "@java.io.MyAnnotation1"); + assertFileContains(path + "model/Person.java", "@java.io.MyAnnotation2"); + assertFileContains(path + "model/Subject.java", "@java.io.MyAnnotation3"); + assertFileContains(path + "model/PersonSex.java", "@java.io.MyAnnotation41\n", "@java.io.MyAnnotation42\n", "@java.io.MyAnnotation43\n"); + } } diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java index 347c6d1a7c..9623743c0f 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java @@ -8,6 +8,7 @@ import org.openapitools.codegen.CodegenConstants; import java.util.List; +import java.util.Map; import static java.util.stream.Collectors.groupingBy; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -836,4 +837,41 @@ fun fromValue(value: String): StringEnum { } """); } + + @Test + void testAdditionalAnnotations() { + + var codegen = new KotlinMicronautClientCodegen(); + codegen.setAdditionalClientTypeAnnotations(List.of("@java.io.MyAnnotation1")); + codegen.setAdditionalModelTypeAnnotations(List.of("@java.io.MyAnnotation2")); + codegen.setAdditionalOneOfTypeAnnotations(List.of("@java.io.MyAnnotation3")); + codegen.setAdditionalEnumTypeAnnotations(List.of("@java.io.MyAnnotation4")); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/oneof-with-discriminator.yml", CodegenConstants.APIS, CodegenConstants.MODELS); + String path = outputPath + "src/main/kotlin/org/openapitools/"; + + assertFileContains(path + "api/SubjectsApi.kt", "@java.io.MyAnnotation1"); + assertFileContains(path + "model/Person.kt", "@java.io.MyAnnotation2"); + assertFileContains(path + "model/Subject.kt", "@java.io.MyAnnotation3"); + assertFileContains(path + "model/PersonSex.kt", "@java.io.MyAnnotation4"); + } + + @Test + void testAdditionalAnnotations2() { + + var codegen = new KotlinMicronautClientCodegen(); + codegen.additionalProperties().putAll(Map.of( + "additionalClientTypeAnnotations", List.of("@java.io.MyAnnotation1"), + "additionalModelTypeAnnotations", List.of("@java.io.MyAnnotation2"), + "additionalOneOfTypeAnnotations", List.of("@java.io.MyAnnotation3"), + "additionalEnumTypeAnnotations", "@java.io.MyAnnotation41;@java.io.MyAnnotation42;\n@java.io.MyAnnotation43;" + )); + codegen.processOpts(); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/oneof-with-discriminator.yml", CodegenConstants.APIS, CodegenConstants.MODELS); + String path = outputPath + "src/main/kotlin/org/openapitools/"; + + assertFileContains(path + "api/SubjectsApi.kt", "@java.io.MyAnnotation1"); + assertFileContains(path + "model/Person.kt", "@java.io.MyAnnotation2"); + assertFileContains(path + "model/Subject.kt", "@java.io.MyAnnotation3"); + assertFileContains(path + "model/PersonSex.kt", "@java.io.MyAnnotation41\n", "@java.io.MyAnnotation42\n", "@java.io.MyAnnotation43\n"); + } } diff --git a/openapi-generator/src/test/resources/3_0/oneof-with-discriminator.yml b/openapi-generator/src/test/resources/3_0/oneof-with-discriminator.yml index b8400aab9a..0e75b337a7 100644 --- a/openapi-generator/src/test/resources/3_0/oneof-with-discriminator.yml +++ b/openapi-generator/src/test/resources/3_0/oneof-with-discriminator.yml @@ -98,3 +98,9 @@ components: personName: type: string description: Person name + sex: + type: string + description: Person sex + enum: + - male + - female