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

Fix warnings in AST Loader for Resource and Operation Shapes with mixins #1626

Merged
merged 6 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -76,19 +76,19 @@ final class AstModelLoader {

private static final List<String> TOP_LEVEL_PROPERTIES = ListUtils.of("smithy", SHAPES, METADATA);
private static final List<String> APPLY_PROPERTIES = ListUtils.of(TYPE, TRAITS);
private static final List<String> SIMPLE_PROPERTY_NAMES = ListUtils.of(TYPE, TRAITS);
private static final List<String> SIMPLE_PROPERTY_NAMES = ListUtils.of(TYPE, TRAITS, MIXINS);
private static final List<String> NAMED_MEMBER_SHAPE_PROPERTY_NAMES = ListUtils.of(TYPE, MEMBERS, TRAITS, MIXINS);
private static final List<String> COLLECTION_PROPERTY_NAMES = ListUtils.of(TYPE, "member", TRAITS);
private static final List<String> MAP_PROPERTY_NAMES = ListUtils.of(TYPE, "key", "value", TRAITS);
private static final List<String> COLLECTION_PROPERTY_NAMES = ListUtils.of(TYPE, "member", TRAITS, MIXINS);
private static final List<String> MAP_PROPERTY_NAMES = ListUtils.of(TYPE, "key", "value", TRAITS, MIXINS);
private static final Set<String> MEMBER_PROPERTIES = SetUtils.of(TARGET, TRAITS);
private static final Set<String> REFERENCE_PROPERTIES = SetUtils.of(TARGET);
private static final Set<String> OPERATION_PROPERTY_NAMES = SetUtils.of(
TYPE, "input", "output", ERRORS, TRAITS);
TYPE, "input", "output", ERRORS, TRAITS, MIXINS);
private static final Set<String> RESOURCE_PROPERTIES = SetUtils.of(
TYPE, "create", "read", "update", "delete", "list", "put",
"identifiers", "resources", "operations", "collectionOperations", "properties", TRAITS);
"identifiers", "resources", "operations", "collectionOperations", "properties", TRAITS, MIXINS);
private static final Set<String> SERVICE_PROPERTIES = SetUtils.of(
TYPE, "version", "operations", "resources", "rename", ERRORS, TRAITS);
TYPE, "version", "operations", "resources", "rename", ERRORS, TRAITS, MIXINS);

private final Version modelVersion;
private final ObjectNode model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,83 @@ public class AstModelLoaderTest {
@Test
public void failsToLoadPropertiesFromV1() {
ValidatedResult<Model> model = Model.assembler()
.addImport(getClass().getResource("invalid/properties-v2-only.json"))
.assemble();
.addImport(getClass().getResource("invalid/properties-v2-only.json"))
.assemble();
assertEquals(1, model.getValidationEvents(Severity.ERROR).size());
assertTrue(model.getValidationEvents(Severity.ERROR).get(0).getMessage()
.contains("Resource properties can only be used with Smithy version 2 or later."));
.contains("Resource properties can only be used with Smithy version 2 or later."));
}

@Test
public void doesNotFailOnEmptyApply() {
// Empty apply statements are pointless but shouldn't break the loader.
Model.assembler()
.addImport(getClass().getResource("ast-empty-apply-1.json"))
.addImport(getClass().getResource("ast-empty-apply-2.json"))
.assemble()
.unwrap();
.addImport(getClass().getResource("ast-empty-apply-1.json"))
.addImport(getClass().getResource("ast-empty-apply-2.json"))
.assemble()
.unwrap();
}

@Test
public void allowsMixinsOnOperationsWithoutWarningOrError() {
ValidatedResult<Model> model = Model.assembler()
.addImport(getClass().getResource("mixins/operation-mixins.json"))
.assemble();
assertEquals(0, model.getValidationEvents(Severity.WARNING).size());
assertEquals(0, model.getValidationEvents(Severity.ERROR).size());
}

@Test
public void allowsMixinsOnResourcesWithoutWarningOrError() {
ValidatedResult<Model> model = Model.assembler()
.addImport(getClass().getResource("mixins/resource-mixins.json"))
.assemble();
assertEquals(0, model.getValidationEvents(Severity.WARNING).size());
assertEquals(0, model.getValidationEvents(Severity.ERROR).size());
}

@Test
public void allowsMixinsOnServiceWithoutWarningOrError() {
ValidatedResult<Model> model = Model.assembler()
.addImport(getClass().getResource("mixins/service-mixins.json"))
.assemble();
assertEquals(0, model.getValidationEvents(Severity.WARNING).size());
assertEquals(0, model.getValidationEvents(Severity.ERROR).size());
}

@Test
public void allowsMixinsOnStructuresWithoutWarningOrError() {
ValidatedResult<Model> model = Model.assembler()
.addImport(getClass().getResource("mixins/structure-mixins.json"))
.assemble();
assertEquals(0, model.getValidationEvents(Severity.WARNING).size());
assertEquals(0, model.getValidationEvents(Severity.ERROR).size());
}

@Test
public void allowsMixinsOnUnionsWithoutWarningOrError() {
ValidatedResult<Model> model = Model.assembler()
.addImport(getClass().getResource("mixins/union-mixins.json"))
.assemble();
assertEquals(0, model.getValidationEvents(Severity.WARNING).size());
assertEquals(0, model.getValidationEvents(Severity.ERROR).size());
}

@Test
public void allowsMixinsOnSimpleShapesWithoutWarningOrError() {
ValidatedResult<Model> model = Model.assembler()
.addImport(getClass().getResource("mixins/simple-shape-mixins.json"))
.assemble();
assertEquals(0, model.getValidationEvents(Severity.WARNING).size());
assertEquals(0, model.getValidationEvents(Severity.ERROR).size());
}

