Skip to content

Commit

Permalink
WireSafeEnum constructed from alias should serialize to enum constant…
Browse files Browse the repository at this point in the history
… name
  • Loading branch information
egoodhall committed Apr 18, 2024
1 parent 400e0f2 commit 1dcb440
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,7 @@ private static <T extends Enum<T>> void initializeCache(Class<T> enumType) {
}

for (String alias : getJsonAliases(enumType, enumValue)) {
jsonMap.computeIfAbsent(
alias,
jsonAlias -> new WireSafeEnum<>(enumType, jsonAlias, enumValue)
);
jsonMap.putIfAbsent(alias, wireSafeEnum);
}
}

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

Expand Down Expand Up @@ -469,21 +469,21 @@ public void itSerializesUnknownValueAsStringWithCollidingJsonAndCreator()
}

@Test
public void itSerializesAliasAsAlias() throws IOException {
public void itSerializesAliasAsConstantName() throws IOException {
WireSafeEnum<AliasJsonEnum> wrapper = WireSafeEnum.fromJson(
AliasJsonEnum.class,
"abc"
);

writeToJson(wrapper).forEach(s -> assertThat(s).isEqualTo("\"abc\""));
writeToJson(wrapper).forEach(s -> assertThat(s).isEqualTo("\"ABC\""));
}

@Test
public void itDeserializesFromKnownAliasString() throws IOException {
readFromJson("\"abc\"", new TypeReference<WireSafeEnum<AliasJsonEnum>>() {})
.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));
});
}
Expand Down

0 comments on commit 1dcb440

Please sign in to comment.