Skip to content

Commit

Permalink
Add support useEnumCaseInsensitive
Browse files Browse the repository at this point in the history
Fixed #1760
  • Loading branch information
altro3 committed Sep 7, 2024
1 parent f6df1b9 commit bed46c8
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public abstract class AbstractMicronautJavaCodegen<T extends GeneratorOptionsBui
public static final String OPT_DATE_LIBRARY_LOCAL_DATETIME = "LOCAL_DATETIME";
public static final String OPT_DATE_FORMAT = "dateFormat";
public static final String OPT_DATE_TIME_FORMAT = "dateTimeFormat";
public static final String OPT_USE_ENUM_CASE_INSENSITIVE = "useEnumCaseInsensitive";
public static final String OPT_REACTIVE = "reactive";
public static final String OPT_GENERATE_HTTP_RESPONSE_ALWAYS = "generateHttpResponseAlways";
public static final String OPT_GENERATE_HTTP_RESPONSE_WHERE_REQUIRED = "generateHttpResponseWhereRequired";
Expand Down Expand Up @@ -145,6 +146,7 @@ public abstract class AbstractMicronautJavaCodegen<T extends GeneratorOptionsBui
protected String testTool;
protected boolean requiredPropertiesInConstructor = true;
protected boolean reactive;
protected boolean useEnumCaseInsensitive;
protected boolean generateHttpResponseAlways;
protected boolean generateHttpResponseWhereRequired = true;
protected String appName;
Expand Down Expand Up @@ -203,7 +205,9 @@ protected AbstractMicronautJavaCodegen() {
SecurityFeature.OAuth2_AuthorizationCode,
SecurityFeature.OAuth2_ClientCredentials,
SecurityFeature.OAuth2_Password,
SecurityFeature.OpenIDConnect
SecurityFeature.OpenIDConnect,
SecurityFeature.SignatureAuth,
SecurityFeature.AWSV4Signature
))
);

