Skip to content

Commit 92046bd

Browse files
committed
Add configurable enum value capitalization
This change makes enum value capitalization optional and configurable through the JaxRsProjectSettings class. Previously, enum values were always converted to uppercase. Now users can control this behavior with the capitalizeEnumValues setting (default: true for backward compatibility). Changes: - Added capitalizeEnumValues field to JaxRsProjectSettings - Modified JaxRsEnumRule to accept settings and conditionally capitalize - Updated JaxRsRuleFactory to pass settings to JaxRsEnumRule - Updated sanitizeEnumConstantName() to respect the configuration To preserve original enum casing: settings.setCapitalizeEnumValues(false); To use uppercase enum constants (default): settings.setCapitalizeEnumValues(true);
1 parent d70ab8f commit 92046bd

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

core/src/main/java/io/apicurio/hub/api/codegen/JaxRsEnumRule.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ public class JaxRsEnumRule implements Rule<JClassContainer, JType> {
6363
private static final String VALUE_FIELD_NAME = "value";
6464

6565
private final RuleFactory ruleFactory;
66-
67-
protected JaxRsEnumRule(RuleFactory ruleFactory) {
66+
private final JaxRsProjectSettings settings;
67+
68+
protected JaxRsEnumRule(RuleFactory ruleFactory, JaxRsProjectSettings settings) {
6869
this.ruleFactory = ruleFactory;
70+
this.settings = settings;
6971
}
7072

7173
/**
@@ -321,7 +323,7 @@ protected String getConstantName(String nodeName, String customName) {
321323
* Sanitizes an enum constant name to be a valid Java identifier.
322324
* - Replaces dashes with underscores
323325
* - Removes other illegal characters
324-
* - Converts to uppercase
326+
* - Optionally converts to uppercase (based on settings)
325327
* - Prepends underscore if name starts with a digit
326328
*
327329
* @param name the original enum value
@@ -334,8 +336,10 @@ private String sanitizeEnumConstantName(String name) {
334336
// Remove all characters that are not valid in Java identifiers (except underscore)
335337
name = name.replaceAll("[^a-zA-Z0-9_]", "");
336338

337-
// Convert to uppercase to follow Java enum constant conventions
338-
name = name.toUpperCase();
339+
// Conditionally convert to uppercase based on settings
340+
if (settings.isCapitalizeEnumValues()) {
341+
name = name.toUpperCase();
342+
}
339343

340344
// Prepend underscore if name starts with a digit
341345
if (!name.isEmpty() && Character.isDigit(name.charAt(0))) {

core/src/main/java/io/apicurio/hub/api/codegen/JaxRsProjectSettings.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class JaxRsProjectSettings {
3636
protected boolean useJsr303 = false;
3737
protected boolean generateBuilders = false;
3838
protected boolean generatesOpenApi = true;
39+
protected boolean capitalizeEnumValues = true;
3940

4041
/**
4142
* Constructor.
@@ -186,4 +187,18 @@ public boolean isGenerateBuilders() {
186187
public void setGenerateBuilders(boolean generateBuilders) {
187188
this.generateBuilders = generateBuilders;
188189
}
190+
191+
/**
192+
* @return the capitalizeEnumValues
193+
*/
194+
public boolean isCapitalizeEnumValues() {
195+
return capitalizeEnumValues;
196+
}
197+
198+
/**
199+
* @param capitalizeEnumValues the capitalizeEnumValues to set
200+
*/
201+
public void setCapitalizeEnumValues(boolean capitalizeEnumValues) {
202+
this.capitalizeEnumValues = capitalizeEnumValues;
203+
}
189204
}

core/src/main/java/io/apicurio/hub/api/codegen/OpenApi2JaxRs.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ public Schema create(Schema parent, String path, String refFragmentPathDelimiter
990990
// TODO if we get here, we probably want to return an empty schema
991991
return super.create(parent, path, refFragmentPathDelimiters);
992992
}
993-
}),
993+
}, settings),
994994
new SchemaGenerator());
995995
String source = mapper.writeValueAsString(bean.get$schema());
996996
schemaMapper.generate(codeModel, generatedBeanName, generatedBeanPackage, source);
@@ -1086,19 +1086,22 @@ public void setUpdateOnly(boolean updateOnly) {
10861086

10871087
public static class JaxRsRuleFactory extends RuleFactory {
10881088

1089+
private final JaxRsProjectSettings settings;
1090+
10891091
/**
10901092
* Constructor.
10911093
*/
1092-
public JaxRsRuleFactory(GenerationConfig generationConfig, Annotator annotator, SchemaStore schemaStore) {
1094+
public JaxRsRuleFactory(GenerationConfig generationConfig, Annotator annotator, SchemaStore schemaStore, JaxRsProjectSettings settings) {
10931095
super(generationConfig, annotator, schemaStore);
1096+
this.settings = settings;
10941097
}
10951098

10961099
/**
10971100
* @see org.jsonschema2pojo.rules.RuleFactory#getEnumRule()
10981101
*/
10991102
@Override
11001103
public Rule<JClassContainer, JType> getEnumRule() {
1101-
return new JaxRsEnumRule(this);
1104+
return new JaxRsEnumRule(this, settings);
11021105
}
11031106
}
11041107

0 commit comments

Comments
 (0)