Skip to content

Commit

Permalink
handle unset unions
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Sep 25, 2024
1 parent 89fedbc commit 6b920c6
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2466,16 +2466,29 @@ private HttpBinding readPayload(
// If payload is a Union, then we need to parse the string into JavaScript object.
importUnionDeserializer(writer);
writer.write("const data: Record<string, any> | undefined "
+ "= __expectUnion(await parseBody(output.body, context));");
+ "= await parseBody(output.body, context);");
} else if (target instanceof StringShape || target instanceof DocumentShape) {
// If payload is String or Document, we need to collect body and convert binary to string.
writer.write("const data: any = await collectBodyString(output.body, context);");
} else {
throw new CodegenException(String.format("Unexpected shape type bound to payload: `%s`",
target.getType()));
}
writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context,

if (target instanceof UnionShape) {
writer.openBlock(
"if (Object.keys(data ?? {}).length) {",
"}",
() -> {
writer.write("contents.$L = __expectUnion($L);", binding.getMemberName(), getOutputValue(context,
Location.PAYLOAD, "data", binding.getMember(), target));
}
);
} else {
writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context,
Location.PAYLOAD, "data", binding.getMember(), target));
}

return binding;
}

Expand Down

0 comments on commit 6b920c6

Please sign in to comment.