-
Notifications
You must be signed in to change notification settings - Fork 43
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
lsingh123
wants to merge
3
commits into
palantir:develop
Choose a base branch
from
lsingh123:lsingh/external-imports
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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"; | ||
|
@@ -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() | ||
|
@@ -150,7 +191,7 @@ void testMapSafetyUnsafeValue() { | |
.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.UNSAFE); | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
|
@@ -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(); | ||
|
@@ -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, " | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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