Expand Down Expand Up @@ -233,15 +237,17 @@ protected AbstractMicronautJavaCodegen() {
cliOptions.add(CliOption.newBoolean(OPT_GENERATE_HTTP_RESPONSE_WHERE_REQUIRED, "Wrap the operations response in HttpResponse object where non-200 HTTP status codes or additional headers are defined", generateHttpResponseWhereRequired));
cliOptions.add(CliOption.newBoolean(OPT_GENERATE_OPERATION_ONLY_FOR_FIRST_TAG, "When false, the operation method will be duplicated in each of the tags if multiple tags are assigned to this operation. " +
"If true, each operation will be generated only once in the first assigned tag.", generateOperationOnlyForFirstTag));
CliOption testToolOption = new CliOption(OPT_TEST, "Specify which test tool to generate files for").defaultValue(testTool);
Map<String, String> testToolOptionMap = new HashMap<>();
cliOptions.add(CliOption.newBoolean(OPT_USE_ENUM_CASE_INSENSITIVE, "Use `equalsIgnoreCase` when String for enum comparison", useEnumCaseInsensitive));

var testToolOption = new CliOption(OPT_TEST, "Specify which test tool to generate files for").defaultValue(testTool);
var testToolOptionMap = new HashMap<String, String>();
testToolOptionMap.put(OPT_TEST_JUNIT, "Use JUnit as test tool");
testToolOptionMap.put(OPT_TEST_SPOCK, "Use Spock as test tool");
testToolOption.setEnum(testToolOptionMap);
cliOptions.add(testToolOption);

CliOption generateSwaggerAnnotationsOption = new CliOption(OPT_GENERATE_SWAGGER_ANNOTATIONS, "Specify if you want to generate swagger annotations and which version").defaultValue(generateSwaggerAnnotations);
Map<String, String> generateSwaggerAnnotationsOptionMap = new HashMap<>();
var generateSwaggerAnnotationsOption = new CliOption(OPT_GENERATE_SWAGGER_ANNOTATIONS, "Specify if you want to generate swagger annotations and which version").defaultValue(generateSwaggerAnnotations);
var generateSwaggerAnnotationsOptionMap = new HashMap<String, String>();
generateSwaggerAnnotationsOptionMap.put(OPT_GENERATE_SWAGGER_ANNOTATIONS_SWAGGER_1, "Use io.swagger:swagger-annotations for annotating operations and schemas");
generateSwaggerAnnotationsOptionMap.put(OPT_GENERATE_SWAGGER_ANNOTATIONS_SWAGGER_2, "Use io.swagger.core.v3:swagger-annotations for annotating operations and schemas");
generateSwaggerAnnotationsOptionMap.put(OPT_GENERATE_SWAGGER_ANNOTATIONS_TRUE, "Equivalent to \"" + OPT_GENERATE_SWAGGER_ANNOTATIONS_SWAGGER_2 + "\"");
Expand Down Expand Up @@ -431,6 +437,11 @@ public void processOpts() {
}
writePropertyBack(OPT_FLUX_FOR_ARRAYS, fluxForArrays);

if (additionalProperties.containsKey(OPT_USE_ENUM_CASE_INSENSITIVE)) {
useEnumCaseInsensitive = convertPropertyToBoolean(OPT_USE_ENUM_CASE_INSENSITIVE);
}
writePropertyBack(OPT_USE_ENUM_CASE_INSENSITIVE, useEnumCaseInsensitive);

if (additionalProperties.containsKey(OPT_GENERATED_ANNOTATION)) {
generatedAnnotation = convertPropertyToBoolean(OPT_GENERATED_ANNOTATION);
}
Expand Down Expand Up @@ -2010,6 +2021,10 @@ public void setDateTimeFormat(String dateTimeFormat) {
additionalProperties.put(OPT_DATE_TIME_FORMAT, dateTimeFormat);
}

public void setUseEnumCaseInsensitive(boolean useEnumCaseInsensitive) {
this.useEnumCaseInsensitive = useEnumCaseInsensitive;
}

@Override
public void postProcess() {
// disable output donation suggestion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public abstract class AbstractMicronautKotlinCodegen<T extends GeneratorOptionsB
public static final String OPT_GENERATE_OPERATION_ONLY_FOR_FIRST_TAG = "generateOperationOnlyForFirstTag";
public static final String OPT_IMPLICIT_HEADERS = "implicitHeaders";
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 CONTENT_TYPE_APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded";
public static final String CONTENT_TYPE_APPLICATION_JSON = "application/json";
Expand All @@ -153,8 +154,9 @@ public abstract class AbstractMicronautKotlinCodegen<T extends GeneratorOptionsB
protected boolean reactive;
protected boolean generateHttpResponseAlways;
protected boolean generateHttpResponseWhereRequired = true;
protected boolean useEnumCaseInsensitive;
protected boolean ksp;
protected boolean implicitHeaders = false;
protected boolean implicitHeaders;
protected String implicitHeadersRegex;
protected String appName;
protected String dateFormat;
Expand Down Expand Up @@ -255,7 +257,9 @@ protected AbstractMicronautKotlinCodegen() {
SecurityFeature.OAuth2_AuthorizationCode,
SecurityFeature.OAuth2_ClientCredentials,
SecurityFeature.OAuth2_Password,
SecurityFeature.OpenIDConnect
SecurityFeature.OpenIDConnect,
SecurityFeature.SignatureAuth,
SecurityFeature.AWSV4Signature
))
.excludeGlobalFeatures(GlobalFeature.XMLStructureDefinitions, GlobalFeature.Callbacks, GlobalFeature.LinkObjects, GlobalFeature.ParameterStyling)
.excludeSchemaSupportFeatures(SchemaSupportFeature.Polymorphism)
Expand Down Expand Up @@ -291,14 +295,16 @@ protected AbstractMicronautKotlinCodegen() {
cliOptions.add(CliOption.newBoolean(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC, isHideGenerationTimestamp()));
cliOptions.add(CliOption.newBoolean(OPT_GENERATE_OPERATION_ONLY_FOR_FIRST_TAG, "When false, the operation method will be duplicated in each of the tags if multiple tags are assigned to this operation. " +
"If true, each operation will be generated only once in the first assigned tag.", generateOperationOnlyForFirstTag));
CliOption testToolOption = new CliOption(OPT_TEST, "Specify which test tool to generate files for").defaultValue(testTool);
Map<String, String> testToolOptionMap = new HashMap<>();
cliOptions.add(CliOption.newBoolean(OPT_USE_ENUM_CASE_INSENSITIVE, "Use `equalsIgnoreCase` when String for enum comparison", useEnumCaseInsensitive));

var testToolOption = new CliOption(OPT_TEST, "Specify which test tool to generate files for").defaultValue(testTool);
var testToolOptionMap = new HashMap<String, String>();
testToolOptionMap.put(OPT_TEST_JUNIT, "Use JUnit as test tool");
testToolOption.setEnum(testToolOptionMap);
cliOptions.add(testToolOption);

CliOption generateSwaggerAnnotationsOption = new CliOption(OPT_GENERATE_SWAGGER_ANNOTATIONS, "Specify if you want to generate swagger annotations and which version").defaultValue(generateSwaggerAnnotations);
Map<String, String> generateSwaggerAnnotationsOptionMap = new HashMap<>();
var generateSwaggerAnnotationsOption = new CliOption(OPT_GENERATE_SWAGGER_ANNOTATIONS, "Specify if you want to generate swagger annotations and which version").defaultValue(generateSwaggerAnnotations);
var generateSwaggerAnnotationsOptionMap = new HashMap<String, String>();
generateSwaggerAnnotationsOptionMap.put(OPT_GENERATE_SWAGGER_ANNOTATIONS_SWAGGER_2, "Use io.swagger.core.v3:swagger-annotations for annotating operations and schemas");
generateSwaggerAnnotationsOptionMap.put(OPT_GENERATE_SWAGGER_ANNOTATIONS_TRUE, "Equivalent to \"" + OPT_GENERATE_SWAGGER_ANNOTATIONS_SWAGGER_2 + "\"");
generateSwaggerAnnotationsOptionMap.put(OPT_GENERATE_SWAGGER_ANNOTATIONS_FALSE, "Do not generate swagger annotations");
Expand All @@ -313,7 +319,7 @@ protected AbstractMicronautKotlinCodegen() {
.filter(o -> o.getOpt().equals(DATE_LIBRARY))
.findFirst()
.ifPresent(opt -> {
Map<String, String> valuesEnum = new HashMap<>();
var valuesEnum = new HashMap<String, String>();
valuesEnum.put(OPT_DATE_LIBRARY_OFFSET_DATETIME, opt.getEnum().get(OPT_DATE_LIBRARY_OFFSET_DATETIME));
valuesEnum.put(OPT_DATE_LIBRARY_LOCAL_DATETIME, opt.getEnum().get(OPT_DATE_LIBRARY_LOCAL_DATETIME));
opt.setEnum(valuesEnum);
Expand Down Expand Up @@ -498,6 +504,11 @@ public void processOpts() {
}
writePropertyBack(OPT_FLUX_FOR_ARRAYS, fluxForArrays);

if (additionalProperties.containsKey(OPT_USE_ENUM_CASE_INSENSITIVE)) {
useEnumCaseInsensitive = convertPropertyToBoolean(OPT_USE_ENUM_CASE_INSENSITIVE);
}
writePropertyBack(OPT_USE_ENUM_CASE_INSENSITIVE, useEnumCaseInsensitive);

if (additionalProperties.containsKey(OPT_GENERATED_ANNOTATION)) {
generatedAnnotation = convertPropertyToBoolean(OPT_GENERATED_ANNOTATION);
}
Expand Down Expand Up @@ -2215,6 +2226,10 @@ public void setDateTimeFormat(String dateTimeFormat) {
additionalProperties.put(OPT_DATE_TIME_FORMAT, dateTimeFormat);
}

public void setUseEnumCaseInsensitive(boolean useEnumCaseInsensitive) {
this.useEnumCaseInsensitive = useEnumCaseInsensitive;
}

@Override
public void postProcess() {
// disable output donation suggestion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ private void configureOptions() {
javaCodeGen.setGenerateHttpResponseWhereRequired(options.generateHttpResponseWhereRequired);
javaCodeGen.setUseOptional(options.optional);
javaCodeGen.setUseBeanValidation(options.beanValidation);
javaCodeGen.setUseEnumCaseInsensitive(options.useEnumCaseInsensitive);
javaCodeGen.setTestTool(options.testFramework.value);
javaCodeGen.setSerializationLibrary(options.serializationLibraryKind.name());
javaCodeGen.setGenerateSwaggerAnnotations(options.generateSwaggerAnnotations);
Expand Down Expand Up @@ -262,6 +263,7 @@ private void configureOptions() {
kotlinCodeGen.setGenerateHttpResponseWhereRequired(options.generateHttpResponseWhereRequired);
kotlinCodeGen.setGenerateSwaggerAnnotations(options.generateSwaggerAnnotations);
kotlinCodeGen.setUseBeanValidation(options.beanValidation);
kotlinCodeGen.setUseEnumCaseInsensitive(options.useEnumCaseInsensitive);
kotlinCodeGen.setTestTool(options.testFramework.value);
kotlinCodeGen.setSerializationLibrary(options.serializationLibraryKind.name());
kotlinCodeGen.setDateTimeLibrary(options.dateTimeFormat.name());
Expand Down Expand Up @@ -504,6 +506,7 @@ private static class DefaultOptionsBuilder implements MicronautCodeGeneratorOpti
private String apiPackage;
private String artifactId;
private boolean beanValidation = true;
private boolean useEnumCaseInsensitive;
private String invokerPackage;
private String modelPackage;
private List<ParameterMapping> parameterMappings;
Expand Down Expand Up @@ -699,6 +702,12 @@ public MicronautCodeGeneratorOptionsBuilder withBeanValidation(boolean beanValid
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withUseEnumCaseInsensitive(boolean useEnumCaseInsensitive) {
this.useEnumCaseInsensitive = useEnumCaseInsensitive;
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withOptional(boolean optional) {
this.optional = optional;
Expand Down Expand Up @@ -757,6 +766,7 @@ private Options build() {
implicitHeadersRegex,

beanValidation,
useEnumCaseInsensitive,
optional,
reactive,
useOneOfInterfaces,
Expand Down Expand Up @@ -813,6 +823,7 @@ private record Options(
String implicitHeadersRegex,

boolean beanValidation,
boolean useEnumCaseInsensitive,
boolean optional,
boolean reactive,
boolean useOneOfInterfaces,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ public interface MicronautCodeGeneratorOptionsBuilder {
*/
MicronautCodeGeneratorOptionsBuilder withBeanValidation(boolean beanValidation);

/**
* If set to true, the generated enums check enum value with ignoring case.
*
* @param useEnumCaseInsensitive the useEnumCaseInsensitive flag
* @return this builder
*/
MicronautCodeGeneratorOptionsBuilder withUseEnumCaseInsensitive(boolean useEnumCaseInsensitive);

/**
* If set to true, the generated code will make use of {@link java.util.Optional}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,37 @@
/**
* Create this enum from a value.
*
* @param value the value
* @param value The value
*
* @return The enum
*/
{{#jackson}}
@JsonCreator
{{/jackson}}
{{#formatSingleLine}}public static {{>common/model/enumName}} fromValue({{{dataType}}} value){{/formatSingleLine}} {
{{#formatSingleLine}}public static {{>common/model/enumName}} fromValue({{{dataType}}} value){{/formatSingleLine}} {{openbrace}}{{#formatNoEmptyLines}}
{{#isString}}{{#useEnumCaseInsensitive}}
var key = value.toLowerCase();
{{/useEnumCaseInsensitive}}{{/isString}}
{{^isNullable}}
if (!VALUE_MAPPING.containsKey(value{{#isString}}{{#useEnumCaseInsensitive}}.toLowerCase(){{/useEnumCaseInsensitive}}{{/isString}})) {
{{#isString}}
{{#useEnumCaseInsensitive}}
if (!VALUE_MAPPING.containsKey(key)) {
throw new IllegalArgumentException("Unexpected value '" + key + "'");
}
{{/useEnumCaseInsensitive}}
{{^useEnumCaseInsensitive}}
if (!VALUE_MAPPING.containsKey(value)) {
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
{{/useEnumCaseInsensitive}}
{{/isString}}
{{^isString}}
if (!VALUE_MAPPING.containsKey(value)) {
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
{{/isString}}
{{/isNullable}}
return VALUE_MAPPING.get(value{{#isString}}{{#useEnumCaseInsensitive}}.toLowerCase(){{/useEnumCaseInsensitive}}{{/isString}});
return VALUE_MAPPING.get({{#isString}}{{#useEnumCaseInsensitive}}key{{/useEnumCaseInsensitive}}{{^useEnumCaseInsensitive}}value{{/useEnumCaseInsensitive}}{{/isString}}{{^isString}}value{{/isString}});
{{/formatNoEmptyLines}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,36 @@
companion object {
@JvmField
val VALUE_MAPPING = entries.associateBy { it.value }
val VALUE_MAPPING = entries.associateBy { it.value{{#useEnumCaseInsensitive}}.lowercase(){{/useEnumCaseInsensitive}} }

/**
* Create this enum from a value.
*
* @param value value for enum
* @param value The value
*
* @return The enum
*/
@JsonCreator
@JvmStatic
fun fromValue(value: {{{dataType}}}): {{>common/model/enumName}}{{#isNullable}}?{{/isNullable}} {
fun fromValue(value: {{{dataType}}}): {{>common/model/enumName}}{{#isNullable}}?{{/isNullable}} {{openbrace}}{{#formatNoEmptyLines}}
{{#isString}}{{#useEnumCaseInsensitive}}
val key = value.lowercase()
{{/useEnumCaseInsensitive}}{{/isString}}
{{^isNullable}}
{{#isString}}
{{#useEnumCaseInsensitive}}
require(VALUE_MAPPING.containsKey(key)) { "Unexpected value '$key'" }
{{/useEnumCaseInsensitive}}
{{^useEnumCaseInsensitive}}
require(VALUE_MAPPING.containsKey(value)) { "Unexpected value '$value'" }
{{/useEnumCaseInsensitive}}
{{/isString}}
{{^isString}}
require(VALUE_MAPPING.containsKey(value)) { "Unexpected value '$value'" }
{{/isString}}
{{/isNullable}}
return VALUE_MAPPING[value{{#isString}}{{#useEnumCaseInsensitive}}.lowerCase(){{/useEnumCaseInsensitive}}{{/isString}}]{{^isNullable}}!!{{/isNullable}}
return VALUE_MAPPING[{{#isString}}{{#useEnumCaseInsensitive}}key{{/useEnumCaseInsensitive}}{{^useEnumCaseInsensitive}}value{{/useEnumCaseInsensitive}}{{/isString}}{{^isString}}value{{/isString}}]{{^isNullable}}!!{{/isNullable}}
}
{{/formatNoEmptyLines}}
}
}
Loading

0 comments on commit bed46c8

Please sign in to comment.