-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Write response deserializer for awsJson (#551)
- Loading branch information
Showing
7 changed files
with
261 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
smithy-swift-codegen/src/test/kotlin/serde/awsjson11/OutputResponseDeserializerTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package serde.awsjson11 | ||
|
||
import TestContext | ||
import asSmithy | ||
import defaultSettings | ||
import getModelFileContents | ||
import io.kotest.matchers.string.shouldContainOnlyOnce | ||
import newTestContext | ||
import org.junit.jupiter.api.Test | ||
import shouldSyntacticSanityCheck | ||
import software.amazon.smithy.swift.codegen.model.AddOperationShapes | ||
|
||
// NOTE: protocol conformance is mostly handled by the protocol tests suite | ||
class OutputResponseDeserializerTests { | ||
private var model = javaClass.getResource("awsjson-output-response-deserializer.smithy").asSmithy() | ||
private fun newTestContext(): TestContext { | ||
val settings = model.defaultSettings() | ||
model = AddOperationShapes.execute(model, settings.getService(model), settings.moduleName) | ||
return model.newTestContext() | ||
} | ||
|
||
val newTestContext = newTestContext() | ||
|
||
init { | ||
newTestContext.generator.generateSerializers(newTestContext.generationCtx) | ||
newTestContext.generator.generateProtocolClient(newTestContext.generationCtx) | ||
newTestContext.generator.generateDeserializers(newTestContext.generationCtx) | ||
newTestContext.generator.generateCodableConformanceForNestedTypes(newTestContext.generationCtx) | ||
newTestContext.generationCtx.delegator.flushWriters() | ||
} | ||
|
||
@Test | ||
fun `it creates correct init for simple structure payloads`() { | ||
val contents = getModelFileContents( | ||
"example", | ||
"SimpleStructureOutputResponse+HttpResponseBinding.swift", | ||
newTestContext.manifest | ||
) | ||
contents.shouldSyntacticSanityCheck() | ||
val expectedContents = | ||
""" | ||
extension SimpleStructureOutputResponse: ClientRuntime.HttpResponseBinding { | ||
public init (httpResponse: ClientRuntime.HttpResponse, decoder: ClientRuntime.ResponseDecoder? = nil) throws { | ||
if let data = try httpResponse.body.toData(), | ||
let responseDecoder = decoder { | ||
let output: SimpleStructureOutputResponseBody = try responseDecoder.decode(responseBody: data) | ||
self.name = output.name | ||
self.number = output.number | ||
} else { | ||
self.name = nil | ||
self.number = nil | ||
} | ||
} | ||
} | ||
""".trimIndent() | ||
contents.shouldContainOnlyOnce(expectedContents) | ||
} | ||
|
||
@Test | ||
fun `it creates correct init for data streaming payloads`() { | ||
val contents = getModelFileContents( | ||
"example", | ||
"DataStreamingOutputResponse+HttpResponseBinding.swift", | ||
newTestContext.manifest | ||
) | ||
contents.shouldSyntacticSanityCheck() | ||
val expectedContents = | ||
""" | ||
extension DataStreamingOutputResponse: ClientRuntime.HttpResponseBinding { | ||
public init (httpResponse: ClientRuntime.HttpResponse, decoder: ClientRuntime.ResponseDecoder? = nil) throws { | ||
switch httpResponse.body { | ||
case .data(let data): | ||
self.streamingData = .data(data) | ||
case .stream(let stream): | ||
self.streamingData = .stream(stream) | ||
case .none: | ||
self.streamingData = nil | ||
} | ||
} | ||
} | ||
""".trimIndent() | ||
contents.shouldContainOnlyOnce(expectedContents) | ||
} | ||
|
||
@Test | ||
fun `it creates correct init for event streaming payloads`() { | ||
val contents = getModelFileContents( | ||
"example", | ||
"EventStreamingOutputResponse+HttpResponseBinding.swift", | ||
newTestContext.manifest | ||
) | ||
contents.shouldSyntacticSanityCheck() | ||
val expectedContents = | ||
""" | ||
extension EventStreamingOutputResponse: ClientRuntime.HttpResponseBinding { | ||
public init (httpResponse: ClientRuntime.HttpResponse, decoder: ClientRuntime.ResponseDecoder? = nil) throws { | ||
if case let .stream(stream) = httpResponse.body, let responseDecoder = decoder { | ||
let messageDecoder: ClientRuntime.MessageDecoder? = nil | ||
let decoderStream = ClientRuntime.EventStream.DefaultMessageDecoderStream<EventStream>(stream: stream, messageDecoder: messageDecoder, responseDecoder: responseDecoder) | ||
self.eventStream = decoderStream.toAsyncStream() | ||
} else { | ||
self.eventStream = nil | ||
} | ||
} | ||
} | ||
""".trimIndent() | ||
contents.shouldContainOnlyOnce(expectedContents) | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...ft-codegen/src/test/resources/serde/awsjson11/awsjson-output-response-deserializer.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
$version: "2.0" | ||
namespace com.test | ||
|
||
use aws.protocols#awsJson1_1 | ||
|
||
@awsJson1_1 | ||
service Example { | ||
version: "1.0.0", | ||
operations: [ | ||
SimpleStructure, | ||
DataStreaming, | ||
EventStreaming | ||
] | ||
} | ||
|
||
@http(method: "PUT", uri: "/SimpleStructure") | ||
operation SimpleStructure { | ||
input: Input | ||
output: SimpleStructureOutput | ||
} | ||
|
||
structure Input {} | ||
|
||
structure SimpleStructureOutput { | ||
name: Name | ||
number: Number | ||
} | ||
|
||
string Name | ||
|
||
integer Number | ||
|
||
@http(method: "PUT", uri: "/DataStreaming") | ||
operation DataStreaming { | ||
input: Input | ||
output: DataStreamingOutput | ||
} | ||
|
||
structure DataStreamingOutput { | ||
@required | ||
streamingData: StreamingData | ||
} | ||
|
||
@streaming | ||
blob StreamingData | ||
|
||
@http(method: "PUT", uri: "/EventStreaming") | ||
operation EventStreaming { | ||
input: Input | ||
output: EventStreamingOutput | ||
} | ||
|
||
structure EventStreamingOutput { | ||
@required | ||
eventStream: EventStream | ||
} | ||
|
||
@streaming | ||
union EventStream { | ||
eventA: EventA | ||
eventB: EventB | ||
} | ||
|
||
structure EventA {} | ||
|
||
structure EventB {} |