From cc534b76f1e3a6e20f9563e0b17e0044fefeec20 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Tue, 25 Dec 2018 13:08:11 +0000 Subject: [PATCH] Conjure enums are no longer case sensitive The Conjure specification requires upper case string values for enum constants. Updated null validation to use safe-loggable exceptions. --- .../product/AliasAsMapKeyExample.java | 33 +++++++----- .../java/com/palantir/product/AnyExample.java | 3 +- .../com/palantir/product/AnyMapExample.java | 5 +- .../palantir/product/BearerTokenExample.java | 3 +- .../com/palantir/product/BinaryExample.java | 3 +- .../product/CovariantListExample.java | 9 ++-- .../product/CovariantOptionalExample.java | 5 +- .../com/palantir/product/DateTimeExample.java | 3 +- .../com/palantir/product/EnumExample.java | 12 ++--- .../palantir/product/EnumFieldExample.java | 3 +- .../com/palantir/product/ListExample.java | 13 ++--- .../palantir/product/ManyFieldExample.java | 25 +++++---- .../java/com/palantir/product/MapExample.java | 5 +- .../com/palantir/product/OptionalExample.java | 5 +- .../product/PrimitiveOptionalsExample.java | 25 +++++---- .../palantir/product/ReservedKeyExample.java | 7 +-- .../java/com/palantir/product/RidExample.java | 3 +- .../com/palantir/product/SafeLongExample.java | 3 +- .../java/com/palantir/product/SetExample.java | 9 ++-- .../java/com/palantir/product/SimpleEnum.java | 12 ++--- .../com/palantir/product/SingleUnion.java | 7 +-- .../com/palantir/product/StringExample.java | 3 +- .../java/com/palantir/product/Union.java | 9 ++-- .../palantir/product/UnionTypeExample.java | 19 +++---- .../com/palantir/product/UuidExample.java | 3 +- .../conjure/java/types/EnumGenerator.java | 28 +++------- .../conjure/java/types/Expressions.java | 3 +- .../conjure/java/types/WireFormatTests.java | 34 ++++++++++-- .../java/lib/internal/ConjureEnums.java | 52 +++++++++++++++++++ 29 files changed, 223 insertions(+), 121 deletions(-) create mode 100644 conjure-lib/src/main/java/com/palantir/conjure/java/lib/internal/ConjureEnums.java diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/AliasAsMapKeyExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/AliasAsMapKeyExample.java index 4064673f2..748f18b50 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/AliasAsMapKeyExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/AliasAsMapKeyExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -219,12 +220,12 @@ public Builder from(AliasAsMapKeyExample other) { @JsonSetter("strings") public Builder strings(Map strings) { this.strings.clear(); - this.strings.putAll(Objects.requireNonNull(strings, "strings cannot be null")); + this.strings.putAll(Preconditions.checkNotNull(strings, "strings cannot be null")); return this; } public Builder putAllStrings(Map strings) { - this.strings.putAll(Objects.requireNonNull(strings, "strings cannot be null")); + this.strings.putAll(Preconditions.checkNotNull(strings, "strings cannot be null")); return this; } @@ -236,12 +237,12 @@ public Builder strings(StringAliasExample key, ManyFieldExample value) { @JsonSetter("rids") public Builder rids(Map rids) { this.rids.clear(); - this.rids.putAll(Objects.requireNonNull(rids, "rids cannot be null")); + this.rids.putAll(Preconditions.checkNotNull(rids, "rids cannot be null")); return this; } public Builder putAllRids(Map rids) { - this.rids.putAll(Objects.requireNonNull(rids, "rids cannot be null")); + this.rids.putAll(Preconditions.checkNotNull(rids, "rids cannot be null")); return this; } @@ -254,14 +255,14 @@ public Builder rids(RidAliasExample key, ManyFieldExample value) { public Builder bearertokens(Map bearertokens) { this.bearertokens.clear(); this.bearertokens.putAll( - Objects.requireNonNull(bearertokens, "bearertokens cannot be null")); + Preconditions.checkNotNull(bearertokens, "bearertokens cannot be null")); return this; } public Builder putAllBearertokens( Map bearertokens) { this.bearertokens.putAll( - Objects.requireNonNull(bearertokens, "bearertokens cannot be null")); + Preconditions.checkNotNull(bearertokens, "bearertokens cannot be null")); return this; } @@ -273,12 +274,12 @@ public Builder bearertokens(BearerTokenAliasExample key, ManyFieldExample value) @JsonSetter("integers") public Builder integers(Map integers) { this.integers.clear(); - this.integers.putAll(Objects.requireNonNull(integers, "integers cannot be null")); + this.integers.putAll(Preconditions.checkNotNull(integers, "integers cannot be null")); return this; } public Builder putAllIntegers(Map integers) { - this.integers.putAll(Objects.requireNonNull(integers, "integers cannot be null")); + this.integers.putAll(Preconditions.checkNotNull(integers, "integers cannot be null")); return this; } @@ -290,12 +291,14 @@ public Builder integers(IntegerAliasExample key, ManyFieldExample value) { @JsonSetter("safelongs") public Builder safelongs(Map safelongs) { this.safelongs.clear(); - this.safelongs.putAll(Objects.requireNonNull(safelongs, "safelongs cannot be null")); + this.safelongs.putAll( + Preconditions.checkNotNull(safelongs, "safelongs cannot be null")); return this; } public Builder putAllSafelongs(Map safelongs) { - this.safelongs.putAll(Objects.requireNonNull(safelongs, "safelongs cannot be null")); + this.safelongs.putAll( + Preconditions.checkNotNull(safelongs, "safelongs cannot be null")); return this; } @@ -307,12 +310,14 @@ public Builder safelongs(SafeLongAliasExample key, ManyFieldExample value) { @JsonSetter("datetimes") public Builder datetimes(Map datetimes) { this.datetimes.clear(); - this.datetimes.putAll(Objects.requireNonNull(datetimes, "datetimes cannot be null")); + this.datetimes.putAll( + Preconditions.checkNotNull(datetimes, "datetimes cannot be null")); return this; } public Builder putAllDatetimes(Map datetimes) { - this.datetimes.putAll(Objects.requireNonNull(datetimes, "datetimes cannot be null")); + this.datetimes.putAll( + Preconditions.checkNotNull(datetimes, "datetimes cannot be null")); return this; } @@ -324,12 +329,12 @@ public Builder datetimes(DateTimeAliasExample key, ManyFieldExample value) { @JsonSetter("uuids") public Builder uuids(Map uuids) { this.uuids.clear(); - this.uuids.putAll(Objects.requireNonNull(uuids, "uuids cannot be null")); + this.uuids.putAll(Preconditions.checkNotNull(uuids, "uuids cannot be null")); return this; } public Builder putAllUuids(Map uuids) { - this.uuids.putAll(Objects.requireNonNull(uuids, "uuids cannot be null")); + this.uuids.putAll(Preconditions.checkNotNull(uuids, "uuids cannot be null")); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/AnyExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/AnyExample.java index 15f45f881..86a788e72 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/AnyExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/AnyExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -100,7 +101,7 @@ public Builder from(AnyExample other) { @JsonSetter("any") public Builder any(Object any) { - this.any = Objects.requireNonNull(any, "any cannot be null"); + this.any = Preconditions.checkNotNull(any, "any cannot be null"); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/AnyMapExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/AnyMapExample.java index 2eb2352fd..4f36b7693 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/AnyMapExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/AnyMapExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -104,12 +105,12 @@ public Builder from(AnyMapExample other) { @JsonSetter("items") public Builder items(Map items) { this.items.clear(); - this.items.putAll(Objects.requireNonNull(items, "items cannot be null")); + this.items.putAll(Preconditions.checkNotNull(items, "items cannot be null")); return this; } public Builder putAllItems(Map items) { - this.items.putAll(Objects.requireNonNull(items, "items cannot be null")); + this.items.putAll(Preconditions.checkNotNull(items, "items cannot be null")); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/BearerTokenExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/BearerTokenExample.java index 1c4cbe874..d9426c399 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/BearerTokenExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/BearerTokenExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import com.palantir.tokens.auth.BearerToken; @@ -103,7 +104,7 @@ public Builder from(BearerTokenExample other) { @JsonSetter("bearerTokenValue") public Builder bearerTokenValue(BearerToken bearerTokenValue) { this.bearerTokenValue = - Objects.requireNonNull(bearerTokenValue, "bearerTokenValue cannot be null"); + Preconditions.checkNotNull(bearerTokenValue, "bearerTokenValue cannot be null"); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/BinaryExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/BinaryExample.java index 02c9de391..465d2df52 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/BinaryExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/BinaryExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.nio.ByteBuffer; @@ -101,7 +102,7 @@ public Builder from(BinaryExample other) { @JsonSetter("binary") public Builder binary(ByteBuffer binary) { - Objects.requireNonNull(binary, "binary cannot be null"); + Preconditions.checkNotNull(binary, "binary cannot be null"); this.binary = ByteBuffer.allocate(binary.remaining()).put(binary.duplicate()); this.binary.rewind(); return this; diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/CovariantListExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/CovariantListExample.java index 2f93f4124..e1edad28d 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/CovariantListExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/CovariantListExample.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.palantir.conjure.java.lib.internal.ConjureCollections; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -124,13 +125,13 @@ public Builder from(CovariantListExample other) { public Builder items(Iterable items) { this.items.clear(); ConjureCollections.addAll( - this.items, Objects.requireNonNull(items, "items cannot be null")); + this.items, Preconditions.checkNotNull(items, "items cannot be null")); return this; } public Builder addAllItems(Iterable items) { ConjureCollections.addAll( - this.items, Objects.requireNonNull(items, "items cannot be null")); + this.items, Preconditions.checkNotNull(items, "items cannot be null")); return this; } @@ -144,7 +145,7 @@ public Builder externalItems(Iterable extern this.externalItems.clear(); ConjureCollections.addAll( this.externalItems, - Objects.requireNonNull(externalItems, "externalItems cannot be null")); + Preconditions.checkNotNull(externalItems, "externalItems cannot be null")); return this; } @@ -152,7 +153,7 @@ public Builder addAllExternalItems( Iterable externalItems) { ConjureCollections.addAll( this.externalItems, - Objects.requireNonNull(externalItems, "externalItems cannot be null")); + Preconditions.checkNotNull(externalItems, "externalItems cannot be null")); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/CovariantOptionalExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/CovariantOptionalExample.java index 8f370b703..7f23b5ab9 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/CovariantOptionalExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/CovariantOptionalExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -103,12 +104,12 @@ public Builder from(CovariantOptionalExample other) { @JsonSetter("item") public Builder item(Optional item) { - this.item = (Optional) Objects.requireNonNull(item, "item cannot be null"); + this.item = (Optional) Preconditions.checkNotNull(item, "item cannot be null"); return this; } public Builder item(Object item) { - this.item = Optional.of(Objects.requireNonNull(item, "item cannot be null")); + this.item = Optional.of(Preconditions.checkNotNull(item, "item cannot be null")); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/DateTimeExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/DateTimeExample.java index 19fa3fd61..9385333f4 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/DateTimeExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/DateTimeExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.time.OffsetDateTime; @@ -102,7 +103,7 @@ public Builder from(DateTimeExample other) { @JsonSetter("datetime") public Builder datetime(OffsetDateTime datetime) { - this.datetime = Objects.requireNonNull(datetime, "datetime cannot be null"); + this.datetime = Preconditions.checkNotNull(datetime, "datetime cannot be null"); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/EnumExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/EnumExample.java index 7a5ca8305..087212385 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/EnumExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/EnumExample.java @@ -2,8 +2,8 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; -import java.util.Locale; -import java.util.Objects; +import com.palantir.conjure.java.lib.internal.ConjureEnums; +import com.palantir.logsafe.Preconditions; import javax.annotation.Generated; /** @@ -61,9 +61,8 @@ public int hashCode() { @JsonCreator public static EnumExample valueOf(String value) { - Objects.requireNonNull(value, "value cannot be null"); - String upperCasedValue = value.toUpperCase(Locale.ROOT); - switch (upperCasedValue) { + Preconditions.checkNotNull(value, "value cannot be null"); + switch (value) { case "ONE": return ONE; case "TWO": @@ -71,7 +70,8 @@ public static EnumExample valueOf(String value) { case "ONE_HUNDRED": return ONE_HUNDRED; default: - return new EnumExample(Value.UNKNOWN, upperCasedValue); + ConjureEnums.validate(value); + return new EnumExample(Value.UNKNOWN, value); } } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/EnumFieldExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/EnumFieldExample.java index 29bfb5546..8e2db409e 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/EnumFieldExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/EnumFieldExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -101,7 +102,7 @@ public Builder from(EnumFieldExample other) { @JsonSetter("enum") public Builder enum_(EnumExample enum_) { - this.enum_ = Objects.requireNonNull(enum_, "enum cannot be null"); + this.enum_ = Preconditions.checkNotNull(enum_, "enum cannot be null"); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/ListExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/ListExample.java index 5c64449e1..060dd06a8 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/ListExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/ListExample.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.palantir.conjure.java.lib.internal.ConjureCollections; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -145,13 +146,13 @@ public Builder from(ListExample other) { public Builder items(Iterable items) { this.items.clear(); ConjureCollections.addAll( - this.items, Objects.requireNonNull(items, "items cannot be null")); + this.items, Preconditions.checkNotNull(items, "items cannot be null")); return this; } public Builder addAllItems(Iterable items) { ConjureCollections.addAll( - this.items, Objects.requireNonNull(items, "items cannot be null")); + this.items, Preconditions.checkNotNull(items, "items cannot be null")); return this; } @@ -165,14 +166,14 @@ public Builder primitiveItems(Iterable primitiveItems) { this.primitiveItems.clear(); ConjureCollections.addAll( this.primitiveItems, - Objects.requireNonNull(primitiveItems, "primitiveItems cannot be null")); + Preconditions.checkNotNull(primitiveItems, "primitiveItems cannot be null")); return this; } public Builder addAllPrimitiveItems(Iterable primitiveItems) { ConjureCollections.addAll( this.primitiveItems, - Objects.requireNonNull(primitiveItems, "primitiveItems cannot be null")); + Preconditions.checkNotNull(primitiveItems, "primitiveItems cannot be null")); return this; } @@ -186,14 +187,14 @@ public Builder doubleItems(Iterable doubleItems) { this.doubleItems.clear(); ConjureCollections.addAll( this.doubleItems, - Objects.requireNonNull(doubleItems, "doubleItems cannot be null")); + Preconditions.checkNotNull(doubleItems, "doubleItems cannot be null")); return this; } public Builder addAllDoubleItems(Iterable doubleItems) { ConjureCollections.addAll( this.doubleItems, - Objects.requireNonNull(doubleItems, "doubleItems cannot be null")); + Preconditions.checkNotNull(doubleItems, "doubleItems cannot be null")); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/ManyFieldExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/ManyFieldExample.java index 091492980..3b57d8d57 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/ManyFieldExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/ManyFieldExample.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.palantir.conjure.java.lib.internal.ConjureCollections; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -250,7 +251,7 @@ public Builder from(ManyFieldExample other) { /** docs for string field */ @JsonSetter("string") public Builder string(String string) { - this.string = Objects.requireNonNull(string, "string cannot be null"); + this.string = Preconditions.checkNotNull(string, "string cannot be null"); return this; } @@ -273,7 +274,8 @@ public Builder doubleValue(double doubleValue) { /** docs for optionalItem field */ @JsonSetter("optionalItem") public Builder optionalItem(Optional optionalItem) { - this.optionalItem = Objects.requireNonNull(optionalItem, "optionalItem cannot be null"); + this.optionalItem = + Preconditions.checkNotNull(optionalItem, "optionalItem cannot be null"); return this; } @@ -281,7 +283,8 @@ public Builder optionalItem(Optional optionalItem) { public Builder optionalItem(String optionalItem) { this.optionalItem = Optional.of( - Objects.requireNonNull(optionalItem, "optionalItem cannot be null")); + Preconditions.checkNotNull( + optionalItem, "optionalItem cannot be null")); return this; } @@ -290,14 +293,14 @@ public Builder optionalItem(String optionalItem) { public Builder items(Iterable items) { this.items.clear(); ConjureCollections.addAll( - this.items, Objects.requireNonNull(items, "items cannot be null")); + this.items, Preconditions.checkNotNull(items, "items cannot be null")); return this; } /** docs for items field */ public Builder addAllItems(Iterable items) { ConjureCollections.addAll( - this.items, Objects.requireNonNull(items, "items cannot be null")); + this.items, Preconditions.checkNotNull(items, "items cannot be null")); return this; } @@ -311,13 +314,15 @@ public Builder items(String items) { @JsonSetter("set") public Builder set(Iterable set) { this.set.clear(); - ConjureCollections.addAll(this.set, Objects.requireNonNull(set, "set cannot be null")); + ConjureCollections.addAll( + this.set, Preconditions.checkNotNull(set, "set cannot be null")); return this; } /** docs for set field */ public Builder addAllSet(Iterable set) { - ConjureCollections.addAll(this.set, Objects.requireNonNull(set, "set cannot be null")); + ConjureCollections.addAll( + this.set, Preconditions.checkNotNull(set, "set cannot be null")); return this; } @@ -331,13 +336,13 @@ public Builder set(String set) { @JsonSetter("map") public Builder map(Map map) { this.map.clear(); - this.map.putAll(Objects.requireNonNull(map, "map cannot be null")); + this.map.putAll(Preconditions.checkNotNull(map, "map cannot be null")); return this; } /** docs for map field */ public Builder putAllMap(Map map) { - this.map.putAll(Objects.requireNonNull(map, "map cannot be null")); + this.map.putAll(Preconditions.checkNotNull(map, "map cannot be null")); return this; } @@ -350,7 +355,7 @@ public Builder map(String key, String value) { /** docs for alias field */ @JsonSetter("alias") public Builder alias(StringAliasExample alias) { - this.alias = Objects.requireNonNull(alias, "alias cannot be null"); + this.alias = Preconditions.checkNotNull(alias, "alias cannot be null"); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/MapExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/MapExample.java index e4779d881..3d7ca9684 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/MapExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/MapExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -104,12 +105,12 @@ public Builder from(MapExample other) { @JsonSetter("items") public Builder items(Map items) { this.items.clear(); - this.items.putAll(Objects.requireNonNull(items, "items cannot be null")); + this.items.putAll(Preconditions.checkNotNull(items, "items cannot be null")); return this; } public Builder putAllItems(Map items) { - this.items.putAll(Objects.requireNonNull(items, "items cannot be null")); + this.items.putAll(Preconditions.checkNotNull(items, "items cannot be null")); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/OptionalExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/OptionalExample.java index cedb222d3..14614f90e 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/OptionalExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/OptionalExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -102,12 +103,12 @@ public Builder from(OptionalExample other) { @JsonSetter("item") public Builder item(Optional item) { - this.item = Objects.requireNonNull(item, "item cannot be null"); + this.item = Preconditions.checkNotNull(item, "item cannot be null"); return this; } public Builder item(String item) { - this.item = Optional.of(Objects.requireNonNull(item, "item cannot be null")); + this.item = Optional.of(Preconditions.checkNotNull(item, "item cannot be null")); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/PrimitiveOptionalsExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/PrimitiveOptionalsExample.java index f6547635e..166627569 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/PrimitiveOptionalsExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/PrimitiveOptionalsExample.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.palantir.conjure.java.lib.SafeLong; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import com.palantir.ri.ResourceIdentifier; @@ -221,7 +222,7 @@ public Builder from(PrimitiveOptionalsExample other) { @JsonSetter("num") public Builder num(OptionalDouble num) { - this.num = Objects.requireNonNull(num, "num cannot be null"); + this.num = Preconditions.checkNotNull(num, "num cannot be null"); return this; } @@ -232,7 +233,7 @@ public Builder num(double num) { @JsonSetter("bool") public Builder bool(Optional bool) { - this.bool = Objects.requireNonNull(bool, "bool cannot be null"); + this.bool = Preconditions.checkNotNull(bool, "bool cannot be null"); return this; } @@ -243,7 +244,7 @@ public Builder bool(boolean bool) { @JsonSetter("integer") public Builder integer(OptionalInt integer) { - this.integer = Objects.requireNonNull(integer, "integer cannot be null"); + this.integer = Preconditions.checkNotNull(integer, "integer cannot be null"); return this; } @@ -254,47 +255,49 @@ public Builder integer(int integer) { @JsonSetter("safelong") public Builder safelong(Optional safelong) { - this.safelong = Objects.requireNonNull(safelong, "safelong cannot be null"); + this.safelong = Preconditions.checkNotNull(safelong, "safelong cannot be null"); return this; } public Builder safelong(SafeLong safelong) { this.safelong = - Optional.of(Objects.requireNonNull(safelong, "safelong cannot be null")); + Optional.of(Preconditions.checkNotNull(safelong, "safelong cannot be null")); return this; } @JsonSetter("rid") public Builder rid(Optional rid) { - this.rid = Objects.requireNonNull(rid, "rid cannot be null"); + this.rid = Preconditions.checkNotNull(rid, "rid cannot be null"); return this; } public Builder rid(ResourceIdentifier rid) { - this.rid = Optional.of(Objects.requireNonNull(rid, "rid cannot be null")); + this.rid = Optional.of(Preconditions.checkNotNull(rid, "rid cannot be null")); return this; } @JsonSetter("bearertoken") public Builder bearertoken(Optional bearertoken) { - this.bearertoken = Objects.requireNonNull(bearertoken, "bearertoken cannot be null"); + this.bearertoken = + Preconditions.checkNotNull(bearertoken, "bearertoken cannot be null"); return this; } public Builder bearertoken(BearerToken bearertoken) { this.bearertoken = - Optional.of(Objects.requireNonNull(bearertoken, "bearertoken cannot be null")); + Optional.of( + Preconditions.checkNotNull(bearertoken, "bearertoken cannot be null")); return this; } @JsonSetter("uuid") public Builder uuid(Optional uuid) { - this.uuid = Objects.requireNonNull(uuid, "uuid cannot be null"); + this.uuid = Preconditions.checkNotNull(uuid, "uuid cannot be null"); return this; } public Builder uuid(UUID uuid) { - this.uuid = Optional.of(Objects.requireNonNull(uuid, "uuid cannot be null")); + this.uuid = Optional.of(Preconditions.checkNotNull(uuid, "uuid cannot be null")); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/ReservedKeyExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/ReservedKeyExample.java index dc9dd008c..342165956 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/ReservedKeyExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/ReservedKeyExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -180,20 +181,20 @@ public Builder from(ReservedKeyExample other) { @JsonSetter("package") public Builder package_(String package_) { - this.package_ = Objects.requireNonNull(package_, "package cannot be null"); + this.package_ = Preconditions.checkNotNull(package_, "package cannot be null"); return this; } @JsonSetter("interface") public Builder interface_(String interface_) { - this.interface_ = Objects.requireNonNull(interface_, "interface cannot be null"); + this.interface_ = Preconditions.checkNotNull(interface_, "interface cannot be null"); return this; } @JsonSetter("field-name-with-dashes") public Builder fieldNameWithDashes(String fieldNameWithDashes) { this.fieldNameWithDashes = - Objects.requireNonNull( + Preconditions.checkNotNull( fieldNameWithDashes, "field-name-with-dashes cannot be null"); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/RidExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/RidExample.java index aed9a6179..4f937d249 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/RidExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/RidExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import com.palantir.ri.ResourceIdentifier; @@ -101,7 +102,7 @@ public Builder from(RidExample other) { @JsonSetter("ridValue") public Builder ridValue(ResourceIdentifier ridValue) { - this.ridValue = Objects.requireNonNull(ridValue, "ridValue cannot be null"); + this.ridValue = Preconditions.checkNotNull(ridValue, "ridValue cannot be null"); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/SafeLongExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/SafeLongExample.java index 40da1b29e..718398281 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/SafeLongExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/SafeLongExample.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.palantir.conjure.java.lib.SafeLong; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -103,7 +104,7 @@ public Builder from(SafeLongExample other) { @JsonSetter("safeLongValue") public Builder safeLongValue(SafeLong safeLongValue) { this.safeLongValue = - Objects.requireNonNull(safeLongValue, "safeLongValue cannot be null"); + Preconditions.checkNotNull(safeLongValue, "safeLongValue cannot be null"); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/SetExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/SetExample.java index a3a5b3d6f..f64c8d161 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/SetExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/SetExample.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.palantir.conjure.java.lib.internal.ConjureCollections; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -122,13 +123,13 @@ public Builder from(SetExample other) { public Builder items(Iterable items) { this.items.clear(); ConjureCollections.addAll( - this.items, Objects.requireNonNull(items, "items cannot be null")); + this.items, Preconditions.checkNotNull(items, "items cannot be null")); return this; } public Builder addAllItems(Iterable items) { ConjureCollections.addAll( - this.items, Objects.requireNonNull(items, "items cannot be null")); + this.items, Preconditions.checkNotNull(items, "items cannot be null")); return this; } @@ -142,14 +143,14 @@ public Builder doubleItems(Iterable doubleItems) { this.doubleItems.clear(); ConjureCollections.addAll( this.doubleItems, - Objects.requireNonNull(doubleItems, "doubleItems cannot be null")); + Preconditions.checkNotNull(doubleItems, "doubleItems cannot be null")); return this; } public Builder addAllDoubleItems(Iterable doubleItems) { ConjureCollections.addAll( this.doubleItems, - Objects.requireNonNull(doubleItems, "doubleItems cannot be null")); + Preconditions.checkNotNull(doubleItems, "doubleItems cannot be null")); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/SimpleEnum.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/SimpleEnum.java index 27d83a523..304bbc56d 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/SimpleEnum.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/SimpleEnum.java @@ -2,8 +2,8 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; -import java.util.Locale; -import java.util.Objects; +import com.palantir.conjure.java.lib.internal.ConjureEnums; +import com.palantir.logsafe.Preconditions; import javax.annotation.Generated; /** @@ -53,13 +53,13 @@ public int hashCode() { @JsonCreator public static SimpleEnum valueOf(String value) { - Objects.requireNonNull(value, "value cannot be null"); - String upperCasedValue = value.toUpperCase(Locale.ROOT); - switch (upperCasedValue) { + Preconditions.checkNotNull(value, "value cannot be null"); + switch (value) { case "VALUE": return VALUE; default: - return new SimpleEnum(Value.UNKNOWN, upperCasedValue); + ConjureEnums.validate(value); + return new SimpleEnum(Value.UNKNOWN, value); } } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/SingleUnion.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/SingleUnion.java index 8bcebfaa3..ed98b041b 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/SingleUnion.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/SingleUnion.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.annotation.JsonValue; +import com.palantir.logsafe.Preconditions; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -88,7 +89,7 @@ private static class FooWrapper implements Base { @JsonCreator private FooWrapper(@JsonProperty("foo") String value) { - Objects.requireNonNull(value, "foo cannot be null"); + Preconditions.checkNotNull(value, "foo cannot be null"); this.value = value; } @@ -139,8 +140,8 @@ private UnknownWrapper(@JsonProperty("type") String type) { } private UnknownWrapper(String type, Map value) { - Objects.requireNonNull(type, "type cannot be null"); - Objects.requireNonNull(value, "value cannot be null"); + Preconditions.checkNotNull(type, "type cannot be null"); + Preconditions.checkNotNull(value, "value cannot be null"); this.type = type; this.value = value; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/StringExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/StringExample.java index 7e7b9a42a..7f2ee7361 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/StringExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/StringExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -100,7 +101,7 @@ public Builder from(StringExample other) { @JsonSetter("string") public Builder string(String string) { - this.string = Objects.requireNonNull(string, "string cannot be null"); + this.string = Preconditions.checkNotNull(string, "string cannot be null"); return this; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/Union.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/Union.java index 19b62e33f..f0dc02664 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/Union.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/Union.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.annotation.JsonValue; +import com.palantir.logsafe.Preconditions; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -96,7 +97,7 @@ private static class FooWrapper implements Base { @JsonCreator private FooWrapper(@JsonProperty("foo") String value) { - Objects.requireNonNull(value, "foo cannot be null"); + Preconditions.checkNotNull(value, "foo cannot be null"); this.value = value; } @@ -137,7 +138,7 @@ private static class BarWrapper implements Base { @JsonCreator private BarWrapper(@JsonProperty("bar") int value) { - Objects.requireNonNull(value, "bar cannot be null"); + Preconditions.checkNotNull(value, "bar cannot be null"); this.value = value; } @@ -188,8 +189,8 @@ private UnknownWrapper(@JsonProperty("type") String type) { } private UnknownWrapper(String type, Map value) { - Objects.requireNonNull(type, "type cannot be null"); - Objects.requireNonNull(value, "value cannot be null"); + Preconditions.checkNotNull(type, "type cannot be null"); + Preconditions.checkNotNull(value, "value cannot be null"); this.type = type; this.value = value; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/UnionTypeExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/UnionTypeExample.java index 2d8b593f7..213c6e90d 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/UnionTypeExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/UnionTypeExample.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.annotation.JsonValue; +import com.palantir.logsafe.Preconditions; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -148,7 +149,7 @@ private static class StringExampleWrapper implements Base { @JsonCreator private StringExampleWrapper(@JsonProperty("stringExample") StringExample value) { - Objects.requireNonNull(value, "stringExample cannot be null"); + Preconditions.checkNotNull(value, "stringExample cannot be null"); this.value = value; } @@ -191,7 +192,7 @@ private static class SetWrapper implements Base { @JsonCreator private SetWrapper(@JsonProperty("set") Set value) { - Objects.requireNonNull(value, "set cannot be null"); + Preconditions.checkNotNull(value, "set cannot be null"); this.value = value; } @@ -232,7 +233,7 @@ private static class ThisFieldIsAnIntegerWrapper implements Base { @JsonCreator private ThisFieldIsAnIntegerWrapper(@JsonProperty("thisFieldIsAnInteger") int value) { - Objects.requireNonNull(value, "thisFieldIsAnInteger cannot be null"); + Preconditions.checkNotNull(value, "thisFieldIsAnInteger cannot be null"); this.value = value; } @@ -275,7 +276,7 @@ private static class AlsoAnIntegerWrapper implements Base { @JsonCreator private AlsoAnIntegerWrapper(@JsonProperty("alsoAnInteger") int value) { - Objects.requireNonNull(value, "alsoAnInteger cannot be null"); + Preconditions.checkNotNull(value, "alsoAnInteger cannot be null"); this.value = value; } @@ -318,7 +319,7 @@ private static class IfWrapper implements Base { @JsonCreator private IfWrapper(@JsonProperty("if") int value) { - Objects.requireNonNull(value, "if cannot be null"); + Preconditions.checkNotNull(value, "if cannot be null"); this.value = value; } @@ -359,7 +360,7 @@ private static class NewWrapper implements Base { @JsonCreator private NewWrapper(@JsonProperty("new") int value) { - Objects.requireNonNull(value, "new cannot be null"); + Preconditions.checkNotNull(value, "new cannot be null"); this.value = value; } @@ -400,7 +401,7 @@ private static class InterfaceWrapper implements Base { @JsonCreator private InterfaceWrapper(@JsonProperty("interface") int value) { - Objects.requireNonNull(value, "interface cannot be null"); + Preconditions.checkNotNull(value, "interface cannot be null"); this.value = value; } @@ -452,8 +453,8 @@ private UnknownWrapper(@JsonProperty("type") String type) { } private UnknownWrapper(String type, Map value) { - Objects.requireNonNull(type, "type cannot be null"); - Objects.requireNonNull(value, "value cannot be null"); + Preconditions.checkNotNull(type, "type cannot be null"); + Preconditions.checkNotNull(value, "value cannot be null"); this.type = type; this.value = value; } diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/UuidExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/UuidExample.java index da0c24f8b..93abf2362 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/UuidExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/UuidExample.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.ArrayList; @@ -101,7 +102,7 @@ public Builder from(UuidExample other) { @JsonSetter("uuid") public Builder uuid(UUID uuid) { - this.uuid = Objects.requireNonNull(uuid, "uuid cannot be null"); + this.uuid = Preconditions.checkNotNull(uuid, "uuid cannot be null"); return this; } 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 ede773391..12db2513a 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 @@ -22,6 +22,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.palantir.conjure.java.ConjureAnnotations; +import com.palantir.conjure.java.lib.internal.ConjureEnums; import com.palantir.conjure.spec.EnumDefinition; import com.palantir.conjure.spec.EnumValueDefinition; import com.squareup.javapoet.ClassName; @@ -35,7 +36,6 @@ import com.squareup.javapoet.TypeSpec; import com.squareup.javapoet.TypeVariableName; import java.util.List; -import java.util.Locale; import javax.lang.model.element.Modifier; import org.apache.commons.lang3.StringUtils; @@ -66,7 +66,7 @@ private static TypeSpec createSafeEnum( TypeSpec.Builder wrapper = TypeSpec.classBuilder(typeDef.getTypeName().getName()) .addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(EnumGenerator.class)) .addModifiers(Modifier.PUBLIC, Modifier.FINAL) - .addType(createEnum(enumClass, typeDef.getValues(), true)) + .addType(createEnum(enumClass, typeDef.getValues())) .addType(createVisitor(visitorClass, typeDef.getValues())) .addField(enumClass, VALUE_PARAMETER, Modifier.PRIVATE, Modifier.FINAL) .addField(ClassName.get(String.class), STRING_PARAMETER, Modifier.PRIVATE, Modifier.FINAL) @@ -123,7 +123,7 @@ private static Iterable createConstants(Iterable }); } - private static TypeSpec createEnum(ClassName enumClass, Iterable values, boolean withUnknown) { + private static TypeSpec createEnum(ClassName enumClass, Iterable values) { TypeSpec.Builder enumBuilder = TypeSpec.enumBuilder(enumClass.simpleName()) .addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(EnumGenerator.class)) .addModifiers(Modifier.PUBLIC); @@ -133,19 +133,7 @@ private static TypeSpec createEnum(ClassName enumClass, Iterable values) { @@ -217,7 +205,7 @@ private static MethodSpec createValueOf(ClassName thisClass, Iterable args) { diff --git a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/WireFormatTests.java b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/WireFormatTests.java index d317b0120..1e8fd7df2 100644 --- a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/WireFormatTests.java +++ b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/WireFormatTests.java @@ -4,12 +4,18 @@ package com.palantir.conjure.java.types; +import static com.palantir.logsafe.testing.Assertions.assertThatLoggableExceptionThrownBy; import static org.assertj.core.api.Assertions.assertThat; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Throwables; import com.google.common.collect.ImmutableSet; import com.palantir.conjure.java.serialization.ObjectMappers; +import com.palantir.logsafe.SafeLoggable; +import com.palantir.logsafe.UnsafeArg; +import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; +import com.palantir.logsafe.testing.LoggableExceptionAssert; import com.palantir.product.BinaryAliasExample; import com.palantir.product.BinaryExample; import com.palantir.product.DateTimeExample; @@ -32,6 +38,7 @@ import java.time.ZoneOffset; import java.util.Set; import java.util.UUID; +import org.assertj.core.api.ThrowableAssert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -154,9 +161,30 @@ public void testEmptyObjectsDeserialize() throws Exception { @Test public void testEnumCasingDeserializationInvariantToInputCase() throws Exception { assertThat(mapper.readValue("\"ONE\"", EnumExample.class)).isEqualTo(EnumExample.ONE); - assertThat(mapper.readValue("\"one\"", EnumExample.class)).isEqualTo(EnumExample.ONE); - assertThat(mapper.readValue("\"onE\"", EnumExample.class)).isEqualTo(EnumExample.ONE); - assertThat(mapper.readValue("\"oNE\"", EnumExample.class)).isEqualTo(EnumExample.ONE); + assertThat(mapper.readValue("\"ONE_HUNDRED\"", EnumExample.class)).isEqualTo(EnumExample.ONE_HUNDRED); + assertThatExceptionThrownRootCause(() -> mapper.readValue("\"one\"", EnumExample.class)) + .isInstanceOf(SafeIllegalArgumentException.class) + .hasLogMessage("Enum values must use SCREAMING_SNAKE_CASE") + .hasExactlyArgs(UnsafeArg.of("value", "one")); + assertThatExceptionThrownRootCause(() -> mapper.readValue("\"onE\"", EnumExample.class)) + .isInstanceOf(SafeIllegalArgumentException.class) + .hasLogMessage("Enum values must use SCREAMING_SNAKE_CASE") + .hasExactlyArgs(UnsafeArg.of("value", "onE")); + assertThatExceptionThrownRootCause(() -> mapper.readValue("\"oNE\"", EnumExample.class)) + .isInstanceOf(SafeIllegalArgumentException.class) + .hasLogMessage("Enum values must use SCREAMING_SNAKE_CASE") + .hasExactlyArgs(UnsafeArg.of("value", "oNE")); + } + + private LoggableExceptionAssert assertThatExceptionThrownRootCause( + ThrowableAssert.ThrowingCallable task) { + return assertThatLoggableExceptionThrownBy(() -> { + try { + task.call(); + } catch (Throwable t) { + throw Throwables.getRootCause(t); + } + }); } @Test diff --git a/conjure-lib/src/main/java/com/palantir/conjure/java/lib/internal/ConjureEnums.java b/conjure-lib/src/main/java/com/palantir/conjure/java/lib/internal/ConjureEnums.java new file mode 100644 index 000000000..a0a307aa3 --- /dev/null +++ b/conjure-lib/src/main/java/com/palantir/conjure/java/lib/internal/ConjureEnums.java @@ -0,0 +1,52 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.conjure.java.lib.internal; + +import com.palantir.logsafe.UnsafeArg; +import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; +import java.util.BitSet; + +/** + * Internal utility functions for conjure enum types. + */ +public final class ConjureEnums { + + private static final BitSet allowedCharacters = new BitSet(); + + static { + allowedCharacters.set('A', 'Z' + 1); + allowedCharacters.set('_'); + } + + private ConjureEnums() { + // cannot instantiate + } + + public static void validate(String value) { + if (value.isEmpty()) { + throw new SafeIllegalArgumentException("Enum values must not be empty"); + } + + final int length = value.length(); + for (int index = 0; index < length; index++) { + if (!allowedCharacters.get(value.charAt(index))) { + throw new SafeIllegalArgumentException("Enum values must use SCREAMING_SNAKE_CASE", + UnsafeArg.of("value", value)); + } + } + } +}