Skip to content

Commit

Permalink
feat(codegen): assert sets have no duplicates (#2764)
Browse files Browse the repository at this point in the history
* feat(codegen): assert sets have no duplicates

* chore(codegen): update generated code
  • Loading branch information
JordonPhillips committed Sep 9, 2021
1 parent 1159680 commit aa62fc3
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 18 deletions.
9 changes: 8 additions & 1 deletion clients/client-app-mesh/protocols/Aws_restJson1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8488,13 +8488,20 @@ const deserializeAws_restJson1PortMapping = (output: any, context: __SerdeContex
};

const deserializeAws_restJson1PortSet = (output: any, context: __SerdeContext): number[] => {
const uniqueValues = new Set<any>();
return (output || [])
.filter((e: any) => e != null)
.map((entry: any) => {
if (entry === null) {
return null as any;
}
return __expectInt32(entry) as any;
const parsedEntry = __expectInt32(entry) as any;
if (uniqueValues.has(parsedEntry)) {
throw new TypeError('All elements of the set "com.amazonaws.appmesh#PortSet" must be unique.');
} else {
uniqueValues.add(parsedEntry);
return parsedEntry;
}
});
};

Expand Down
18 changes: 16 additions & 2 deletions clients/client-codeguruprofiler/protocols/Aws_restJson1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3132,24 +3132,38 @@ const deserializeAws_restJson1Channel = (output: any, context: __SerdeContext):
};

const deserializeAws_restJson1Channels = (output: any, context: __SerdeContext): Channel[] => {
const uniqueValues = new Set<any>();
return (output || [])
.filter((e: any) => e != null)
.map((entry: any) => {
if (entry === null) {
return null as any;
}
return deserializeAws_restJson1Channel(entry, context);
const parsedEntry = deserializeAws_restJson1Channel(entry, context);
if (uniqueValues.has(parsedEntry)) {
throw new TypeError('All elements of the set "com.amazonaws.codeguruprofiler#Channels" must be unique.');
} else {
uniqueValues.add(parsedEntry);
return parsedEntry;
}
});
};

const deserializeAws_restJson1EventPublishers = (output: any, context: __SerdeContext): (EventPublisher | string)[] => {
const uniqueValues = new Set<any>();
return (output || [])
.filter((e: any) => e != null)
.map((entry: any) => {
if (entry === null) {
return null as any;
}
return __expectString(entry) as any;
const parsedEntry = __expectString(entry) as any;
if (uniqueValues.has(parsedEntry)) {
throw new TypeError('All elements of the set "com.amazonaws.codeguruprofiler#EventPublishers" must be unique.');
} else {
uniqueValues.add(parsedEntry);
return parsedEntry;
}
});
};

Expand Down
42 changes: 38 additions & 4 deletions clients/client-ssm-incidents/protocols/Aws_restJson1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3993,24 +3993,42 @@ const deserializeAws_restJson1AutomationExecutionSet = (
output: any,
context: __SerdeContext
): AutomationExecution[] => {
const uniqueValues = new Set<any>();
return (output || [])
.filter((e: any) => e != null)
.map((entry: any) => {
if (entry === null) {
return null as any;
}
return deserializeAws_restJson1AutomationExecution(entry, context);
const parsedEntry = deserializeAws_restJson1AutomationExecution(entry, context);
if (uniqueValues.has(parsedEntry)) {
throw new TypeError(
'All elements of the set "com.amazonaws.ssmincidents#AutomationExecutionSet" must be unique.'
);
} else {
uniqueValues.add(parsedEntry);
return parsedEntry;
}
});
};

const deserializeAws_restJson1ChatbotSnsConfigurationSet = (output: any, context: __SerdeContext): string[] => {
const uniqueValues = new Set<any>();
return (output || [])
.filter((e: any) => e != null)
.map((entry: any) => {
if (entry === null) {
return null as any;
}
return __expectString(entry) as any;
const parsedEntry = __expectString(entry) as any;
if (uniqueValues.has(parsedEntry)) {
throw new TypeError(
'All elements of the set "com.amazonaws.ssmincidents#ChatbotSnsConfigurationSet" must be unique.'
);
} else {
uniqueValues.add(parsedEntry);
return parsedEntry;
}
});
};

Expand All @@ -4033,13 +4051,20 @@ const deserializeAws_restJson1EmptyChatChannel = (output: any, context: __SerdeC
};

const deserializeAws_restJson1EngagementSet = (output: any, context: __SerdeContext): string[] => {
const uniqueValues = new Set<any>();
return (output || [])
.filter((e: any) => e != null)
.map((entry: any) => {
if (entry === null) {
return null as any;
}
return __expectString(entry) as any;
const parsedEntry = __expectString(entry) as any;
if (uniqueValues.has(parsedEntry)) {
throw new TypeError('All elements of the set "com.amazonaws.ssmincidents#EngagementSet" must be unique.');
} else {
uniqueValues.add(parsedEntry);
return parsedEntry;
}
});
};

