From 4d37855b9d959f56430dca724f1c6b1d68e222f6 Mon Sep 17 00:00:00 2001 From: Mohamed EL-Habib Date: Fri, 5 Dec 2025 19:51:39 +0100 Subject: [PATCH] Fix: Keep acronyms uppercase in Java enum constants (#2850) Enum constants containing known acronyms (like "SPA") were being incorrectly converted when using --acronym-style=camel, e.g. "MULTI_SPA_IN_GROUP_REJECTED" became "MULTI_Spa_IN_GROUP_REJECTED". For Java enum constants (UPPER_UNDERSCORE style), acronyms should always remain uppercase regardless of the --acronym-style setting. - Modified javaNameStyle() to use allUpperWordStyle for acronyms in enum constants - Added test case for enum with "SPA" acronym --- .../quicktype-core/src/language/Java/utils.ts | 8 ++-- .../main/java/io/quicktype/MessageCode.java | 42 +++++++++++++++++++ test/inputs/json/priority/enum-spa.json | 10 +++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/java/src/main/java/io/quicktype/MessageCode.java create mode 100644 test/inputs/json/priority/enum-spa.json diff --git a/packages/quicktype-core/src/language/Java/utils.ts b/packages/quicktype-core/src/language/Java/utils.ts index 8e27c72b6..48827f4c1 100644 --- a/packages/quicktype-core/src/language/Java/utils.ts +++ b/packages/quicktype-core/src/language/Java/utils.ts @@ -44,13 +44,15 @@ export function javaNameStyle( upperUnderscore ? allUpperWordStyle : startWithUpper - ? firstUpperWordStyle - : allLowerWordStyle, + ? firstUpperWordStyle + : allLowerWordStyle, upperUnderscore ? allUpperWordStyle : firstUpperWordStyle, upperUnderscore || startWithUpper ? allUpperWordStyle : allLowerWordStyle, - acronymsStyle, + // For UPPER_UNDERSCORE style (Java enum constants), always use allUpperWordStyle for acronyms + // to maintain consistency with the naming convention (e.g., XXX_SPA_XXX) + upperUnderscore ? allUpperWordStyle : acronymsStyle, upperUnderscore ? "_" : "", isStartCharacter, ); diff --git a/test/fixtures/java/src/main/java/io/quicktype/MessageCode.java b/test/fixtures/java/src/main/java/io/quicktype/MessageCode.java new file mode 100644 index 000000000..4d113ae8f --- /dev/null +++ b/test/fixtures/java/src/main/java/io/quicktype/MessageCode.java @@ -0,0 +1,42 @@ +package io.quicktype; + +import java.io.IOException; +import com.fasterxml.jackson.annotation.*; + +public class MessageCode { + private Code code; + + @JsonProperty("code") + public Code getCode() { + return code; + } + + @JsonProperty("code") + public void setCode(Code value) { + this.code = value; + } + + public enum Code { + MULTI_SPA_IN_GROUP_REJECTED, SOME_OTHER_VALUE; + + @JsonValue + public String toValue() { + switch (this) { + case MULTI_SPA_IN_GROUP_REJECTED: + return "MULTI_SPA_IN_GROUP_REJECTED"; + case SOME_OTHER_VALUE: + return "SOME_OTHER_VALUE"; + } + return null; + } + + @JsonCreator + public static Code forValue(String value) throws IOException { + if (value.equals("MULTI_SPA_IN_GROUP_REJECTED")) + return MULTI_SPA_IN_GROUP_REJECTED; + if (value.equals("SOME_OTHER_VALUE")) + return SOME_OTHER_VALUE; + throw new IOException("Cannot deserialize Code"); + } + } +} diff --git a/test/inputs/json/priority/enum-spa.json b/test/inputs/json/priority/enum-spa.json new file mode 100644 index 000000000..ce5b3aeb3 --- /dev/null +++ b/test/inputs/json/priority/enum-spa.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "code": { + "type": "string", + "enum": ["SOME_OTHER_VALUE", "MULTI_SPA_IN_GROUP_REJECTED"] + } + } +}