Skip to content

Commit

Permalink
Add compilation test
Browse files Browse the repository at this point in the history
  • Loading branch information
lauzadis committed Aug 8, 2024
1 parent 417f843 commit 8281792
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/compile/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
testImplementation(project(":runtime:protocol:http"))
testImplementation(project(":runtime:protocol:http-client-engines:http-client-engine-default"))
testImplementation(project(":runtime:serde:serde-json"))
testImplementation(project(":runtime:serde:serde-xml"))
testImplementation(project(":runtime:observability:telemetry-api"))
testImplementation(project(":runtime:observability:telemetry-defaults"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,55 @@ class SmithySdkTest {

assertEquals(KotlinCompilation.ExitCode.OK, compilationResult.exitCode, compileOutputStream.toString())
}

// https://github.com/smithy-lang/smithy-kotlin/issues/1125
@Test
fun `it compiles models with string enums`() {
val model = """
namespace com.test
use aws.protocols#restXml
@restXml
service Example {
version: "1.0.0",
operations: [
Foo,
]
}
@http(method: "POST", uri: "/foo-no-input")
operation Foo {
output: FooResponse
}
structure FooResponse {
payload: StringBasedEnumList
}
@enum([
{
value: "blarg",
name: "Blarg"
},
{
value: "blergh",
name: "Blergh"
}
])
string StringBasedEnum
list StringBasedEnumList {
member: StringBasedEnum
}
""".asSmithy()

val compileOutputStream = ByteArrayOutputStream()
val compilationResult = compileSdkAndTest(model = model, outputSink = compileOutputStream, emitSourcesToTmp = Debug.emitSourcesToTemp)
compileOutputStream.flush()

assertEquals(KotlinCompilation.ExitCode.OK, compilationResult.exitCode, compileOutputStream.toString())
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package software.amazon.smithy.kotlin.codegen.util

import software.amazon.smithy.aws.traits.protocols.RestJson1Trait
import software.amazon.smithy.aws.traits.protocols.RestXmlTrait
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes
import software.amazon.smithy.kotlin.codegen.core.withBlock
Expand All @@ -20,7 +21,10 @@ import software.amazon.smithy.model.traits.TimestampFormatTrait
* Integration that registers protocol generators this package provides
*/
class CodegenTestIntegration : KotlinIntegration {
override val protocolGenerators: List<ProtocolGenerator> = listOf(RestJsonTestProtocolGenerator())
override val protocolGenerators: List<ProtocolGenerator> = listOf(
RestJsonTestProtocolGenerator(),
RestXmlTestProtocolGenerator()
)
}

/**
Expand Down Expand Up @@ -70,3 +74,48 @@ class MockRestJsonProtocolClientGenerator(
middleware: List<ProtocolMiddleware>,
httpBindingResolver: HttpBindingResolver,
) : HttpProtocolClientGenerator(ctx, middleware, httpBindingResolver)

/**
* A partial ProtocolGenerator to generate minimal sdks for tests of restXml models.
*/
class RestXmlTestProtocolGenerator(
override val defaultTimestampFormat: TimestampFormatTrait.Format = TimestampFormatTrait.Format.EPOCH_SECONDS,
override val protocol: ShapeId = RestXmlTrait.ID,
) : HttpBindingProtocolGenerator() {

override fun getProtocolHttpBindingResolver(model: Model, serviceShape: ServiceShape): HttpBindingResolver =
HttpTraitResolver(model, serviceShape, ProtocolContentTypes.consistent("application/xml"))

override fun generateProtocolUnitTests(ctx: ProtocolGenerator.GenerationContext) {
// NOOP
}

override fun getHttpProtocolClientGenerator(ctx: ProtocolGenerator.GenerationContext): HttpProtocolClientGenerator =
MockRestXmlProtocolClientGenerator(ctx, getHttpMiddleware(ctx), getProtocolHttpBindingResolver(ctx.model, ctx.service))

override fun structuredDataSerializer(ctx: ProtocolGenerator.GenerationContext): StructuredDataSerializerGenerator =
XmlSerializerGenerator(this, TimestampFormatTrait.Format.EPOCH_SECONDS)

override fun structuredDataParser(ctx: ProtocolGenerator.GenerationContext): StructuredDataParserGenerator =
XmlParserGenerator(TimestampFormatTrait.Format.EPOCH_SECONDS)

override fun operationErrorHandler(ctx: ProtocolGenerator.GenerationContext, op: OperationShape): Symbol =
op.errorHandler(ctx.settings) { writer ->
writer.withBlock(
"private fun ${op.errorHandlerName()}(context: #T, call: #T, payload: #T?): Nothing {",
"}",
RuntimeTypes.Core.ExecutionContext,
RuntimeTypes.Http.HttpCall,
KotlinTypes.ByteArray,
) {
write("error(\"not needed for compile tests\")")
}
}
}

class MockRestXmlProtocolClientGenerator(
ctx: ProtocolGenerator.GenerationContext,
middleware: List<ProtocolMiddleware>,
httpBindingResolver: HttpBindingResolver,
) : HttpProtocolClientGenerator(ctx, middleware, httpBindingResolver)

0 comments on commit 8281792

Please sign in to comment.