@Test
public void allowsMixinsOnCollectionShapesWithoutWarningOrError() {
ValidatedResult<Model> model = Model.assembler()
.addImport(getClass().getResource("mixins/collection-mixins.json"))
.assemble();
assertEquals(0, model.getValidationEvents(Severity.WARNING).size());
assertEquals(0, model.getValidationEvents(Severity.ERROR).size());
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[WARNING] smithy.example#Hi: Expected an object with possible properties of `key`, `traits`, `type`, `value`, but found additional properties: `foo` | Model
[WARNING] smithy.example#Hi: Expected an object with possible properties of `key`, `mixins`, `traits`, `type`, `value`, but found additional properties: `foo` | Model
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[WARNING] smithy.example#Hi: Expected an object with possible properties of `traits`, `type`, but found additional properties: `invalid` | Model
[WARNING] smithy.example#Hi: Expected an object with possible properties of `mixins`, `traits`, `type`, but found additional properties: `invalid` | Model
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"smithy": "2.0",
"shapes": {
"smithy.example#MyList": {
"type": "list",
"mixins": [
{
"target": "smithy.example#MyListMixin"
}
]
},
"smithy.example#MyListMixin": {
"type": "list",
"member": {
"target": "smithy.api#String"
},
"traits": {
"smithy.api#length": {
"min": 2,
"max": 4
},
"smithy.api#mixin": {}
}
},
"smithy.example#MyMap": {
"type": "map",
"mixins": [
{
"target": "smithy.example#MyMapMixin"
}
]
},
"smithy.example#MyMapMixin": {
"type": "map",
"key": {
"target": "smithy.api#String"
},
"value": {
"target": "smithy.api#String"
},
"traits": {
"smithy.api#length": {
"min": 1,
"max": 4
},
"smithy.api#mixin": {}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"smithy": "2.0",
"shapes": {
"example.weather.errors#ThrottlingException": {
"type": "structure",
"members": {},
"traits": {
"smithy.api#error": "server"
}
},
"example.weather.errors#ValidationException": {
"type": "structure",
"members": {},
"traits": {
"smithy.api#error": "client"
}
},
"example.weather.mixins#OperationMixin": {
"type": "operation",
"input": {
"target": "smithy.api#Unit"
},
"output": {
"target": "smithy.api#Unit"
},
"errors": [
{
"target": "example.weather.errors#ThrottlingException"
},
{
"target": "example.weather.errors#ValidationException"
}
],
"traits": {
"smithy.api#mixin": {}
}
},
"example.weather.operations#OperationWithMixin": {
"type": "operation",
"mixins": [
{
"target": "example.weather.mixins#OperationMixin"
}
],
"input": {
"target": "example.weather.operations#OperationWithMixinInput"
},
"output": {
"target": "smithy.api#Unit"
},
"traits": {
"smithy.api#http": {
"method": "POST",
"uri": "/my/resource/uri/{myInputField}"
}
}
},
"example.weather.operations#OperationWithMixinInput": {
"type": "structure",
"members": {
"myInputField": {
"target": "smithy.api#String",
"traits": {
"smithy.api#httpLabel": {},
"smithy.api#required": {}
}
},
"other": {
"target": "smithy.api#String"
}
},
"traits": {
"smithy.api#input": {}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"smithy": "2.0",
"shapes": {
"example.weather.resources#DummyOperation": {
"type": "operation",
"input": {
"target": "example.weather.resources#DummyOperationInput"
},
"output": {
"target": "smithy.api#Unit"
},
"traits": {
}
},
"example.weather.resources#DummyOperationInput": {
"type": "structure",
"members": {
"dummyInput": {
"target": "smithy.api#String"
}
},
"traits": {
"smithy.api#input": {}
}
},
"example.weather.mixins#ResourceMixin": {
"type": "resource",
"traits": {
"smithy.api#mixin": {}
}
},
"example.weather.resources#ResourceWithMixin": {
"type": "resource",
"mixins": [
{
"target": "example.weather.mixins#ResourceMixin"
}
],
"operations": [
{
"target": "example.weather.resources#DummyOperation"
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"smithy": "2.0",
"shapes": {
"smithy.example#MyDummyOperation": {
"type": "operation",
"input": {
"target": "smithy.example#MyDummyOperationInput"
},
"output": {
"target": "smithy.api#Unit"
}
},
"smithy.example#MyDummyOperationInput": {
"type": "structure",
"members": {
"foo": {
"target": "smithy.api#String"
}
},
"traits": {
"smithy.api#input": {}
}
},
"smithy.example#MyMixin": {
"type": "service",
"operations": [
{
"target": "smithy.example#MyDummyOperation"
}
],
"traits": {
"smithy.api#mixin": {}
}
},
"smithy.example#MyService": {
"type": "service",
"mixins": [
{
"target": "smithy.example#MyMixin"
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"smithy": "2.0",
"shapes": {
"smithy.example#MyIntMixin": {
"type": "integer",
"traits": {
"smithy.api#mixin": {},
"smithy.api#range": {
"min": 1
}
}
},
"smithy.example#MyStringMixin": {
"type": "string",
"traits": {
"smithy.api#length": {
"min": 2,
"max": 4
},
"smithy.api#mixin": {}
}
},
"smithy.example#MyTestInt": {
"type": "integer",
"mixins": [
{
"target": "smithy.example#MyIntMixin"
}
]
},
"smithy.example#MyTestString": {
"type": "string",
"mixins": [
{
"target": "smithy.example#MyStringMixin"
}
]
}
}
}
Loading