Skip to content

Commit

Permalink
handles unknown field in json body while deserializing
Browse files Browse the repository at this point in the history
  • Loading branch information
skotambkar committed Jun 1, 2020
1 parent 4348d9a commit 60c29a0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
20 changes: 16 additions & 4 deletions aws/protocol/json/decoder_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ func GetErrorInfo(decoder *json.Decoder) (string, string, error) {
typeCode = v.(string)
break
default:
for decoder.More() {
// discard tokens for unknown field
decoder.Token()
}
DiscardUnknownField(decoder)
break
}
}

Expand Down Expand Up @@ -94,3 +92,17 @@ func SanitizeErrorCode(errorCode string) string {

return errorCode
}


// DiscardUnknownField discards unknown fields from decoder body.
// This function is useful while deserializing json body with additional
// unknown information that should be discarded.
func DiscardUnknownField(decoder *json.Decoder) {
v, _ := decoder.Token()
if _, ok := v.(json.Delim); ok {
for decoder.More() {
DiscardUnknownField(decoder)
}
decoder.Token()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -818,20 +818,27 @@ private void generateDocumentBindingStructureShapeDeserializer(
() -> {
writer.write("t, err := decoder.Token()");
writer.write("if err != nil { return err }");
for (MemberShape memberShape : shape.members()) {
if (!filterMemberShapes.test(memberShape)) {
continue;
writer.openBlock("switch t {", "}", () -> {
for (MemberShape memberShape : shape.members()) {
if (!filterMemberShapes.test(memberShape)) {
continue;
}

String memberName = symbolProvider.toMemberName(memberShape);
writer.openBlock("case $S :", "", memberShape.getMemberName(), () -> {
String operand = generateDocumentBindingMemberShapeDeserializer(writer, model, symbolProvider, memberShape);
writer.write(String.format("v.%s = %s", memberName, operand));
writer.write("break");
});
}

String memberName = symbolProvider.toMemberName(memberShape);
writer.write("");
writer.openBlock("if t == $S {", "}",
memberShape.getMemberName(), () -> {
String operand = generateDocumentBindingMemberShapeDeserializer(
writer, model, symbolProvider, memberShape);
writer.write(String.format("v.%s = %s", memberName, operand));
});
}
// default case to handle unknown fields
writer.openBlock("default : ","", () -> {
writer.addUseImports(GoDependency.AWS_JSON_PROTOCOL_ALIAS);
writer.write("jsonprotocol.DiscardUnknownField(decoder)");
writer.write("break");
});
});
});
writeJsonTokenizerEndStub(writer, shape);
}
Expand Down Expand Up @@ -1124,6 +1131,7 @@ private void writeJsonTokenizerEndStub(GoWriter writer, Shape shape) {
});
}


@Override
public void generateSharedDeserializerComponents(GenerationContext context) {
super.generateSharedDeserializerComponents(context);
Expand Down

0 comments on commit 60c29a0

Please sign in to comment.