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 sourceLocation propagation in Traits #864

Merged
merged 6 commits into from
Jul 21, 2021

Conversation

gosar
Copy link
Contributor

@gosar gosar commented Jul 14, 2021

The sourceLocation should propogate from

  1. TraitService.createTrait -> Trait
  2. Trait -> createNode
  3. Trait -> toBuilder

Across traits, this wasn't done consistently.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link
Contributor Author

@gosar gosar left a comment

Choose a reason for hiding this comment

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

I saw sourceLocation propagated in createNode and toBuilder in other Traits, but probably missed in some other Traits too, like IdRefTrait.createNode. So want to confirm they are needed and if so will fix all.

}

@Override
protected Node createNode() {
return throttling ? Node.objectNode().withMember(THROTTLING, true) : Node.objectNode();
ObjectNode.Builder nodeBuilder = Node.objectNodeBuilder().sourceLocation(getSourceLocation());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm unsure about the need for this propagation.

/**
 * The result of toNode is used for hashCodes and equality. Subclasses
 * must implement createNode to turn the trait into a Node. This is then
 * cached for subsequent retrievals.
 *
 * @return Returns the trait as a node.
 */
protected abstract Node createNode();

And Node's equals doesn't look at sourceLocation. So does propagating sourceLocation matter?

Copy link
Member

Choose a reason for hiding this comment

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

Yes we need it set here because we use SourceLocation in places like the NodeValidationVisitor (e.g., https://github.com/awslabs/smithy/blob/main/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/PatternTraitPlugin.java#L43)

@@ -69,7 +73,8 @@ public ShapeId getShapeId() {
@Override
public RetryableTrait createTrait(ShapeId target, Node value) {
ObjectNode node = value.expectObjectNode();
return builder().throttling(node.getBooleanMemberOrDefault(THROTTLING)).build();
Builder builder = builder().sourceLocation(value.getSourceLocation());
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 is used to create a modified Trait instance. Does continuing the sourceLocation make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably helps with some traceability. Most existing tratis propagate it, so updating any traits, like RetryableTrait, that aren't doing so.

@gosar gosar changed the title Fix sourceLocation propagation in RetryableTrait Fix sourceLocation propagation in Traits Jul 14, 2021
@@ -70,6 +70,7 @@ public Provider() {

@Override
public Trait createTrait(ShapeId target, Node value) {
// BUG: sourceLocation
Copy link
Contributor

Choose a reason for hiding this comment

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

value.getSourceLocation()

@@ -88,6 +91,7 @@ public Provider() {

@Override
public ClientDiscoveredEndpointTrait createTrait(ShapeId target, Node value) {
// BUG: sourceLocation
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use value.getSourceLocation() for all of these

@@ -336,12 +337,14 @@ protected ObjectNode createNode() {
NodeMapper mapper = new NodeMapper();
mapper.disableToNodeForClass(IntegrationTrait.class);
mapper.setOmitEmptyValues(true);
return mapper.serialize(this).expectObjectNode();
ObjectNode.Builder builder = mapper.serialize(this).expectObjectNode().toBuilder();
return builder.sourceLocation(getSourceLocation()).build();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@JordonPhillips this still is necessary after #865 right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nope

Copy link
Contributor Author

@gosar gosar Jul 19, 2021

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

It isn't. This implementation of ToNode is using the NodeMapper, which will set it for you. The implementation is identical to the same method in this test trait. I modeled that test class after this exact trait

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. I did not notice the disableToNodeForClass part. I was also confused by ObjectNode.toBuilder() not propagating the sourceLocation, which affected my local testing.

@gosar gosar requested a review from JordonPhillips July 16, 2021 22:36
gosar added 5 commits July 19, 2021 12:56
Most existing toBuilders do this, but these ones were missing that
logic.
Most existing createNode methods do this, but these ones were missing
that logic.
Most existing createTrait methods do this, but these ones were missing
that logic.
@gosar gosar added the bug This issue is a bug. label Jul 19, 2021
Copy link
Contributor

@JordonPhillips JordonPhillips left a comment

Choose a reason for hiding this comment

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

Traits using NodeMapper in createNode do not need anything extra done at the top level.

@gosar gosar requested a review from JordonPhillips July 20, 2021 17:38
@JordonPhillips JordonPhillips merged commit 20e4eea into smithy-lang:main Jul 21, 2021
@gosar gosar deleted the trait-fixes branch July 27, 2021 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants