From 1dcb440e2d4cc454237d1775b92cb4b564310af0 Mon Sep 17 00:00:00 2001 From: Eric Goodhall Date: Thu, 18 Apr 2024 15:51:55 -0400 Subject: [PATCH] WireSafeEnum constructed from alias should serialize to enum constant name --- .../java/com/hubspot/immutables/utils/WireSafeEnum.java | 5 +---- .../com/hubspot/immutables/utils/WireSafeEnumTest.java | 8 ++++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/hubspot-style/src/main/java/com/hubspot/immutables/utils/WireSafeEnum.java b/hubspot-style/src/main/java/com/hubspot/immutables/utils/WireSafeEnum.java index 4e47ad8..73cb51d 100644 --- a/hubspot-style/src/main/java/com/hubspot/immutables/utils/WireSafeEnum.java +++ b/hubspot-style/src/main/java/com/hubspot/immutables/utils/WireSafeEnum.java @@ -279,10 +279,7 @@ private static > void initializeCache(Class enumType) { } for (String alias : getJsonAliases(enumType, enumValue)) { - jsonMap.computeIfAbsent( - alias, - jsonAlias -> new WireSafeEnum<>(enumType, jsonAlias, enumValue) - ); + jsonMap.putIfAbsent(alias, wireSafeEnum); } } diff --git a/hubspot-style/src/test/java/com/hubspot/immutables/utils/WireSafeEnumTest.java b/hubspot-style/src/test/java/com/hubspot/immutables/utils/WireSafeEnumTest.java index 14ec775..3236a14 100644 --- a/hubspot-style/src/test/java/com/hubspot/immutables/utils/WireSafeEnumTest.java +++ b/hubspot-style/src/test/java/com/hubspot/immutables/utils/WireSafeEnumTest.java @@ -316,7 +316,7 @@ public void itBuildsFromJsonAlias() { ); assertThat(wrapper.enumType()).isEqualTo(AliasJsonEnum.class); - assertThat(wrapper.asString()).isEqualTo("abc"); + assertThat(wrapper.asString()).isEqualTo("ABC"); assertThat(wrapper.asEnum()).isEqualTo(Optional.of(AliasJsonEnum.ABC)); } @@ -469,13 +469,13 @@ public void itSerializesUnknownValueAsStringWithCollidingJsonAndCreator() } @Test - public void itSerializesAliasAsAlias() throws IOException { + public void itSerializesAliasAsConstantName() throws IOException { WireSafeEnum wrapper = WireSafeEnum.fromJson( AliasJsonEnum.class, "abc" ); - writeToJson(wrapper).forEach(s -> assertThat(s).isEqualTo("\"abc\"")); + writeToJson(wrapper).forEach(s -> assertThat(s).isEqualTo("\"ABC\"")); } @Test @@ -483,7 +483,7 @@ public void itDeserializesFromKnownAliasString() throws IOException { readFromJson("\"abc\"", new TypeReference>() {}) .forEach(wrapper -> { assertThat(wrapper.enumType()).isEqualTo(AliasJsonEnum.class); - assertThat(wrapper.asString()).isEqualTo("abc"); + assertThat(wrapper.asString()).isEqualTo("ABC"); assertThat(wrapper.asEnum()).isEqualTo(Optional.of(AliasJsonEnum.ABC)); }); }