Skip to content

Commit 6df2d87

Browse files
Set default namespace for json codec
1 parent a07082b commit 6df2d87

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/ConfigGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ private static List<ConfigProperty> getProtocolProperties(GenerationContext cont
130130
.build())
131131
.documentation("The protocol to serialize and deserialize requests with.")
132132
.initialize(w -> {
133-
w.write("self.protocol = protocol or $T()",
134-
context.protocolGenerator().getProtocolSymbol(context));
133+
w.write("self.protocol = protocol or ${C|}",
134+
w.consumer(writer -> context.protocolGenerator().initializeProtocol(context, writer)));
135135
});
136136

137137
var transportBuilder = ConfigProperty.builder()

codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/ProtocolGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
*/
55
package software.amazon.smithy.python.codegen.generators;
66

7-
import software.amazon.smithy.codegen.core.Symbol;
87
import software.amazon.smithy.model.shapes.ShapeId;
98
import software.amazon.smithy.python.codegen.ApplicationProtocol;
109
import software.amazon.smithy.python.codegen.GenerationContext;
10+
import software.amazon.smithy.python.codegen.writer.PythonWriter;
1111
import software.amazon.smithy.utils.SmithyUnstableApi;
1212

1313
/**
@@ -41,7 +41,7 @@ default String getName() {
4141
*/
4242
ApplicationProtocol getApplicationProtocol(GenerationContext context);
4343

44-
Symbol getProtocolSymbol(GenerationContext context);
44+
void initializeProtocol(GenerationContext context, PythonWriter writer);
4545

4646
/**
4747
* Generates the code for validating the generated protocol's serializers and deserializers.

codegen/core/src/main/java/software/amazon/smithy/python/codegen/integrations/RestJsonProtocolGenerator.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import java.util.Set;
88
import software.amazon.smithy.aws.traits.protocols.RestJson1Trait;
9-
import software.amazon.smithy.codegen.core.Symbol;
109
import software.amazon.smithy.model.node.ArrayNode;
1110
import software.amazon.smithy.model.node.ObjectNode;
1211
import software.amazon.smithy.model.shapes.ShapeId;
@@ -15,7 +14,9 @@
1514
import software.amazon.smithy.python.codegen.GenerationContext;
1615
import software.amazon.smithy.python.codegen.HttpProtocolTestGenerator;
1716
import software.amazon.smithy.python.codegen.SmithyPythonDependency;
17+
import software.amazon.smithy.python.codegen.SymbolProperties;
1818
import software.amazon.smithy.python.codegen.generators.ProtocolGenerator;
19+
import software.amazon.smithy.python.codegen.writer.PythonWriter;
1920
import software.amazon.smithy.utils.SmithyUnstableApi;
2021

2122
/**
@@ -84,12 +85,12 @@ public ApplicationProtocol getApplicationProtocol(GenerationContext context) {
8485
}
8586

8687
@Override
87-
public Symbol getProtocolSymbol(GenerationContext context) {
88-
return Symbol.builder()
89-
.name("RestJsonClientProtocol")
90-
.namespace("smithy_aws_core.aio.protocols", ".")
91-
.dependencies(SmithyPythonDependency.SMITHY_AWS_CORE)
92-
.build();
88+
public void initializeProtocol(GenerationContext context, PythonWriter writer) {
89+
writer.addDependency(SmithyPythonDependency.SMITHY_AWS_CORE);
90+
writer.addImport("smithy_aws_core.aio.protocols", "RestJsonClientProtocol");
91+
var serviceSymbol = context.symbolProvider().toSymbol(context.settings().service(context.model()));
92+
var serviceSchema = serviceSymbol.expectProperty(SymbolProperties.SCHEMA);
93+
writer.write("RestJsonClientProtocol($T)", serviceSchema);
9394
}
9495

9596
// This is here rather than in HttpBindingProtocolGenerator because eventually

packages/smithy-aws-core/src/smithy_aws_core/aio/protocols.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from smithy_core.codecs import Codec
66
from smithy_core.exceptions import DiscriminatorError
7-
from smithy_core.schemas import APIOperation
7+
from smithy_core.schemas import APIOperation, Schema
88
from smithy_core.shapes import ShapeID, ShapeType
9+
from smithy_core.types import TimestampFormat
910
from smithy_http.aio.interfaces import HTTPErrorIdentifier, HTTPResponse
1011
from smithy_http.aio.protocols import HttpBindingClientProtocol
1112
from smithy_json import JSONCodec, JSONDocument
@@ -50,10 +51,20 @@ class RestJsonClientProtocol(HttpBindingClientProtocol):
5051
"""An implementation of the aws.protocols#restJson1 protocol."""
5152

5253
_id: Final = RestJson1Trait.id
53-
_codec: Final = JSONCodec(document_class=AWSJSONDocument)
5454
_contentType: Final = "application/json"
5555
_error_identifier: Final = AWSErrorIdentifier()
5656

57+
def __init__(self, service_schema: Schema) -> None:
58+
"""Initialize a RestJsonClientProtocol.
59+
60+
:param service: The schema for the service to interact with.
61+
"""
62+
self._codec: Final = JSONCodec(
63+
document_class=AWSJSONDocument,
64+
default_namespace=service_schema.id.namespace,
65+
default_timestamp_format=TimestampFormat.EPOCH_SECONDS,
66+
)
67+
5768
@property
5869
def id(self) -> ShapeID:
5970
return self._id

0 commit comments

Comments
 (0)