Skip to content

Commit 4f57710

Browse files
Add xmlFlattened trait support
This adds an indication of whether a given list/map is flat or wrapped.
1 parent f688130 commit 4f57710

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

smithy-docgen-core/src/main/java/software/amazon/smithy/docgen/core/integrations/BuiltinsIntegration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import software.amazon.smithy.docgen.core.interceptors.UniqueItemsInterceptor;
3636
import software.amazon.smithy.docgen.core.interceptors.UnstableInterceptor;
3737
import software.amazon.smithy.docgen.core.interceptors.XmlAttributeInterceptor;
38+
import software.amazon.smithy.docgen.core.interceptors.XmlFlattenedInterceptor;
3839
import software.amazon.smithy.docgen.core.interceptors.XmlNameInterceptor;
3940
import software.amazon.smithy.docgen.core.writers.DocWriter;
4041
import software.amazon.smithy.docgen.core.writers.MarkdownWriter;
@@ -83,6 +84,7 @@ public List<? extends CodeInterceptor<? extends CodeSection, DocWriter>> interce
8384
new JsonNameInterceptor(),
8485
new XmlAttributeInterceptor(),
8586
new XmlNameInterceptor(),
87+
new XmlFlattenedInterceptor(),
8688
new PaginationInterceptor(),
8789
new RequestCompressionInterceptor(),
8890
new NoReplaceBindingInterceptor(),
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.docgen.core.interceptors;
7+
8+
import software.amazon.smithy.docgen.core.DocGenerationContext;
9+
import software.amazon.smithy.docgen.core.sections.ProtocolSection;
10+
import software.amazon.smithy.docgen.core.writers.DocWriter;
11+
import software.amazon.smithy.model.shapes.Shape;
12+
import software.amazon.smithy.model.traits.ProtocolDefinitionTrait;
13+
import software.amazon.smithy.model.traits.XmlFlattenedTrait;
14+
import software.amazon.smithy.utils.CodeInterceptor;
15+
import software.amazon.smithy.utils.Pair;
16+
import software.amazon.smithy.utils.SmithyInternalApi;
17+
18+
/**
19+
* Adds information to the protocol section for members indicating whether they target
20+
* a flat list/map or wrapped list/map depending on whether they have the
21+
* <a href="https://smithy.io/2.0/spec/protocol-traits.html#xmlflattened-trait">
22+
* xmlFlattened</a> trait.
23+
*/
24+
@SmithyInternalApi
25+
public class XmlFlattenedInterceptor implements CodeInterceptor<ProtocolSection, DocWriter> {
26+
private static final Pair<String, String> WRAPPED_LIST_REF = Pair.of(
27+
"wrapped", "https://smithy.io/2.0/spec/protocol-traits.html#wrapped-list-serialization"
28+
);
29+
private static final Pair<String, String> FLAT_LIST_REF = Pair.of(
30+
"flat", "https://smithy.io/2.0/spec/protocol-traits.html#flattened-list-serialization"
31+
);
32+
private static final Pair<String, String> WRAPPED_MAP_REF = Pair.of(
33+
"wrapped", "https://smithy.io/2.0/spec/protocol-traits.html#wrapped-map-serialization"
34+
);
35+
private static final Pair<String, String> FLAT_MAP_REF = Pair.of(
36+
"flat", "https://smithy.io/2.0/spec/protocol-traits.html#flattened-map-serialization"
37+
);
38+
39+
@Override
40+
public Class<ProtocolSection> sectionType() {
41+
return ProtocolSection.class;
42+
}
43+
44+
@Override
45+
public boolean isIntercepted(ProtocolSection section) {
46+
if (!section.shape().isMemberShape()) {
47+
return false;
48+
}
49+
50+
var protocolShape = section.context().model().expectShape(section.protocol());
51+
var protocolDefinition = protocolShape.expectTrait(ProtocolDefinitionTrait.class);
52+
if (!protocolDefinition.getTraits().contains(XmlFlattenedTrait.ID)) {
53+
return false;
54+
}
55+
56+
var target = section.context().model().expectShape(section.shape().asMemberShape().get().getTarget());
57+
return target.isListShape() || target.isMapShape();
58+
}
59+
60+
@Override
61+
public void write(DocWriter writer, String previousText, ProtocolSection section) {
62+
writer.write("""
63+
Serialization type: $R
64+
65+
$L""", getRef(section.context(), section.shape()), previousText);
66+
67+
}
68+
69+
private Pair<String, String> getRef(DocGenerationContext context, Shape shape) {
70+
var target = context.model().expectShape(shape.asMemberShape().get().getTarget());
71+
if (target.isMapShape()) {
72+
if (shape.hasTrait(XmlFlattenedTrait.class)) {
73+
return FLAT_MAP_REF;
74+
}
75+
return WRAPPED_MAP_REF;
76+
}
77+
78+
if (shape.hasTrait(XmlFlattenedTrait.class)) {
79+
return FLAT_LIST_REF;
80+
}
81+
return WRAPPED_LIST_REF;
82+
}
83+
}

smithy-docgen-test/model/main.smithy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ structure XmlTraits {
168168
/// This shows how xml attributes are displayed.
169169
@xmlAttribute
170170
xmlAttribute: String
171+
172+
/// This list uses the default nesting behavior.
173+
nestedList: StringList
174+
175+
/// This list uses the non-default flat list behavior.
176+
@xmlFlattened
177+
flatList: StringList
178+
179+
/// This map uses the default nesting behavior.
180+
nestedMap: StringMap
181+
182+
/// This map uses the non-default flat map behavior.
183+
@xmlFlattened
184+
flatMap: StringMap
185+
}
186+
187+
list StringList {
188+
member: String
171189
}
172190

173191
/// This in an enum that can have one of the following values:

0 commit comments

Comments
 (0)