Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read safety of external types #1945

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ public Optional<LogSafety> visitReference(TypeName value) {
}

@Override
public Optional<LogSafety> visitExternal(ExternalReference _value) {
// External types have unknown safety for now
return Optional.empty();
public Optional<LogSafety> visitExternal(ExternalReference value) {
return value.getSafety();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.Iterables;
import com.palantir.conjure.defs.SafetyDeclarationRequirements;
import com.palantir.conjure.defs.validator.ConjureDefinitionValidator;
import com.palantir.conjure.spec.AliasDefinition;
import com.palantir.conjure.spec.ConjureDefinition;
import com.palantir.conjure.spec.Documentation;
import com.palantir.conjure.spec.EnumDefinition;
import com.palantir.conjure.spec.ExternalReference;
import com.palantir.conjure.spec.FieldDefinition;
import com.palantir.conjure.spec.FieldName;
import com.palantir.conjure.spec.LogSafety;
Expand All @@ -35,7 +37,12 @@
import com.palantir.conjure.spec.TypeName;
import com.palantir.conjure.spec.UnionDefinition;
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class SafetyEvaluatorTest {
private static final String PACKAGE = "package";
Expand Down Expand Up @@ -130,11 +137,45 @@ void testMapSafeKey() {
.types(object)
.types(SAFE_ALIAS)
.build();
ConjureDefinitionValidator.validateAll(conjureDef);
// there's an ABI break between 4.28.0 -> 4.36.0
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(object)).isEmpty();
}

private static Stream<Arguments> providesExternalRefTypes_ImportTime() {
Type external = Type.external(ExternalReference.builder()
.externalReference(TypeName.of("Long", "java.lang"))
.fallback(Type.primitive(PrimitiveType.STRING))
.safety(LogSafety.DO_NOT_LOG)
.build());
return getTypes(external);
}

@ParameterizedTest
@MethodSource("providesExternalRefTypes_ImportTime")
void testExternalRefType_AtImportTime(TypeDefinition typeDefinition, ConjureDefinition conjureDef) {
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(typeDefinition)).hasValue(LogSafety.DO_NOT_LOG);
}

private static Stream<Arguments> providesExternalRefTypes_NoSafety() {
Type external = Type.external(ExternalReference.builder()
.externalReference(TypeName.of("Long", "java.lang"))
.fallback(Type.primitive(PrimitiveType.STRING))
.build());
return getTypes(external);
}

@ParameterizedTest
@MethodSource("providesExternalRefTypes_NoSafety")
void testExternalRefType_NoSafety(TypeDefinition typeDefinition, ConjureDefinition conjureDef) {
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(typeDefinition)).isEmpty();
}

@Test
void testMapSafetyUnsafeValue() {
TypeDefinition object = TypeDefinition.object(ObjectDefinition.builder()
Expand All @@ -150,7 +191,7 @@ void testMapSafetyUnsafeValue() {
.types(SAFE_ALIAS)
.types(UNSAFE_ALIAS)
.build();
ConjureDefinitionValidator.validateAll(conjureDef);
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method changed between the previous conjure version and the one I bumped to

SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(object)).hasValue(LogSafety.UNSAFE);
}
Expand All @@ -170,7 +211,7 @@ void testMapSafetySafeKeyAndValue() {
.types(SAFE_ALIAS)
.types(UNSAFE_ALIAS)
.build();
ConjureDefinitionValidator.validateAll(conjureDef);
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(object)).hasValue(LogSafety.SAFE);
}
Expand Down Expand Up @@ -203,7 +244,7 @@ void testNested() {
.types(secondObject)
.types(UNSAFE_ALIAS)
.build();
ConjureDefinitionValidator.validateAll(conjureDef);
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(firstObject)).hasValue(LogSafety.UNSAFE);
}
Expand All @@ -222,7 +263,7 @@ void testPrimitiveSafety() {
.build())
.build()))
.build();
ConjureDefinitionValidator.validateAll(conjureDef);
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(Iterables.getOnlyElement(conjureDef.getTypes())))
.hasValue(LogSafety.UNSAFE);
Expand All @@ -241,7 +282,7 @@ void testNoSafety() {
.build())
.build()))
.build();
ConjureDefinitionValidator.validateAll(conjureDef);
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(Iterables.getOnlyElement(conjureDef.getTypes())))
.isEmpty();
Expand All @@ -254,7 +295,7 @@ void testEmptyUnion() {
.types(TypeDefinition.union(
UnionDefinition.builder().typeName(FOO).build()))
.build();
ConjureDefinitionValidator.validateAll(conjureDef);
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(Iterables.getOnlyElement(conjureDef.getTypes())))
.as("No guarantees can be made about future union values, "
Expand All @@ -275,7 +316,7 @@ void testUnsafeElementUnion() {
.build())
.build()))
.build();
ConjureDefinitionValidator.validateAll(conjureDef);
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(Iterables.getOnlyElement(conjureDef.getTypes())))
.hasValue(LogSafety.UNSAFE);
Expand All @@ -294,7 +335,7 @@ void testDoNotLogElementUnion() {
.build())
.build()))
.build();
ConjureDefinitionValidator.validateAll(conjureDef);
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(Iterables.getOnlyElement(conjureDef.getTypes())))
.hasValue(LogSafety.DO_NOT_LOG);
Expand All @@ -307,7 +348,7 @@ void testEmptyObject() {
.types(TypeDefinition.object(
ObjectDefinition.builder().typeName(FOO).build()))
.build();
ConjureDefinitionValidator.validateAll(conjureDef);
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(Iterables.getOnlyElement(conjureDef.getTypes())))
.hasValue(LogSafety.SAFE);
Expand All @@ -320,9 +361,52 @@ void testEmptyEnum() {
.types(TypeDefinition.enum_(
EnumDefinition.builder().typeName(FOO).build()))
.build();
ConjureDefinitionValidator.validateAll(conjureDef);
ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED);
SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef);
assertThat(evaluator.evaluate(Iterables.getOnlyElement(conjureDef.getTypes())))
.hasValue(LogSafety.SAFE);
}

