Skip to content

Commit 718270e

Browse files
sichanyooSichan Yoojbelkins
authored
fix: Make operations correctly model service errors (#581)
* Plug service errors into pre-existing error code-gen for initializers, deserializers, struct, body, and codable conformance of nested members --------- Co-authored-by: Sichan Yoo <[email protected]> Co-authored-by: Josh Elkins <[email protected]>
1 parent d0269d2 commit 718270e

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpBindingProtocolGenerator.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,16 @@ abstract class HttpBindingProtocolGenerator : ProtocolGenerator {
322322
.flatMap { it.errors }
323323
.map { ctx.model.expectShape(it) }
324324
.toSet()
325-
return operationErrorShapes.filter { shapes -> shapes.members().any { it.isInHttpBody() } }.toMutableSet()
325+
326+
val serviceErrorShapes = ctx.service.errors.map {
327+
ctx.model.expectShape(it)
328+
}.toSet()
329+
330+
return operationErrorShapes.filter { shape ->
331+
shape.members().any { it.isInHttpBody() }
332+
}.toMutableSet() + serviceErrorShapes.filter { shape ->
333+
shape.members().any { it.isInHttpBody() }
334+
}.toMutableSet()
326335
}
327336

328337
private fun resolveShapesNeedingCodableConformance(ctx: ProtocolGenerator.GenerationContext): Set<Shape> {
@@ -342,6 +351,12 @@ abstract class HttpBindingProtocolGenerator : ProtocolGenerator {
342351
.filter { it.isStructureShape || it.isUnionShape || it is CollectionShape || it.isMapShape }
343352
.toSet()
344353

354+
val topLevelServiceErrorMembers = ctx.service.errors
355+
.flatMap { ctx.model.expectShape(it).members() }
356+
.map { ctx.model.expectShape(it.target) }
357+
.filter { it.isStructureShape || it.isUnionShape || it is CollectionShape || it.isMapShape }
358+
.toSet()
359+
345360
val topLevelInputMembers = getHttpBindingOperations(ctx).flatMap {
346361
val inputShape = ctx.model.expectShape(it.input.get())
347362
inputShape.members()
@@ -350,7 +365,11 @@ abstract class HttpBindingProtocolGenerator : ProtocolGenerator {
350365
.filter { it.isStructureShape || it.isUnionShape || it is CollectionShape || it.isMapShape }
351366
.toSet()
352367

353-
val allTopLevelMembers = topLevelOutputMembers.union(topLevelErrorMembers).union(topLevelInputMembers)
368+
val allTopLevelMembers =
369+
topLevelOutputMembers
370+
.union(topLevelErrorMembers)
371+
.union(topLevelServiceErrorMembers)
372+
.union(topLevelInputMembers)
354373

355374
val nestedTypes = walkNestedShapesRequiringSerde(ctx, allTopLevelMembers)
356375
return nestedTypes

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/httpResponse/HttpResponseBindingErrorGeneratable.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ import software.amazon.smithy.model.shapes.OperationShape
1010
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
1111

1212
interface HttpResponseBindingErrorGeneratable {
13-
fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, unknownServiceErrorSymbol: Symbol)
13+
fun renderServiceError(ctx: ProtocolGenerator.GenerationContext)
14+
fun renderOperationError(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, unknownServiceErrorSymbol: Symbol)
1415
}

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/httpResponse/HttpResponseGenerator.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,23 @@ class HttpResponseGenerator(
3333
}
3434
}
3535

36+
if (ctx.service.errors.isNotEmpty()) {
37+
httpResponseBindingErrorGenerator.renderServiceError(ctx)
38+
}
3639
httpOperations.forEach {
37-
httpResponseBindingErrorGenerator.render(ctx, it, unknownServiceErrorSymbol)
40+
httpResponseBindingErrorGenerator.renderOperationError(ctx, it, unknownServiceErrorSymbol)
3841
}
3942

40-
val modeledErrors = httpOperations.flatMap { it.errors }.map { ctx.model.expectShape(it) as StructureShape }.toSet()
43+
val modeledOperationErrors = httpOperations
44+
.flatMap { it.errors }
45+
.map { ctx.model.expectShape(it) as StructureShape }
46+
.toSet()
47+
48+
val modeledServiceErrors = ctx.service.errors
49+
.map { ctx.model.expectShape(it) as StructureShape }
50+
.toSet()
51+
52+
val modeledErrors = modeledOperationErrors + modeledServiceErrors
4153
modeledErrors.forEach {
4254
httpResponseBindingErrorInitGenerator(ctx, it, httpBindingResolver, defaultTimestampFormat)
4355
}

smithy-swift-codegen/src/test/kotlin/mocks/MockHttpResponseBindingErrorGenerator.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import software.amazon.smithy.swift.codegen.integration.httpResponse.HttpRespons
1313
import software.amazon.smithy.swift.codegen.integration.middlewares.handlers.MiddlewareShapeUtils
1414

1515
class MockHttpResponseBindingErrorGenerator : HttpResponseBindingErrorGeneratable {
16-
override fun render(
16+
override fun renderOperationError(
1717
ctx: ProtocolGenerator.GenerationContext,
1818
op: OperationShape,
1919
unknownServiceErrorSymbol: Symbol
@@ -34,4 +34,16 @@ class MockHttpResponseBindingErrorGenerator : HttpResponseBindingErrorGeneratabl
3434
}
3535
}
3636
}
37+
38+
override fun renderServiceError(ctx: ProtocolGenerator.GenerationContext) {
39+
/*
40+
41+
TODO(
42+
"Organize test suites of smithy-swift and aws-sdk-swift " +
43+
"and see if this class and consumers of this class should be moved to aws-sdk-swift " +
44+
"OR AWS protocol tests in aws-sdk-swift should be moved to smithy-swift."
45+
)
46+
47+
*/
48+
}
3749
}

0 commit comments

Comments
 (0)