diff --git a/dialogue-serde/src/main/java/com/palantir/conjure/java/dialogue/serde/ConjureBodySerDe.java b/dialogue-serde/src/main/java/com/palantir/conjure/java/dialogue/serde/ConjureBodySerDe.java index 5b87aa02d..fcdd84555 100644 --- a/dialogue-serde/src/main/java/com/palantir/conjure/java/dialogue/serde/ConjureBodySerDe.java +++ b/dialogue-serde/src/main/java/com/palantir/conjure/java/dialogue/serde/ConjureBodySerDe.java @@ -82,16 +82,16 @@ final class ConjureBodySerDe implements BodySerDe { emptyContainerDeserializer, BinaryEncoding.MARKER, DeserializerArgs.builder() - .withBaseType(BinaryEncoding.MARKER) - .withExpectedResult(BinaryEncoding.MARKER) + .baseType(BinaryEncoding.MARKER) + .success(BinaryEncoding.MARKER) .build()); this.optionalBinaryInputStreamDeserializer = new EncodingDeserializerForEndpointRegistry<>( ImmutableList.of(BinaryEncoding.INSTANCE), emptyContainerDeserializer, BinaryEncoding.OPTIONAL_MARKER, DeserializerArgs.>builder() - .withBaseType(BinaryEncoding.OPTIONAL_MARKER) - .withExpectedResult(BinaryEncoding.OPTIONAL_MARKER) + .baseType(BinaryEncoding.OPTIONAL_MARKER) + .success(BinaryEncoding.OPTIONAL_MARKER) .build()); this.emptyBodyDeserializer = new EmptyBodyDeserializer(new EndpointErrorDecoder<>(Collections.emptyMap(), Optional.empty())); @@ -108,8 +108,8 @@ private EncodingDeserializerForEndpointRegistry buildCacheEntry(TypeMarke emptyContainerDeserializer, typeMarker, DeserializerArgs.builder() - .withBaseType(typeMarker) - .withExpectedResult(typeMarker) + .baseType(typeMarker) + .success(typeMarker) .build()); } diff --git a/dialogue-serde/src/main/java/com/palantir/conjure/java/dialogue/serde/EndpointErrorDecoder.java b/dialogue-serde/src/main/java/com/palantir/conjure/java/dialogue/serde/EndpointErrorDecoder.java index c93776e6d..cb158256b 100644 --- a/dialogue-serde/src/main/java/com/palantir/conjure/java/dialogue/serde/EndpointErrorDecoder.java +++ b/dialogue-serde/src/main/java/com/palantir/conjure/java/dialogue/serde/EndpointErrorDecoder.java @@ -19,6 +19,8 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; import com.google.common.io.CharStreams; import com.google.common.net.HttpHeaders; import com.google.common.primitives.Longs; @@ -53,7 +55,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; /** * Extracts the error from a {@link Response}. @@ -71,10 +72,8 @@ final class EndpointErrorDecoder { EndpointErrorDecoder( Map> errorNameToTypeMap, Optional maybeJsonEncoding) { this.errorNameToJsonDeserializerMap = maybeJsonEncoding - .>>map( - jsonEncoding -> errorNameToTypeMap.entrySet().stream() - .collect(Collectors.toMap( - Map.Entry::getKey, entry -> jsonEncoding.deserializer(entry.getValue())))) + .>>map(jsonEncoding -> + ImmutableMap.copyOf(Maps.transformValues(errorNameToTypeMap, jsonEncoding::deserializer))) .orElseGet(Collections::emptyMap); } diff --git a/dialogue-serde/src/test/java/com/palantir/conjure/java/dialogue/serde/EndpointErrorsConjureBodySerDeTest.java b/dialogue-serde/src/test/java/com/palantir/conjure/java/dialogue/serde/EndpointErrorsConjureBodySerDeTest.java index a08c84806..cde06f5fa 100644 --- a/dialogue-serde/src/test/java/com/palantir/conjure/java/dialogue/serde/EndpointErrorsConjureBodySerDeTest.java +++ b/dialogue-serde/src/test/java/com/palantir/conjure/java/dialogue/serde/EndpointErrorsConjureBodySerDeTest.java @@ -116,9 +116,9 @@ public void testDeserializeCustomError() throws IOException { .code(500); BodySerDe serializers = conjureBodySerDe("application/json", "text/plain"); DeserializerArgs deserializerArgs = DeserializerArgs.builder() - .withBaseType(new TypeMarker<>() {}) - .withExpectedResult(new TypeMarker() {}) - .withErrorType("Default:FailedPrecondition", new TypeMarker() {}) + .baseType(new TypeMarker<>() {}) + .success(new TypeMarker() {}) + .error("Default:FailedPrecondition", new TypeMarker() {}) .build(); // When @@ -155,8 +155,8 @@ public void testDeserializingUndefinedErrorFallsbackToSerializableError() throws .code(500); BodySerDe serializers = conjureBodySerDe("application/json", "text/plain"); DeserializerArgs deserializerArgs = DeserializerArgs.builder() - .withBaseType(new TypeMarker<>() {}) - .withExpectedResult(new TypeMarker() {}) + .baseType(new TypeMarker<>() {}) + .success(new TypeMarker() {}) // Note: no error types are registered. .build(); @@ -190,9 +190,9 @@ public void testDeserializeExpectedValue() { .code(200); BodySerDe serializers = conjureBodySerDe("application/json", "text/plain"); DeserializerArgs deserializerArgs = DeserializerArgs.builder() - .withBaseType(new TypeMarker<>() {}) - .withExpectedResult(new TypeMarker() {}) - .withErrorType("Default:FailedPrecondition", new TypeMarker() {}) + .baseType(new TypeMarker<>() {}) + .success(new TypeMarker() {}) + .error("Default:FailedPrecondition", new TypeMarker() {}) .build(); // When EndpointReturnBaseType value = diff --git a/dialogue-target/src/main/java/com/palantir/dialogue/DeserializerArgs.java b/dialogue-target/src/main/java/com/palantir/dialogue/DeserializerArgs.java index c2690ef79..80fdb60eb 100644 --- a/dialogue-target/src/main/java/com/palantir/dialogue/DeserializerArgs.java +++ b/dialogue-target/src/main/java/com/palantir/dialogue/DeserializerArgs.java @@ -21,6 +21,7 @@ import java.util.HashMap; import java.util.Map; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public final class DeserializerArgs { private final TypeMarker baseType; @@ -42,59 +43,48 @@ public static Builder builder() { public static final class Builder { private boolean buildInvoked = false; - private TypeMarker baseType; - private boolean baseTypeSet = false; - private TypeMarker expectedResultType; - private boolean expectedResultSet = false; + private @Nullable TypeMarker baseType; + private @Nullable TypeMarker successType; private final Map> errorNameToTypeMarker; - @SuppressWarnings("NullAway") - // We ensure that the baseType and expectedResultType are set before building. private Builder() { this.errorNameToTypeMarker = new HashMap<>(); } - public Builder withBaseType(@Nonnull TypeMarker base) { + public Builder baseType(@Nonnull TypeMarker baseT) { checkNotBuilt(); - this.baseType = Preconditions.checkNotNull(base, "base type must be non-null"); - this.baseTypeSet = true; + this.baseType = Preconditions.checkNotNull(baseT, "base type must be non-null"); return this; } - public Builder withExpectedResult(TypeMarker expectedResultT) { + public Builder success(@Nonnull TypeMarker successT) { checkNotBuilt(); - this.expectedResultType = - Preconditions.checkNotNull(expectedResultT, "expected result type must be non-null"); - this.expectedResultSet = true; + this.successType = Preconditions.checkNotNull(successT, "success type must be non-null"); return this; } - public Builder withErrorType(@Nonnull String errorName, @Nonnull TypeMarker errorType) { + public Builder error(@Nonnull String errorName, @Nonnull TypeMarker errorT) { checkNotBuilt(); this.errorNameToTypeMarker.put( Preconditions.checkNotNull(errorName, "error name must be non-null"), - Preconditions.checkNotNull(errorType, "error type must be non-null")); + Preconditions.checkNotNull(errorT, "error type must be non-null")); return this; } public DeserializerArgs build() { checkNotBuilt(); - checkRequiredArgsSet(); + Preconditions.checkNotNull(baseType, "base type must be set"); + Preconditions.checkNotNull(successType, "success type must be set"); buildInvoked = true; - return new DeserializerArgs<>(baseType, expectedResultType, ImmutableMap.copyOf(errorNameToTypeMarker)); + return new DeserializerArgs<>(baseType, successType, ImmutableMap.copyOf(errorNameToTypeMarker)); } private void checkNotBuilt() { - Preconditions.checkState(!buildInvoked, "Build has already been called"); - } - - private void checkRequiredArgsSet() { - Preconditions.checkState(baseTypeSet, "base type must be set"); - Preconditions.checkState(expectedResultSet, "expected result type must be set"); + Preconditions.checkState(!buildInvoked, "build has already been called"); } } - public TypeMarker baseType() { + public TypeMarker baseType() { return baseType; }