private static Stream<Arguments> getTypes(Type externalReference) {
TypeDefinition objectType = TypeDefinition.object(ObjectDefinition.builder()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it's sufficient to just test objects and the rest is overkill

.typeName(FOO)
.fields(FieldDefinition.builder()
.fieldName(FieldName.of("import"))
.type(externalReference)
.docs(DOCS)
.build())
.build());
ConjureDefinition conjureObjectDef = ConjureDefinition.builder()
.version(1)
.types(objectType)
.types(UNSAFE_ALIAS)
.build();

TypeDefinition aliasType = TypeDefinition.alias(
AliasDefinition.builder().typeName(FOO).alias(externalReference).build());
ConjureDefinition conjureAliasDef = ConjureDefinition.builder()
.version(1)
.types(aliasType)
.types(UNSAFE_ALIAS)
.build();

TypeDefinition unionType = TypeDefinition.union(UnionDefinition.builder()
.union(FieldDefinition.builder()
.fieldName(FieldName.of("importOne"))
.type(externalReference)
.docs(DOCS)
.build())
.typeName(FOO)
.build());
ConjureDefinition conjureUnionDef = ConjureDefinition.builder()
.version(1)
.types(unionType)
.types(UNSAFE_ALIAS)
.build();

return Stream.of(
Arguments.of(Named.of("Object", objectType), conjureObjectDef),
Arguments.of(Named.of("Alias", aliasType), conjureAliasDef),
Arguments.of(Named.of("Union", unionType), conjureUnionDef));
}
}
16 changes: 8 additions & 8 deletions versions.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
com.atlassian.commonmark:commonmark:0.12.1 (1 constraints: 36052a3b)
com.fasterxml.jackson.core:jackson-annotations:2.14.1 (20 constraints: c14d5f30)
com.fasterxml.jackson.core:jackson-core:2.14.1 (18 constraints: 386dff6a)
com.fasterxml.jackson.core:jackson-databind:2.14.1 (35 constraints: a7918e71)
com.fasterxml.jackson.core:jackson-databind:2.14.1 (35 constraints: e99011cd)
com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.14.1 (2 constraints: e1338f00)
com.fasterxml.jackson.dataformat:jackson-dataformat-smile:2.14.1 (1 constraints: 7f1c9ca4)
com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.14.1 (3 constraints: 3b25d80f)
com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.14.1 (3 constraints: 3b25da0f)
com.fasterxml.jackson.datatype:jackson-datatype-guava:2.14.1 (4 constraints: a65067ce)
com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.14.1 (4 constraints: 2a3f0b90)
com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.14.1 (4 constraints: 2a3f0d90)
com.fasterxml.jackson.datatype:jackson-datatype-joda:2.14.1 (2 constraints: 302bedaf)
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.1 (2 constraints: 302bedaf)
com.fasterxml.jackson.module:jackson-module-afterburner:2.14.1 (2 constraints: 4c2b80b6)
Expand All @@ -20,8 +20,8 @@ com.google.guava:guava:31.1-jre (34 constraints: 2b5e25c9)
com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava (1 constraints: bd17c918)
com.google.j2objc:j2objc-annotations:1.3 (1 constraints: b809eda0)
com.palantir.common:streams:2.1.0 (1 constraints: 0505f835)
com.palantir.conjure:conjure-api-objects:4.28.0 (2 constraints: 6322d2e4)
com.palantir.conjure:conjure-generator-common:4.28.0 (2 constraints: 07149683)
com.palantir.conjure:conjure-api-objects:4.36.0 (2 constraints: 612297e4)
com.palantir.conjure:conjure-generator-common:4.36.0 (2 constraints: 05146783)
com.palantir.conjure.java.api:errors:2.34.0 (6 constraints: d66d7bc6)
com.palantir.conjure.java.api:service-config:2.34.0 (4 constraints: 3848f64e)
com.palantir.conjure.java.api:ssl-config:2.34.0 (2 constraints: a825c238)
Expand All @@ -46,7 +46,7 @@ com.palantir.safe-logging:logger-spi:3.2.0 (2 constraints: 0f1e997a)
com.palantir.safe-logging:preconditions:3.2.0 (26 constraints: 93cdde31)
com.palantir.safe-logging:safe-logging:3.2.0 (21 constraints: 0d5d6eb0)
com.palantir.safethreadlocalrandom:safe-thread-local-random:0.1.0 (3 constraints: 933225f9)
com.palantir.syntactic-paths:syntactic-paths:0.9.0 (2 constraints: 9b137361)
com.palantir.syntactic-paths:syntactic-paths:0.9.0 (2 constraints: 9d137b61)
com.palantir.tokens:auth-tokens:3.16.0 (5 constraints: 5e511ae3)
com.palantir.tracing:tracing:6.15.0 (8 constraints: de7caf43)
com.palantir.tracing:tracing-api:6.15.0 (6 constraints: 1760c248)
Expand Down Expand Up @@ -78,7 +78,7 @@ org.jboss.logging:jboss-logging:3.4.1.Final (4 constraints: a24557a8)
org.jboss.threads:jboss-threads:3.1.0.Final (2 constraints: 561a9b42)
org.jboss.xnio:xnio-api:3.8.7.Final (2 constraints: 771a3146)
org.jboss.xnio:xnio-nio:3.8.7.Final (1 constraints: c80dcb30)
org.jetbrains:annotations:24.0.0 (3 constraints: 94287fbe)
org.jetbrains:annotations:24.0.0 (4 constraints: fa396f95)
org.mpierce.metrics.reservoir:hdrhistogram-metrics-reservoir:1.1.3 (1 constraints: 0d10f991)
org.slf4j:slf4j-api:1.7.36 (27 constraints: 608897a9)
org.slf4j:slf4j-simple:1.7.36 (1 constraints: 43054b3b)
Expand All @@ -105,7 +105,7 @@ com.helger:profiler:1.1.1 (1 constraints: e21053b8)
com.netflix.concurrency-limits:concurrency-limits-core:0.2.2 (1 constraints: 7d14fe7b)
com.netflix.feign:feign-core:8.18.0 (3 constraints: de3f76e0)
com.netflix.feign:feign-jackson:8.18.0 (1 constraints: c718909e)
com.palantir.conjure:conjure-core:4.28.0 (1 constraints: 4005573b)
com.palantir.conjure:conjure-core:4.36.0 (1 constraints: 3f05553b)
com.palantir.conjure.java.api:test-utils:2.34.0 (1 constraints: 3b05433b)
com.palantir.conjure.java.runtime:conjure-java-annotations:7.39.0 (1 constraints: c918959e)
com.palantir.conjure.java.runtime:conjure-java-jaxrs-client:7.39.0 (1 constraints: 4505703b)
Expand Down
2 changes: 1 addition & 1 deletion versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ com.palantir.common:streams = 2.1.0
com.palantir.conjure.java.api:* = 2.34.0
com.palantir.conjure.java.runtime:* = 7.39.0
com.palantir.conjure.verification:* = 0.19.0
com.palantir.conjure:* = 4.28.0
com.palantir.conjure:* = 4.36.0
com.palantir.dialogue:* = 3.74.0
com.palantir.ri:resource-identifier = 2.5.0
com.palantir.safe-logging:* = 3.2.0
Expand Down