Skip to content

Commit f0dbe61

Browse files
committed
Add tests for filterSensitiveLog
1 parent 1bb22be commit f0dbe61

File tree

9 files changed

+268
-7
lines changed

9 files changed

+268
-7
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/StructuredMemberWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private void writeStructureFilterSensitiveLog(
109109
return;
110110
}
111111
// Call filterSensitiveLog on Structure.
112-
writer.openBlock("$T.filterSensitiveLog($L)", symbolProvider.toSymbol(structureTarget), structureParam);
112+
writer.write("$T.filterSensitiveLog($L)", symbolProvider.toSymbol(structureTarget), structureParam);
113113
}
114114

115115
/**

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/StructureGeneratorTest.java

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,92 @@ public void properlyGeneratesRequiredMessageMemberOfException() {
4343
+ "}");
4444
}
4545

46-
public void testErrorStructureCodegen(String file, String expectedType) {
46+
@Test
47+
public void addsfilterSensitiveLogForSimpleShape() {
48+
testStructureCodegen("sensitive-test-simple-shape.smithy",
49+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
50+
+ " ...obj,\n"
51+
+ " ...(obj.password && { password:\n"
52+
+ " SENSITIVE_STRING\n"
53+
+ " }),\n"
54+
+ " })\n");
55+
}
56+
57+
@Test
58+
public void addsfilterSensitiveLogForStructure() {
59+
testStructureCodegen("sensitive-test-structure.smithy",
60+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
61+
+ " ...obj,\n"
62+
+ " ...(obj.foo && { foo:\n"
63+
+ " User.filterSensitiveLog(obj.foo)\n"
64+
+ " }),\n"
65+
+ " })\n");
66+
}
67+
68+
@Test
69+
public void addsfilterSensitiveLogForStructureSelf() {
70+
testStructureCodegen("sensitive-test-structure-self.smithy",
71+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
72+
+ " ...obj,\n"
73+
+ " ...(obj.foo && { foo:\n"
74+
+ " SENSITIVE_STRING\n"
75+
+ " }),\n"
76+
+ " })\n");
77+
}
78+
79+
@Test
80+
public void addsfilterSensitiveLogForList() {
81+
testStructureCodegen("sensitive-test-list.smithy",
82+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
83+
+ " ...obj,\n"
84+
+ " ...(obj.foo && { foo:\n"
85+
+ " obj.foo.map(\n"
86+
+ " item =>\n"
87+
+ " User.filterSensitiveLog(item)\n"
88+
+ " )\n"
89+
+ " }),\n"
90+
+ " })\n");
91+
}
92+
93+
@Test
94+
public void addsfilterSensitiveLogForListSelf() {
95+
testStructureCodegen("sensitive-test-list-self.smithy",
96+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
97+
+ " ...obj,\n"
98+
+ " ...(obj.foo && { foo:\n"
99+
+ " SENSITIVE_STRING\n"
100+
+ " }),\n"
101+
+ " })\n");
102+
}
103+
104+
@Test
105+
public void addsfilterSensitiveLogForMap() {
106+
testStructureCodegen("sensitive-test-map.smithy",
107+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
108+
+ " ...obj,\n"
109+
+ " ...(obj.foo && { foo:\n"
110+
+ " Object.entries(obj.foo).reduce((acc: any, [key, value]: [string, User]) => ({\n"
111+
+ " ...acc,\n"
112+
+ " [key]:\n"
113+
+ " User.filterSensitiveLog(value)\n"
114+
+ " ,\n"
115+
+ " }), {})\n"
116+
+ " }),\n"
117+
+ " })\n");
118+
}
119+
120+
@Test
121+
public void addsfilterSensitiveLogForMapSelf() {
122+
testStructureCodegen("sensitive-test-map-self.smithy",
123+
" export const filterSensitiveLog = (obj: GetFooInput): any => ({\n"
124+
+ " ...obj,\n"
125+
+ " ...(obj.foo && { foo:\n"
126+
+ " SENSITIVE_STRING\n"
127+
+ " }),\n"
128+
+ " })\n");
129+
}
130+
131+
private String testStructureCodegen(String file, String expectedType) {
47132
Model model = Model.assembler()
48133
.addImport(getClass().getResource(file))
49134
.assemble()
@@ -53,18 +138,24 @@ public void testErrorStructureCodegen(String file, String expectedType) {
53138
.model(model)
54139
.fileManifest(manifest)
55140
.settings(Node.objectNodeBuilder()
56-
.withMember("service", Node.from("smithy.example#Example"))
57-
.withMember("package", Node.from("example"))
58-
.withMember("packageVersion", Node.from("1.0.0"))
59-
.build())
141+
.withMember("service", Node.from("smithy.example#Example"))
142+
.withMember("package", Node.from("example"))
143+
.withMember("packageVersion", Node.from("1.0.0"))
144+
.build())
60145
.build();
61146

62147
new TypeScriptCodegenPlugin().execute(context);
63148
String contents = manifest.getFileString("/models/index.ts").get();
64149

150+
assertThat(contents, containsString(expectedType));
151+
return contents;
152+
}
153+
154+
private void testErrorStructureCodegen(String file, String expectedType) {
155+
String contents = testStructureCodegen(file, expectedType);
156+
65157
assertThat(contents, containsString("as __isa"));
66158
assertThat(contents, containsString("as __SmithyException"));
67-
assertThat(contents, containsString(expectedType));
68159
assertThat(contents, containsString("namespace Err {"));
69160
assertThat(contents, containsString(" export const isa = (o: any): o is Err => "
70161
+ "__isa(o, \"Err\");\n"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
@required
13+
@sensitive
14+
foo: UserList
15+
}
16+
17+
list UserList {
18+
member: User
19+
}
20+
21+
structure User {
22+
@required
23+
username: String,
24+
25+
@sensitive
26+
password: String
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
@required
13+
foo: UserList
14+
}
15+
16+
list UserList {
17+
member: User
18+
}
19+
20+
structure User {
21+
@required
22+
username: String,
23+
24+
@sensitive
25+
password: String
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
@required
13+
@sensitive
14+
foo: UserMap
15+
}
16+
17+
map UserMap {
18+
key: String,
19+
value: User
20+
}
21+
22+
structure User {
23+
@required
24+
username: String,
25+
26+
@sensitive
27+
password: String
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
@required
13+
foo: UserMap
14+
}
15+
16+
map UserMap {
17+
key: String,
18+
value: User
19+
}
20+
21+
structure User {
22+
@required
23+
username: String,
24+
25+
@sensitive
26+
password: String
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
@required
13+
username: String,
14+
15+
@sensitive
16+
password: String
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
@required
13+
@sensitive
14+
foo: User
15+
}
16+
17+
structure User {
18+
@required
19+
username: String,
20+
21+
@sensitive
22+
password: String
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace smithy.example
2+
3+
@protocols([{name: "aws.rest-json-1.1"}])
4+
service Example {
5+
version: "1.0.0",
6+
operations: [GetFoo]
7+
}
8+
9+
operation GetFoo(GetFooInput)
10+
11+
structure GetFooInput {
12+
@required
13+
foo: User
14+
}
15+
16+
structure User {
17+
@required
18+
username: String,
19+
20+
@sensitive
21+
password: String
22+
}

0 commit comments

Comments
 (0)