From a1e187a86ec8b6c5a58739e98f29a68663464401 Mon Sep 17 00:00:00 2001 From: Andrew Ash Date: Thu, 29 Feb 2024 01:30:12 -0800 Subject: [PATCH 1/2] SafeLoggable exception for invalid UUID values --- .../com/palantir/product/UuidAliasExample.java | 8 +++++++- .../com/palantir/product/UuidAliasExample.java | 8 +++++++- .../conjure/java/types/AliasGenerator.java | 9 +++++++++ .../conjure/java/types/AliasTests.java | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/UuidAliasExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/UuidAliasExample.java index 9f9b9dd0c..85de7e2a5 100644 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/UuidAliasExample.java +++ b/conjure-java-core/src/integrationInput/java/com/palantir/product/UuidAliasExample.java @@ -3,6 +3,8 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; import com.palantir.logsafe.Preconditions; +import com.palantir.logsafe.UnsafeArg; +import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.UUID; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -38,7 +40,11 @@ public int hashCode() { } public static UuidAliasExample valueOf(String value) { - return of(UUID.fromString(value)); + try { + return of(UUID.fromString(value)); + } catch (IllegalArgumentException e) { + throw new SafeIllegalArgumentException("Unable to parse as UUID", e, UnsafeArg.of("input", value)); + } } @JsonCreator(mode = JsonCreator.Mode.DELEGATING) diff --git a/conjure-java-core/src/integrationInput/java/test/prefix/com/palantir/product/UuidAliasExample.java b/conjure-java-core/src/integrationInput/java/test/prefix/com/palantir/product/UuidAliasExample.java index c2f7a7e61..c25087b04 100644 --- a/conjure-java-core/src/integrationInput/java/test/prefix/com/palantir/product/UuidAliasExample.java +++ b/conjure-java-core/src/integrationInput/java/test/prefix/com/palantir/product/UuidAliasExample.java @@ -3,6 +3,8 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; import com.palantir.logsafe.Preconditions; +import com.palantir.logsafe.UnsafeArg; +import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.util.UUID; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -38,7 +40,11 @@ public int hashCode() { } public static UuidAliasExample valueOf(String value) { - return of(UUID.fromString(value)); + try { + return of(UUID.fromString(value)); + } catch (IllegalArgumentException e) { + throw new SafeIllegalArgumentException("Unable to parse as UUID", e, UnsafeArg.of("input", value)); + } } @JsonCreator(mode = JsonCreator.Mode.DELEGATING) diff --git a/conjure-java-core/src/main/java/com/palantir/conjure/java/types/AliasGenerator.java b/conjure-java-core/src/main/java/com/palantir/conjure/java/types/AliasGenerator.java index 6e7f31dbc..f60dd6449 100644 --- a/conjure-java-core/src/main/java/com/palantir/conjure/java/types/AliasGenerator.java +++ b/conjure-java-core/src/main/java/com/palantir/conjure/java/types/AliasGenerator.java @@ -39,6 +39,8 @@ import com.palantir.conjure.visitor.TypeVisitor; import com.palantir.logsafe.Preconditions; import com.palantir.logsafe.Safe; +import com.palantir.logsafe.UnsafeArg; +import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import com.squareup.javapoet.AnnotationSpec; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; @@ -385,7 +387,14 @@ private static CodeBlock valueOfFactoryMethodForPrimitive(PrimitiveType primitiv .build(); case UUID: return CodeBlock.builder() + .beginControlFlow("try") .addStatement("return of($T.fromString(value))", aliasTypeName.withoutAnnotations()) + .nextControlFlow("catch (IllegalArgumentException e)") + .addStatement( + "throw new $T(\"Unable to parse as UUID\", e, $T.of(\"input\", value))", + SafeIllegalArgumentException.class, + UnsafeArg.class) + .endControlFlow() .build(); case DATETIME: return CodeBlock.builder() diff --git a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/AliasTests.java b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/AliasTests.java index 3dc2623a1..3f507ab15 100644 --- a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/AliasTests.java +++ b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/AliasTests.java @@ -16,17 +16,21 @@ package com.palantir.conjure.java.types; +import static com.palantir.logsafe.testing.Assertions.assertThatLoggableExceptionThrownBy; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.palantir.conjure.java.serialization.ObjectMappers; +import com.palantir.logsafe.UnsafeArg; +import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import com.palantir.logsafe.exceptions.SafeNullPointerException; import com.palantir.product.DoubleAliasExample; import com.palantir.product.ExternalLongAliasOne; import com.palantir.product.ExternalLongAliasTwo; import com.palantir.product.UuidAliasExample; +import java.util.UUID; import org.junit.jupiter.api.Test; public class AliasTests { @@ -39,6 +43,20 @@ public void testNullValueSafeLoggable() { .hasMessage("value cannot be null"); } + @Test + void testValueOf_validUuid_succeeds() { + UUID uuid = UUID.randomUUID(); + assertThat(UuidAliasExample.valueOf(uuid.toString())).isEqualTo(UuidAliasExample.of(uuid)); + } + + @Test + void testValueOf_invalidUuid_throwsSafeException() { + assertThatLoggableExceptionThrownBy(() -> UuidAliasExample.valueOf("not-a-uuid")) + .isInstanceOf(SafeIllegalArgumentException.class) + .hasMessageContaining("Unable to parse as UUID") + .hasExactlyArgs(UnsafeArg.of("input", "not-a-uuid")); + } + @Test public void testValueOf_external() { assertThat(ExternalLongAliasOne.valueOf("123")).isEqualTo(ExternalLongAliasOne.of(123L)); From 2ad5a433bf2355461253a0888ce7eccc27ec7cd3 Mon Sep 17 00:00:00 2001 From: svc-changelog Date: Thu, 29 Feb 2024 16:33:43 +0000 Subject: [PATCH 2/2] Add generated changelog entries --- changelog/@unreleased/pr-2212.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-2212.v2.yml diff --git a/changelog/@unreleased/pr-2212.v2.yml b/changelog/@unreleased/pr-2212.v2.yml new file mode 100644 index 000000000..75fa64602 --- /dev/null +++ b/changelog/@unreleased/pr-2212.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: SafeLoggable exception for invalid UUID values + links: + - https://github.com/palantir/conjure-java/pull/2212