Skip to content

Commit

Permalink
Added support additionalEnumTypeAnnotations, `additionalModelTypeAn…
Browse files Browse the repository at this point in the history
…notations`, `additionalOneOfTypeAnnotations` properties to generator.

Added ability to set `additionalProperties` by gradle / maven plugin
  • Loading branch information
altro3 committed Sep 16, 2024
1 parent 067fd50 commit 182f87d
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -385,6 +386,21 @@ public void setGeneratedAnnotation(boolean generatedAnnotation) {

@Override
public void processOpts() {

// need it to add ability to set List<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -132,6 +135,8 @@ public abstract class AbstractMicronautKotlinCodegen<T extends GeneratorOptionsB
public static final String OPT_IMPLICIT_HEADERS_REGEX = "implicitHeadersRegex";
public static final String OPT_USE_ENUM_CASE_INSENSITIVE = "useEnumCaseInsensitive";
public static final String OPT_KSP = "ksp";
public static final String ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS = "additionalOneOfTypeAnnotations";
public static final String ADDITIONAL_ENUM_TYPE_ANNOTATIONS = "additionalEnumTypeAnnotations";
public static final String CONTENT_TYPE_APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded";
public static final String CONTENT_TYPE_APPLICATION_JSON = "application/json";
public static final String CONTENT_TYPE_MULTIPART_FORM_DATA = "multipart/form-data";
Expand Down Expand Up @@ -167,6 +172,8 @@ public abstract class AbstractMicronautKotlinCodegen<T extends GeneratorOptionsB
protected List<ParameterMapping> parameterMappings = new ArrayList<>();
protected List<ResponseBodyMapping> responseBodyMappings = new ArrayList<>();
protected Map<String, CodegenModel> allModels = new HashMap<>();
protected List<String> additionalOneOfTypeAnnotations = new LinkedList<>();
protected List<String> additionalEnumTypeAnnotations = new LinkedList<>();

private final Logger log = LoggerFactory.getLogger(getClass());

Expand Down Expand Up @@ -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<List<?>> for arrays in generated code", fluxForArrays));
cliOptions.add(CliOption.newBoolean(OPT_GENERATED_ANNOTATION, "Generate code with \"@Generated\" annotation", generatedAnnotation));
Expand Down Expand Up @@ -457,6 +466,21 @@ public void setKsp(boolean ksp) {

@Override
public void processOpts() {

// need it to add ability to set List<String> 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
Expand Down Expand Up @@ -1554,6 +1578,20 @@ public String toVarName(String name) {
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
objs = super.postProcessAllModels(objs);

if (!additionalOneOfTypeAnnotations.isEmpty()) {
for (String modelName : objs.keySet()) {
Map<String, Object> models = objs.get(modelName);
models.put(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS, additionalOneOfTypeAnnotations);
}
}

if (!additionalEnumTypeAnnotations.isEmpty()) {
for (String modelName : objs.keySet()) {
Map<String, Object> models = objs.get(modelName);
models.put(ADDITIONAL_ENUM_TYPE_ANNOTATIONS, additionalEnumTypeAnnotations);
}
}

var isServer = isServer();

for (ModelsMap models : objs.values()) {
Expand Down Expand Up @@ -2230,6 +2268,14 @@ public void setUseEnumCaseInsensitive(boolean useEnumCaseInsensitive) {
this.useEnumCaseInsensitive = useEnumCaseInsensitive;
}

public void setAdditionalOneOfTypeAnnotations(List<String> additionalOneOfTypeAnnotations) {
this.additionalOneOfTypeAnnotations = additionalOneOfTypeAnnotations;
}

public void setAdditionalEnumTypeAnnotations(List<String> additionalEnumTypeAnnotations) {
this.additionalEnumTypeAnnotations = additionalEnumTypeAnnotations;
}

@Override
public void postProcess() {
// disable output donation suggestion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String> additionalEnumTypeAnnotations;
private List<String> additionalModelTypeAnnotations;
private List<String> additionalOneOfTypeAnnotations;
private Map<String, Object> additionalProperties;

@Override
public MicronautCodeGeneratorOptionsBuilder withLang(GeneratorLanguage lang) {
Expand Down Expand Up @@ -738,6 +767,30 @@ public MicronautCodeGeneratorOptionsBuilder withUseOneOfInterfaces(boolean useOn
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withAdditionalEnumTypeAnnotations(List<String> additionalEnumTypeAnnotations) {
this.additionalEnumTypeAnnotations = additionalEnumTypeAnnotations;
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withAdditionalModelTypeAnnotations(List<String> additionalModelTypeAnnotations) {
this.additionalModelTypeAnnotations = additionalModelTypeAnnotations;
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withAdditionalOneOfTypeAnnotations(List<String> additionalOneOfTypeAnnotations) {
this.additionalOneOfTypeAnnotations = additionalOneOfTypeAnnotations;
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withAdditionalProperties(Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
return this;
}

private Options build() {
return new Options(
lang,
Expand Down Expand Up @@ -775,7 +828,12 @@ private Options build() {
generateSwaggerAnnotations,
testFramework,
serializationLibraryKind,
dateTimeFormat);
dateTimeFormat,
additionalEnumTypeAnnotations,
additionalModelTypeAnnotations,
additionalOneOfTypeAnnotations,
additionalProperties
);
}
}
}
Expand Down Expand Up @@ -832,7 +890,11 @@ private record Options(
boolean generateSwaggerAnnotations,
TestFramework testFramework,
SerializationLibraryKind serializationLibraryKind,
MicronautCodeGeneratorOptionsBuilder.DateTimeFormat dateTimeFormat
MicronautCodeGeneratorOptionsBuilder.DateTimeFormat dateTimeFormat,
List<String> additionalEnumTypeAnnotations,
List<String> additionalModelTypeAnnotations,
List<String> additionalOneOfTypeAnnotations,
Map<String, Object> additionalProperties
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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<String> additionalOneOfTypeAnnotations);

/**
* Additional generator properties.
*
* @param additionalProperties additional generator properties.
*
* @return this builder
*/
MicronautCodeGeneratorOptionsBuilder withAdditionalProperties(Map<String, Object> additionalProperties);

/**
* The possible date-time formatting configurations.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> readListOfStringsProperty(String property, Map<String, Object> additionalProperties) {
var additionalAnnotations = additionalProperties.get(property);
if (additionalAnnotations == null) {
return Collections.emptyList();
}
List<String> 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;
}
}
Loading

0 comments on commit 182f87d

Please sign in to comment.