-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow error shape rename and disallow error shape rename for all AWS …
…protocols
- Loading branch information
1 parent
51b8baf
commit 9f12b31
Showing
22 changed files
with
195 additions
and
10 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-------------------- | ||
Error shape renaming | ||
-------------------- | ||
|
||
By default, Smithy permits renaming shapes to disambiguate shape ID conflicts in | ||
the :ref:`service closure <service-closure>` via the ``rename`` property. However, services using this protocol are | ||
not allowed to rename error shapes (shapes with :ref:`error trait <error-trait>` applied). |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-------------------- | ||
Error shape renaming | ||
-------------------- | ||
|
||
By default, Smithy permits renaming shapes to disambiguate shape ID conflicts in | ||
the :ref:`service closure <service-closure>` via the ``rename`` property. However, services using this protocol are | ||
not allowed to rename error shapes (shapes with :ref:`error trait <error-trait>` applied). |
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
106 changes: 106 additions & 0 deletions
106
smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/ErrorRenameValidator.java
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.aws.traits; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import software.amazon.smithy.aws.traits.protocols.AwsJson1_0Trait; | ||
import software.amazon.smithy.aws.traits.protocols.AwsJson1_1Trait; | ||
import software.amazon.smithy.aws.traits.protocols.AwsQueryTrait; | ||
import software.amazon.smithy.aws.traits.protocols.Ec2QueryTrait; | ||
import software.amazon.smithy.aws.traits.protocols.RestJson1Trait; | ||
import software.amazon.smithy.aws.traits.protocols.RestXmlTrait; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.ServiceShape; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.traits.ErrorTrait; | ||
import software.amazon.smithy.model.traits.Trait; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
import software.amazon.smithy.utils.SetUtils; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
@SmithyInternalApi | ||
public final class ErrorRenameValidator extends AbstractValidator { | ||
|
||
/** | ||
* By default, server-side implementation of AWS Protocols send the error shape ID on the wire, | ||
* and clients can use it to resolve the error type. | ||
* | ||
* However, some protocols implementations, including AWS JSON 1.1, only send the shape name. | ||
* If there are conflicting shape IDs e.g. smithy.service#ServiceError and smithy.other#ServiceError, | ||
* clients are unable to resolve the error type with the shape name alone. | ||
* | ||
* In addition, Server-side implementation of these protocols don't handle or send renamed shape names. | ||
* | ||
* Hence, error shape renaming are not supported for these protocols. | ||
*/ | ||
private static final Set<Class<? extends Trait>> UNSUPPORTED_PROTOCOLS = SetUtils.of( | ||
AwsJson1_0Trait.class, AwsJson1_1Trait.class, AwsQueryTrait.class, Ec2QueryTrait.class, | ||
RestJson1Trait.class, RestXmlTrait.class | ||
); | ||
|
||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
for (ServiceShape shape : model.getServiceShapes()) { | ||
validate(model, shape, events); | ||
} | ||
return events; | ||
} | ||
|
||
private void validate(Model model, ServiceShape service, List<ValidationEvent> events) { | ||
final Map<ShapeId, String> renames = service.getRename(); | ||
|
||
if (renames.isEmpty()) { | ||
return; | ||
} | ||
|
||
Set<String> unSupportedProtocols = getUnsupportedProtocols(service); | ||
|
||
if (unSupportedProtocols.isEmpty()) { | ||
return; | ||
} | ||
|
||
renames.keySet() | ||
.forEach(shapeId -> { | ||
Optional<Shape> shape = model.getShape(shapeId); | ||
|
||
if (!shape.isPresent() || !shape.get().hasTrait(ErrorTrait.class)) { | ||
return; | ||
} | ||
|
||
ShapeId from = shape.get().getId(); | ||
String to = renames.get(from); | ||
events.add(error(service, String.format( | ||
"Service attempts to rename an error shape from `%s` to \"%s\"; " | ||
+ "Service protocols %s do not support error renaming.", | ||
from, to, unSupportedProtocols))); | ||
}); | ||
} | ||
|
||
private Set<String> getUnsupportedProtocols(ServiceShape service) { | ||
return service.getAllTraits().values().stream() | ||
.filter(trait -> UNSUPPORTED_PROTOCOLS.contains(trait.getClass())) | ||
.map(trait -> trait.toShapeId().getName()) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
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
2 changes: 2 additions & 0 deletions
2
...raits/src/test/resources/software/amazon/smithy/aws/traits/errorfiles/error-rename.errors
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[ERROR] smithy.example#MyService: Service attempts to rename an error shape from `smithy.example#BadGreeting` to "ThisDoesNotWork"; Service protocols [restXml, awsJson1_1] do not support error renaming. | ErrorRename | ||
[ERROR] smithy.example#MyService: Service attempts to rename an error shape from `smithy.example#ServerError` to "ServerDown"; Service protocols [restXml, awsJson1_1] do not support error renaming. | ErrorRename |
42 changes: 42 additions & 0 deletions
42
...raits/src/test/resources/software/amazon/smithy/aws/traits/errorfiles/error-rename.smithy
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
use aws.protocols#awsJson1_1 | ||
use aws.protocols#restXml | ||
|
||
@awsJson1_1 | ||
@restXml | ||
service MyService { | ||
version: "1", | ||
operations: [ | ||
SayHello, | ||
], | ||
rename: { | ||
"smithy.example#BadGreeting": "ThisDoesNotWork" | ||
"smithy.example#ServerError": "ServerDown" | ||
"smithy.example#Language": "LanguageSettings" | ||
} | ||
} | ||
|
||
operation SayHello { | ||
input: SayHelloInput, | ||
output: SayHelloOutput, | ||
errors: [BadGreeting, ServerError] | ||
} | ||
|
||
@input | ||
structure SayHelloInput { | ||
language: Language | ||
} | ||
|
||
@output | ||
structure SayHelloOutput {} | ||
|
||
@error("client") | ||
structure BadGreeting {} | ||
|
||
@error("server") | ||
structure ServerError {} | ||
|
||
structure Language {} |
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 |
---|---|---|
@@ -1 +0,0 @@ | ||
[ERROR] smithy.example#MyService: Service attempts to rename a structure shape from `smithy.example#BadGreeting` to "ThisDoesNotWork"; errors cannot be renamed | Service | ||