From 8bf6a57b5883e95ed52737c087b4bb453b023f20 Mon Sep 17 00:00:00 2001 From: iamdanfox Date: Mon, 18 Feb 2019 13:51:46 +0000 Subject: [PATCH] useInsensitiveEnums feature flag fully restores old behaviour (#239) ## Before this PR @jcvitanovic reported problems on an internal product where empty strings were persisted to a database, but trying to opt in to the old behaviour using this feature flag would still break. ## After this PR The feature flag brings back exactly the old pre-2.8.0 behavour, where strings are leniently deserialized. _A full revert is still being considered here: https://github.com/palantir/conjure-java/pull/238_ --- .../com/palantir/insensitive/InsensitiveEnum.java | 2 -- .../palantir/conjure/java/types/EnumGenerator.java | 11 ++++++----- .../com/palantir/conjure/java/types/EnumTests.java | 12 ++++++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/insensitive/InsensitiveEnum.java b/conjure-java-core/src/integrationInput/java/com/palantir/insensitive/InsensitiveEnum.java index 34c087da5..44e40d602 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/insensitive/InsensitiveEnum.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/insensitive/InsensitiveEnum.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; -import com.palantir.conjure.java.lib.internal.ConjureEnums; import com.palantir.logsafe.Preconditions; import java.util.Locale; import javax.annotation.Generated; @@ -75,7 +74,6 @@ public static InsensitiveEnum valueOf(String value) { case "ONE_HUNDRED": return ONE_HUNDRED; default: - ConjureEnums.validate(value); return new InsensitiveEnum(Value.UNKNOWN, value); } } diff --git a/conjure-java-core/src/main/java/com/palantir/conjure/java/types/EnumGenerator.java b/conjure-java-core/src/main/java/com/palantir/conjure/java/types/EnumGenerator.java index 965439285..ddfea145f 100644 --- a/conjure-java-core/src/main/java/com/palantir/conjure/java/types/EnumGenerator.java +++ b/conjure-java-core/src/main/java/com/palantir/conjure/java/types/EnumGenerator.java @@ -224,11 +224,12 @@ private static MethodSpec createValueOf( .addStatement("return $L", value.getValue()) .unindent(); } - parser.add("default:\n") - .indent() - // Only validate unknown values, matches are validated at build time. - .addStatement("$T.validate($N)", ConjureEnums.class, param) - .addStatement("return new $T(Value.UNKNOWN, $N)", thisClass, param) + parser.add("default:\n").indent(); + if (!featureFlags.contains(FeatureFlags.CaseInsensitiveEnums)) { + // Only validate unknown values, matches are validated at build time. + parser.addStatement("$T.validate($N)", ConjureEnums.class, param); + } + parser.addStatement("return new $T(Value.UNKNOWN, $N)", thisClass, param) .unindent() .endControlFlow(); diff --git a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/EnumTests.java b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/EnumTests.java index a4268d4c3..e3a906afc 100644 --- a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/EnumTests.java +++ b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/EnumTests.java @@ -60,6 +60,18 @@ public void testInsensitiveEnum_lowerCaseUnknown() { assertThat(value.get()).isEqualTo(InsensitiveEnum.Value.UNKNOWN); } + @Test + public void testInsensitiveEnum_unknownToleratesEmptyStringsForHistoricalReasons() { + InsensitiveEnum value = InsensitiveEnum.valueOf(""); + assertThat(value.get()).isEqualTo(InsensitiveEnum.Value.UNKNOWN); + } + + @Test + public void testInsensitiveEnum_unknownToleratesStrangeCharactersForHistoricalReasons() { + InsensitiveEnum value = InsensitiveEnum.valueOf("!@£$%^&*()"); + assertThat(value.get()).isEqualTo(InsensitiveEnum.Value.UNKNOWN); + } + @Test public void testNullValidationUsesSafeLoggable() { assertThatLoggableExceptionThrownBy(() -> EnumExample.valueOf(null)).hasLogMessage("value cannot be null");