From a7843adf6f3905df5f2e003e82889008c950b6eb Mon Sep 17 00:00:00 2001 From: George Fu Date: Wed, 24 Jul 2024 19:48:22 -0400 Subject: [PATCH] fix(event-streams): use member name as :event-type header value (#1349) * fix(event-streams): use member name as :event-type header value * comparator --- .../integration/EventStreamGenerator.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGenerator.java index 0b6ced0f3fc..cdd717ef8ad 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/EventStreamGenerator.java @@ -16,6 +16,7 @@ package software.amazon.smithy.typescript.codegen.integration; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.TreeSet; @@ -43,6 +44,7 @@ import software.amazon.smithy.typescript.codegen.TypeScriptWriter; import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext; import software.amazon.smithy.typescript.codegen.knowledge.SerdeElisionIndex; +import software.amazon.smithy.utils.Pair; import software.amazon.smithy.utils.SmithyUnstableApi; /** @@ -102,15 +104,21 @@ public void generateEventStreamSerializers( TopDownIndex topDownIndex = TopDownIndex.of(model); Set operations = topDownIndex.getContainedOperations(service); TreeSet eventUnionsToSerialize = new TreeSet<>(); - TreeSet eventShapesToMarshall = new TreeSet<>(); + TreeSet> eventShapesToMarshall = new TreeSet<>( + (a, b) -> Objects.compare(a.getRight(), b.getRight(), StructureShape::compareTo) + ); + for (OperationShape operation : operations) { if (hasEventStreamInput(context, operation)) { UnionShape eventsUnion = getEventStreamInputShape(context, operation); eventUnionsToSerialize.add(eventsUnion); - Set eventShapes = eventsUnion.members().stream() - .map(member -> model.expectShape(member.getTarget()).asStructureShape().get()) - .collect(Collectors.toSet()); - eventShapes.forEach(eventShapesToMarshall::add); + eventsUnion.members() + .forEach(member -> { + eventShapesToMarshall.add(Pair.of( + member.getMemberName(), + model.expectShape(member.getTarget()).asStructureShape().get() + )); + }); } } @@ -118,14 +126,16 @@ public void generateEventStreamSerializers( generateEventStreamSerializer(context, eventsUnion); }); SerdeElisionIndex serdeElisionIndex = SerdeElisionIndex.of(model); - eventShapesToMarshall.forEach(event -> { + eventShapesToMarshall.forEach(memberNameAndEvent -> { generateEventMarshaller( context, - event, + memberNameAndEvent.getLeft(), + memberNameAndEvent.getRight(), documentContentType, serializeInputEventDocumentPayload, documentShapesToSerialize, - serdeElisionIndex); + serdeElisionIndex + ); }); } @@ -155,6 +165,7 @@ public void generateEventStreamDeserializers( Set operations = topDownIndex.getContainedOperations(service); TreeSet eventUnionsToDeserialize = new TreeSet<>(); TreeSet eventShapesToUnmarshall = new TreeSet<>(); + for (OperationShape operation : operations) { if (hasEventStreamOutput(context, operation)) { UnionShape eventsUnion = getEventStreamOutputShape(context, operation); @@ -237,6 +248,7 @@ private Symbol getSymbol(GenerationContext context, Shape shape) { public void generateEventMarshaller( GenerationContext context, + String memberName, StructureShape event, String documentContentType, Runnable serializeInputEventDocumentPayload, @@ -253,7 +265,7 @@ public void generateEventMarshaller( + "): __Message => {", "}", methodName, symbol, () -> { writer.openBlock("const headers: __MessageHeaders = {", "}", () -> { //fix headers required by event stream - writer.write("\":event-type\": { type: \"string\", value: $S },", symbol.getName()); + writer.write("\":event-type\": { type: \"string\", value: $S },", memberName); writer.write("\":message-type\": { type: \"string\", value: \"event\" },"); writeEventContentTypeHeader(context, event, documentContentType); });