Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ build-java: ## Builds the Java code generation packages.
cd codegen && ./gradlew clean build


test-protocols: ## Generates and runs the restJson1 protocol tests.
cd codegen && ./gradlew :protocol-test:build
uv pip install codegen/protocol-test/build/smithyprojections/protocol-test/rest-json-1/python-client-codegen
uv run pytest codegen/protocol-test/build/smithyprojections/protocol-test/rest-json-1/python-client-codegen
test-protocols: ## Generates and runs protocol tests for all supported protocols.
cd codegen && ./gradlew :protocol-test:clean :protocol-test:build
@set -e; for projection_dir in codegen/protocol-test/build/smithyprojections/protocol-test/*/python-client-codegen; do \
uv pip install "$$projection_dir"; \
uv run pytest "$$projection_dir"; \
done


lint-py: ## Runs linters and formatters on the python packages.
Expand Down
1 change: 1 addition & 0 deletions codegen/aws/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ extra["moduleName"] = "software.amazon.smithy.python.aws.codegen"
dependencies {
implementation(project(":core"))
implementation(libs.smithy.aws.traits)
implementation(libs.smithy.protocol.test.traits)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.python.aws.codegen;

import java.util.Set;
import software.amazon.smithy.aws.traits.protocols.AwsJson1_0Trait;
import software.amazon.smithy.model.node.ArrayNode;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.python.codegen.ApplicationProtocol;
import software.amazon.smithy.python.codegen.GenerationContext;
import software.amazon.smithy.python.codegen.HttpProtocolTestGenerator;
import software.amazon.smithy.python.codegen.SymbolProperties;
import software.amazon.smithy.python.codegen.generators.ProtocolGenerator;
import software.amazon.smithy.python.codegen.writer.PythonWriter;
import software.amazon.smithy.utils.SmithyInternalApi;

@SmithyInternalApi
public final class AwsJson10ProtocolGenerator implements ProtocolGenerator {
private static final Set<String> TESTS_TO_SKIP = Set.of(
// These two tests essentially try to assert nan == nan,
// which is never true. We should update the generator to
// make specific assertions for these.
"AwsJson10SupportsNaNFloatInputs",

// TODO: support the request compression trait
// https://smithy.io/2.0/spec/behavior-traits.html#smithy-api-requestcompression-trait
"SDKAppliedContentEncoding_awsJson1_0",
"SDKAppendsGzipAndIgnoresHttpProvidedEncoding_awsJson1_0",

// TODO: Fix for both REST-JSON and JSON-RPC
"AwsJson10ClientPopulatesDefaultValuesInInput",
"AwsJson10ClientSkipsTopLevelDefaultValuesInInput",
"AwsJson10ClientUsesExplicitlyProvidedMemberValuesOverDefaults",
"AwsJson10ClientPopulatesDefaultsValuesWhenMissingInResponse",
"AwsJson10ClientIgnoresNonTopLevelDefaultsOnMembersWithClientOptional",
"AwsJson10ClientIgnoresDefaultValuesIfMemberValuesArePresentInResponse",

// TODO: support of the endpoint trait
"AwsJson10EndpointTraitWithHostLabel",
"AwsJson10EndpointTrait",

// TODO: support client error-correction behavior when the server
// omits required values in modeled error responses.
"AwsJson10ClientErrorCorrectsWhenServerFailsToSerializeRequiredValues",
"AwsJson10ClientErrorCorrectsWithDefaultValuesWhenServerFailsToSerializeRequiredValues");
Comment on lines +45 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't you say you fixed this?


@Override
public ShapeId getProtocol() {
return AwsJson1_0Trait.ID;
}

@Override
public ApplicationProtocol getApplicationProtocol(GenerationContext context) {
var service = context.settings().service(context.model());
var trait = service.expectTrait(AwsJson1_0Trait.class);
var config = ObjectNode.builder()
.withMember("http", ArrayNode.fromStrings(trait.getHttp()))
.withMember("eventStreamHttp", ArrayNode.fromStrings(trait.getEventStreamHttp()))
.build();
return ApplicationProtocol.createDefaultHttpApplicationProtocol(config);
}

@Override
public void initializeProtocol(GenerationContext context, PythonWriter writer) {
writer.addDependency(AwsPythonDependency.SMITHY_AWS_CORE.withOptionalDependencies("json"));
writer.addImport("smithy_aws_core.aio.protocols", "AwsJson10ClientProtocol");
var serviceSymbol = context.symbolProvider().toSymbol(context.settings().service(context.model()));
var serviceSchema = serviceSymbol.expectProperty(SymbolProperties.SCHEMA);
writer.write("AwsJson10ClientProtocol($T)", serviceSchema);
}

@Override
public void generateProtocolTests(GenerationContext context) {
context.writerDelegator()
.useFileWriter("./tests/test_awsjson10_protocol.py", "tests.test_awsjson10_protocol", writer -> {
new HttpProtocolTestGenerator(
context,
getProtocol(),
writer,
(shape, testCase) -> TESTS_TO_SKIP.contains(testCase.getId())).run();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.python.aws.codegen;

import java.util.Set;
import software.amazon.smithy.aws.traits.protocols.AwsJson1_1Trait;
import software.amazon.smithy.model.node.ArrayNode;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.python.codegen.ApplicationProtocol;
import software.amazon.smithy.python.codegen.GenerationContext;
import software.amazon.smithy.python.codegen.HttpProtocolTestGenerator;
import software.amazon.smithy.python.codegen.SymbolProperties;
import software.amazon.smithy.python.codegen.generators.ProtocolGenerator;
import software.amazon.smithy.python.codegen.writer.PythonWriter;
import software.amazon.smithy.utils.SmithyInternalApi;

@SmithyInternalApi
public final class AwsJson11ProtocolGenerator implements ProtocolGenerator {
private static final Set<String> TESTS_TO_SKIP = Set.of(
// These two tests essentially try to assert nan == nan,
// which is never true. We should update the generator to
// make specific assertions for these.
"AwsJson11SupportsNaNFloatInputs",

// TODO: support the request compression trait
// https://smithy.io/2.0/spec/behavior-traits.html#smithy-api-requestcompression-trait
"SDKAppliedContentEncoding_awsJson1_1",
"SDKAppendsGzipAndIgnoresHttpProvidedEncoding_awsJson1_1",

// TODO: support of the endpoint trait
"AwsJson11EndpointTraitWithHostLabel",
"AwsJson11EndpointTrait");

@Override
public ShapeId getProtocol() {
return AwsJson1_1Trait.ID;
}

@Override
public ApplicationProtocol getApplicationProtocol(GenerationContext context) {
var service = context.settings().service(context.model());
var trait = service.expectTrait(AwsJson1_1Trait.class);
var config = ObjectNode.builder()
.withMember("http", ArrayNode.fromStrings(trait.getHttp()))
.withMember("eventStreamHttp", ArrayNode.fromStrings(trait.getEventStreamHttp()))
.build();
return ApplicationProtocol.createDefaultHttpApplicationProtocol(config);
}

@Override
public void initializeProtocol(GenerationContext context, PythonWriter writer) {
writer.addDependency(AwsPythonDependency.SMITHY_AWS_CORE.withOptionalDependencies("json"));
writer.addImport("smithy_aws_core.aio.protocols", "AwsJson11ClientProtocol");
var serviceSymbol = context.symbolProvider().toSymbol(context.settings().service(context.model()));
var serviceSchema = serviceSymbol.expectProperty(SymbolProperties.SCHEMA);
writer.write("AwsJson11ClientProtocol($T)", serviceSchema);
}

@Override
public void generateProtocolTests(GenerationContext context) {
context.writerDelegator()
.useFileWriter("./tests/test_awsjson11_protocol.py", "tests.test_awsjson11_protocol", writer -> {
new HttpProtocolTestGenerator(
context,
getProtocol(),
writer,
(shape, testCase) -> TESTS_TO_SKIP.contains(testCase.getId())).run();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
public class AwsProtocolsIntegration implements PythonIntegration {
@Override
public List<ProtocolGenerator> getProtocolGenerators() {
return List.of();
return List.of(new AwsJson10ProtocolGenerator(), new AwsJson11ProtocolGenerator());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import software.amazon.smithy.aws.traits.auth.SigV4Trait;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.model.Model;
Expand Down Expand Up @@ -188,12 +189,14 @@ private void generateRequestTest(OperationShape operation, HttpRequestTestCase t
endpoint_uri="https://$L/$L",
transport = $T(),
retry_strategy=SimpleRetryStrategy(max_attempts=1),
${C|}
)
""",
CodegenUtils.getConfigSymbol(context.settings()),
host,
path,
REQUEST_TEST_ASYNC_HTTP_CLIENT_SYMBOL);
REQUEST_TEST_ASYNC_HTTP_CLIENT_SYMBOL,
(Runnable) this::writeSigV4TestConfig);
}));

// Generate the input using the expected shape and params
Expand Down Expand Up @@ -437,13 +440,15 @@ private void generateResponseTest(OperationShape operation, HttpResponseTestCase
headers=$J,
body=b$S,
),
${C|}
)
""",
CodegenUtils.getConfigSymbol(context.settings()),
RESPONSE_TEST_ASYNC_HTTP_CLIENT_SYMBOL,
testCase.getCode(),
CodegenUtils.toTuples(testCase.getHeaders()),
testCase.getBody().filter(body -> !body.isEmpty()).orElse(""));
testCase.getBody().filter(body -> !body.isEmpty()).orElse(""),
(Runnable) this::writeSigV4TestConfig);
}));
// Create an empty input object to pass
var inputShape = model.expectShape(operation.getInputShape(), StructureShape.class);
Expand Down Expand Up @@ -490,13 +495,15 @@ private void generateErrorResponseTest(
headers=$J,
body=b$S,
),
${C|}
)
""",
CodegenUtils.getConfigSymbol(context.settings()),
RESPONSE_TEST_ASYNC_HTTP_CLIENT_SYMBOL,
testCase.getCode(),
CodegenUtils.toTuples(testCase.getHeaders()),
testCase.getBody().orElse(""));
testCase.getBody().orElse(""),
(Runnable) this::writeSigV4TestConfig);
}));
// Create an empty input object to pass
var inputShape = model.expectShape(operation.getInputShape(), StructureShape.class);
Expand Down Expand Up @@ -607,6 +614,19 @@ private void writeClientBlock(
});
}

private void writeSigV4TestConfig() {
if (!service.hasTrait(SigV4Trait.class)) {
return;
}
writer.addImport("smithy_aws_core.identity", "StaticCredentialsResolver");
writer.write("""
region="us-east-1",
aws_access_key_id="test-access-key-id",
aws_secret_access_key="test-secret-access-key",
aws_credentials_identity_resolver=StaticCredentialsResolver(),
""");
}

private void writeUtilStubs(Symbol serviceSymbol) {
LOGGER.fine(String.format("Writing utility stubs for %s : %s", serviceSymbol.getName(), protocol.getName()));
writer.addDependency(SmithyPythonDependency.SMITHY_CORE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ public void initializeProtocol(GenerationContext context, PythonWriter writer) {
// it will need to generate some protocol-specific comparators.
@Override
public void generateProtocolTests(GenerationContext context) {
context.writerDelegator().useFileWriter("./tests/test_protocol.py", "tests.test_protocol", writer -> {
new HttpProtocolTestGenerator(
context,
getProtocol(),
writer,
(shape, testCase) -> filterTests(testCase)).run();
});
context.writerDelegator()
.useFileWriter("./tests/test_restjson_protocol.py", "tests.test_restjson_protocol", writer -> {
new HttpProtocolTestGenerator(
context,
getProtocol(),
writer,
(shape, testCase) -> filterTests(testCase)).run();
});
}

private boolean filterTests(HttpMessageTestCase testCase) {
Expand Down
1 change: 1 addition & 0 deletions codegen/protocol-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ repositories {

dependencies {
implementation(project(":core"))
implementation(project(":aws:core"))
implementation(libs.smithy.aws.protocol.tests)
}
44 changes: 44 additions & 0 deletions codegen/protocol-test/smithy-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,50 @@
"moduleVersion": "0.0.1"
}
}
},
"aws-json-1-0": {
"transforms": [
{
"name": "includeServices",
"args": {
"services": [
"aws.protocoltests.json10#JsonRpc10"
]
}
},
{
"name": "removeUnusedShapes"
}
],
"plugins": {
"python-client-codegen": {
"service": "aws.protocoltests.json10#JsonRpc10",
"module": "awsjson10",
"moduleVersion": "0.0.1"
}
}
},
"aws-json-1-1": {
"transforms": [
{
"name": "includeServices",
"args": {
"services": [
"aws.protocoltests.json#JsonProtocol"
]
}
},
{
"name": "removeUnusedShapes"
}
],
"plugins": {
"python-client-codegen": {
"service": "aws.protocoltests.json#JsonProtocol",
"module": "awsjson11",
"moduleVersion": "0.0.1"
}
}
}
}
}
Loading
Loading