Expand Down Expand Up @@ -4204,13 +4229,22 @@ const deserializeAws_restJson1NotificationTargetSet = (
output: any,
context: __SerdeContext
): NotificationTargetItem[] => {
const uniqueValues = new Set<any>();
return (output || [])
.filter((e: any) => e != null)
.map((entry: any) => {
if (entry === null) {
return null as any;
}
return deserializeAws_restJson1NotificationTargetItem(entry, context);
const parsedEntry = deserializeAws_restJson1NotificationTargetItem(entry, context);
if (uniqueValues.has(parsedEntry)) {
throw new TypeError(
'All elements of the set "com.amazonaws.ssmincidents#NotificationTargetSet" must be unique.'
);
} else {
uniqueValues.add(parsedEntry);
return parsedEntry;
}
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,6 @@ private static boolean filterMalformedRequestTests(
if (testCase.getId().startsWith("RestJsonMalformedUnion")) {
return true;
}
//TODO: we don't do any set validation
if (testCase.getId().startsWith("RestJsonMalformedSet")) {
return true;
}
//TODO: we don't do any list validation
if (testCase.getId().startsWith("RestJsonBodyMalformedList")) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,27 @@ protected void deserializeCollection(GenerationContext context, CollectionShape
potentialFilter = ".filter((e: any) => e != null)";
}

if (shape.isSetShape()) {
writer.write("const uniqueValues = new Set<any>();");
}

writer.openBlock("return (output || [])$L.map((entry: any) => {", "});", potentialFilter, () -> {
// Short circuit null values from serialization.
writer.write("if (entry === null) { return null as any; }");

// Dispatch to the output value provider for any additional handling.
writer.write("return $L$L;",
target.accept(getMemberVisitor(shape.getMember(), "entry")),
usesExpect(target) ? " as any" : "");
if (shape.isSetShape()) {
writer.write("const parsedEntry = $L$L;",
target.accept(getMemberVisitor(shape.getMember(), "entry")),
usesExpect(target) ? " as any" : "");
writer.write("if (uniqueValues.has(parsedEntry)) { throw new "
+ "TypeError('All elements of the set $S must be unique.'); } else { "
+ "uniqueValues.add(parsedEntry)\nreturn parsedEntry; }",
shape.getId());
} else {
// Dispatch to the output value provider for any additional handling.
writer.write("return $L$L;", target.accept(getMemberVisitor(shape.getMember(), "entry")),
usesExpect(target) ? " as any" : "");
}
});
}

Expand Down
9 changes: 8 additions & 1 deletion protocol_tests/aws-json/protocols/Aws_json1_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,13 +1653,20 @@ const deserializeAws_json1_1FooEnumMap = (
};

const deserializeAws_json1_1FooEnumSet = (output: any, context: __SerdeContext): (FooEnum | string)[] => {
const uniqueValues = new Set<any>();
return (output || [])
.filter((e: any) => e != null)
.map((entry: any) => {
if (entry === null) {
return null as any;
}
return __expectString(entry) as any;
const parsedEntry = __expectString(entry) as any;
if (uniqueValues.has(parsedEntry)) {
throw new TypeError('All elements of the set "aws.protocoltests.shared#FooEnumSet" must be unique.');
} else {
uniqueValues.add(parsedEntry);
return parsedEntry;
}
});
};

Expand Down
18 changes: 16 additions & 2 deletions protocol_tests/aws-restjson/protocols/Aws_restJson1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4704,13 +4704,20 @@ const deserializeAws_restJson1FooEnumMap = (
};

const deserializeAws_restJson1FooEnumSet = (output: any, context: __SerdeContext): (FooEnum | string)[] => {
const uniqueValues = new Set<any>();
return (output || [])
.filter((e: any) => e != null)
.map((entry: any) => {
if (entry === null) {
return null as any;
}
return __expectString(entry) as any;
const parsedEntry = __expectString(entry) as any;
if (uniqueValues.has(parsedEntry)) {
throw new TypeError('All elements of the set "aws.protocoltests.shared#FooEnumSet" must be unique.');
} else {
uniqueValues.add(parsedEntry);
return parsedEntry;
}
});
};

Expand Down Expand Up @@ -4787,13 +4794,20 @@ const deserializeAws_restJson1StringMap = (output: any, context: __SerdeContext)
};

const deserializeAws_restJson1StringSet = (output: any, context: __SerdeContext): string[] => {
const uniqueValues = new Set<any>();
return (output || [])
.filter((e: any) => e != null)
.map((entry: any) => {
if (entry === null) {
return null as any;
}
return __expectString(entry) as any;
const parsedEntry = __expectString(entry) as any;
if (uniqueValues.has(parsedEntry)) {
throw new TypeError('All elements of the set "aws.protocoltests.shared#StringSet" must be unique.');
} else {
uniqueValues.add(parsedEntry);
return parsedEntry;
}
});
};

Expand Down

0 comments on commit aa62fc3

Please sign in to comment.