-
Notifications
You must be signed in to change notification settings - Fork 218
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
Add validator for xmlFlattened + xmlName #2439
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.model.validation.validators; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.ListShape; | ||
import software.amazon.smithy.model.shapes.MemberShape; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.traits.XmlFlattenedTrait; | ||
import software.amazon.smithy.model.traits.XmlNameTrait; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
|
||
/** | ||
* Validates that xmlFlattened members aren't unintentionally ignoring the | ||
* xmlName of their targets. | ||
*/ | ||
public final class XmlFlattenedTraitValidator extends AbstractValidator { | ||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
for (MemberShape member : model.getMemberShapesWithTrait(XmlFlattenedTrait.class)) { | ||
// Don't emit the event if they're being explicit about the xmlName on this member | ||
if (member.hasTrait(XmlNameTrait.class)) { | ||
continue; | ||
} | ||
|
||
model.getShape(member.getTarget()) | ||
.flatMap(Shape::asListShape) | ||
.flatMap(listShape -> validateMemberTargetingList(member, listShape)) | ||
.ifPresent(events::add); | ||
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. IMO reading this as a stream is harder than just reading
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. True I forgot we run that type of validation first, so we can expect the member target to exist. |
||
} | ||
return events; | ||
} | ||
|
||
private Optional<ValidationEvent> validateMemberTargetingList(MemberShape member, ListShape targetList) { | ||
return targetList.getMember().getTrait(XmlNameTrait.class) | ||
.filter(xmlName -> !member.getMemberName().equals(xmlName.getValue())) | ||
.map(xmlName -> warning(member, String.format( | ||
"Member is `@xmlFlattened`, so `@xmlName` of target's member (`%s`) has no effect." | ||
+ " The flattened list elements will have the name of this member - `%s`. If this" | ||
+ " is unintended, you can add `@xmlName(\"%s\")` to this member.", | ||
targetList.getMember().getId(), member.getMemberName(), xmlName.getValue()))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[WARNING] smithy.example#Struct$flattenedNoXmlName: Member is `@xmlFlattened`, so `@xmlName` of target's member (`smithy.example#ListWithXmlName$member`) has no effect. The flattened list elements will have the name of this member - `flattenedNoXmlName`. If this is unintended, you can add `@xmlName("customMember")` to this member. | XmlFlattenedTrait |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
$version: "2" | ||
|
||
namespace smithy.example | ||
|
||
structure Struct { | ||
// No event because not flattened | ||
notFlattened: ListWithXmlName | ||
|
||
// Event because flattened and non-matching member name | ||
@xmlFlattened | ||
flattenedNoXmlName: ListWithXmlName | ||
|
||
// No event because the member name matches the xml name | ||
@xmlFlattened | ||
customMember: ListWithXmlName | ||
|
||
// No event because you're being explicit about the name to use | ||
@xmlFlattened | ||
@xmlName("customMember") | ||
flattenedMatchingXmlName: ListWithXmlName | ||
|
||
// No event because you're being explicit about the name to use | ||
@xmlFlattened | ||
@xmlName("Bar") | ||
flattenedNonMatchingXmlName: ListWithXmlName | ||
|
||
// Validator doesn't apply to maps | ||
@xmlFlattened | ||
flattenedMap: MapWithXmlName | ||
} | ||
|
||
list ListWithXmlName { | ||
@xmlName("customMember") | ||
member: String | ||
} | ||
|
||
map MapWithXmlName { | ||
@xmlName("customKey") | ||
key: String | ||
|
||
@xmlName("customValue") | ||
value: String | ||
} |
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.
Is it prohibitive to use the
@suppress
trait instead of a metadata suppression? It's a nice bit of documentation on that specific member as well that it's intending to test the behavior.