Skip to content

Commit 065a1eb

Browse files
authored
Fix trait codegen for List or Map traits with annotations or Javadocs (#2729)
1 parent 8159e00 commit 065a1eb

File tree

6 files changed

+145
-1
lines changed

6 files changed

+145
-1
lines changed

smithy-trait-codegen/src/main/java/software/amazon/smithy/traitcodegen/integrations/annotations/DeprecatedAnnotationInterceptor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55
package software.amazon.smithy.traitcodegen.integrations.annotations;
66

7+
import software.amazon.smithy.model.shapes.Shape;
78
import software.amazon.smithy.model.traits.DeprecatedTrait;
9+
import software.amazon.smithy.model.traits.TraitDefinition;
810
import software.amazon.smithy.traitcodegen.sections.ClassSection;
911
import software.amazon.smithy.traitcodegen.sections.EnumVariantSection;
1012
import software.amazon.smithy.traitcodegen.sections.GetterSection;
@@ -27,7 +29,8 @@ public boolean isIntercepted(CodeSection section) {
2729
if (section instanceof ClassSection) {
2830
return ((ClassSection) section).shape().hasTrait(DeprecatedTrait.ID);
2931
} else if (section instanceof GetterSection) {
30-
return ((GetterSection) section).shape().hasTrait(DeprecatedTrait.ID);
32+
Shape shape = ((GetterSection) section).shape();
33+
return shape.hasTrait(DeprecatedTrait.ID) && !shape.hasTrait(TraitDefinition.ID);
3134
} else if (section instanceof EnumVariantSection) {
3235
return ((EnumVariantSection) section).memberShape().hasTrait(DeprecatedTrait.ID);
3336
}

smithy-trait-codegen/src/main/java/software/amazon/smithy/traitcodegen/integrations/javadoc/JavaDocInjectorInterceptor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package software.amazon.smithy.traitcodegen.integrations.javadoc;
66

77
import software.amazon.smithy.model.shapes.Shape;
8+
import software.amazon.smithy.model.traits.TraitDefinition;
89
import software.amazon.smithy.traitcodegen.sections.ClassSection;
910
import software.amazon.smithy.traitcodegen.sections.EnumVariantSection;
1011
import software.amazon.smithy.traitcodegen.sections.GetterSection;
@@ -40,6 +41,9 @@ public void prepend(TraitCodegenWriter writer, CodeSection section) {
4041
shape = ((ClassSection) section).shape();
4142
} else if (section instanceof GetterSection) {
4243
shape = ((GetterSection) section).shape();
44+
if (shape.hasTrait(TraitDefinition.ID)) {
45+
return;
46+
}
4347
} else if (section instanceof EnumVariantSection) {
4448
shape = ((EnumVariantSection) section).memberShape();
4549
} else {

smithy-trait-codegen/src/test/java/software/amazon/smithy/traitcodegen/integrations/annotations/AnnotationsTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,48 @@ void unstableAnnotationOnEnumVariant() {
130130
assertTrue(fileContents.contains(expected));
131131
}
132132

133+
@Test
134+
void deprecatedAnnotationOnListTrait() {
135+
String fileContents = getFileContentsFromShapeName("DeprecatedList", true);
136+
String expected = "@Deprecated\n" +
137+
"@SmithyGenerated\n" +
138+
"public final class DeprecatedListTrait";
139+
assertTrue(fileContents.contains(expected));
140+
}
141+
142+
@Test
143+
void noDeprecatedAnnotationOnListGetValues() {
144+
String fileContents = getFileContentsFromShapeName("DeprecatedList", true);
145+
String expected = " }\n\n" +
146+
" public List<Integer> getValues() {";
147+
assertTrue(fileContents.contains(expected));
148+
}
149+
150+
@Test
151+
void deprecatedAnnotationOnMapTrait() {
152+
String fileContents = getFileContentsFromShapeName("DeprecatedMap", true);
153+
String expected = "@Deprecated\n" +
154+
"@SmithyGenerated\n" +
155+
"public final class DeprecatedMapTrait";
156+
assertTrue(fileContents.contains(expected));
157+
}
158+
159+
@Test
160+
void noDeprecatedAnnotationOnMapGetValues() {
161+
String fileContents = getFileContentsFromShapeName("DeprecatedMap", true);
162+
String expected = " }\n\n" +
163+
" public Map<String, Integer> getValues() {";
164+
assertTrue(fileContents.contains(expected));
165+
}
166+
167+
@Test
168+
void deprecatedAnnotationOnListMember() {
169+
String fileContents = getFileContentsFromShapeName("DeprecatedStructure", true);
170+
String expected = " @Deprecated\n" +
171+
" public Optional<Map<String, Integer>> getDeprecatedMap() {";
172+
assertTrue(fileContents.contains(expected));
173+
}
174+
133175
private String getFileContentsFromShapeName(String className, boolean isTrait) {
134176
String suffix = isTrait ? "Trait" : "";
135177
String path = String.format("com/example/traits/%s%s.java", className, suffix);

smithy-trait-codegen/src/test/java/software/amazon/smithy/traitcodegen/integrations/javadoc/JavadocTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,57 @@ void allDocumentationIncludedTogetherEnumVariant() {
227227
assertTrue(fileContents.contains(expected));
228228
}
229229

230+
@Test
231+
void deprecatedAnnotationAndNoteForListTrait() {
232+
String fileContents = getFileContentsFromShapeName("DeprecatedList", true);
233+
String expectedForClass = "/**\n" +
234+
" * A deprecated list trait\n" +
235+
" *\n" +
236+
" * @see <a href=\"https://example.com\">Example</a>\n" +
237+
" * @since 4.5\n" +
238+
" * @deprecated As of yesterday. A message\n" +
239+
" */\n" +
240+
"@Deprecated\n" +
241+
"@SmithyGenerated\n" +
242+
"public final class DeprecatedListTrait";
243+
String expectedForGetter = " }\n\n" +
244+
" public List<Integer> getValues() {";
245+
assertTrue(fileContents.contains(expectedForClass));
246+
assertTrue(fileContents.contains(expectedForGetter));
247+
}
248+
249+
@Test
250+
void deprecatedAnnotationAndNoteForMapTrait() {
251+
String fileContents = getFileContentsFromShapeName("DeprecatedMap", true);
252+
String expectedForClass = "/**\n" +
253+
" * A deprecated map trait\n" +
254+
" *\n" +
255+
" * @see <a href=\"https://example.com\">Example</a>\n" +
256+
" * @since 4.5\n" +
257+
" * @deprecated As of yesterday. A message\n" +
258+
" */\n" +
259+
"@Deprecated\n" +
260+
"@SmithyGenerated\n" +
261+
"public final class DeprecatedMapTrait";
262+
String expectedForGetter = " }\n\n" +
263+
" public Map<String, Integer> getValues() {";
264+
assertTrue(fileContents.contains(expectedForClass));
265+
assertTrue(fileContents.contains(expectedForGetter));
266+
}
267+
268+
@Test
269+
void deprecatedAnnotationAndNoteForListMember() {
270+
String fileContents = getFileContentsFromShapeName("DeprecatedStructure", true);
271+
String expected = " /**\n" +
272+
" * @deprecated As of yesterday. A message\n" +
273+
" */\n" +
274+
" @Deprecated\n" +
275+
" public Optional<List<Integer>> getDeprecatedList() {\n" +
276+
" return Optional.ofNullable(deprecatedList);\n" +
277+
" }";
278+
assertTrue(fileContents.contains(expected));
279+
}
280+
230281
private String getFileContentsFromShapeName(String className, boolean isTrait) {
231282
String suffix = isTrait ? "Trait" : "";
232283
String path = String.format("com/example/traits/%s%s.java", className, suffix);

smithy-trait-codegen/src/test/resources/software/amazon/smithy/traitcodegen/integrations/annotations/annotations-test.smithy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ structure DeprecatedStructure {
2020
/// Has docs in addition to deprecated
2121
@deprecated
2222
deprecatedWithDocs: String
23+
24+
@deprecated
25+
deprecatedMap: MemberMap
26+
}
27+
28+
map MemberMap {
29+
key: String
30+
value: Integer
2331
}
2432

2533
@trait
@@ -41,4 +49,15 @@ enum EnumWithAnnotations {
4149
UNSTABLE
4250
}
4351

52+
@trait
53+
@deprecated
54+
list DeprecatedList {
55+
member: Integer
56+
}
4457

58+
@trait
59+
@deprecated
60+
map DeprecatedMap {
61+
key: String
62+
value: Integer
63+
}

smithy-trait-codegen/src/test/resources/software/amazon/smithy/traitcodegen/integrations/javadoc/javadoc-test.smithy

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ structure DeprecatedStructure {
3131
/// Has docs in addition to deprecated
3232
@deprecated(message: "A message", since: "yesterday")
3333
deprecatedWithDocs: String
34+
35+
@deprecated(message: "A message", since: "yesterday")
36+
deprecatedList: MemberList
37+
}
38+
39+
list MemberList {
40+
member: Integer
3441
}
3542

3643
@trait
@@ -78,3 +85,21 @@ enum EnumVariantsTest {
7885
B
7986
}
8087

88+
/// A deprecated list trait
89+
@trait
90+
@deprecated(message: "A message", since: "yesterday")
91+
@externalDocumentation(Example: "https://example.com")
92+
@since("4.5")
93+
list DeprecatedList {
94+
member: Integer
95+
}
96+
97+
/// A deprecated map trait
98+
@trait
99+
@deprecated(message: "A message", since: "yesterday")
100+
@externalDocumentation(Example: "https://example.com")
101+
@since("4.5")
102+
map DeprecatedMap {
103+
key: String
104+
value: Integer
105+
}

0 commit comments

Comments
 (0)