Skip to content

Commit 7f53a5e

Browse files
authored
Add support for operation and service schemas (#413)
* Update weather example to use correct dependency * Only import `Field` when single-value `httpHeader` is actually in the model squish * Add support for generating schemas for operations and service shapes
1 parent 7673674 commit 7f53a5e

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ public static void generateAll(
8383
var index = TopologicalIndex.of(context.model());
8484
Stream.concat(index.getOrderedShapes().stream(), index.getRecursiveShapes().stream())
8585
.filter(shapes::contains)
86-
.filter(shape -> !shape.isOperationShape() && !shape.isResourceShape()
87-
&& !shape.isServiceShape()
86+
.filter(shape -> !shape.isResourceShape()
8887
&& !shape.isMemberShape()
8988
&& !Prelude.isPreludeShape(shape))
9089
.filter(filter::apply)

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private void serializeHeaders(
199199
) {
200200
writer.pushState(new SerializeFieldsSection(operation));
201201
writer.addDependency(SmithyPythonDependency.SMITHY_HTTP);
202-
writer.addImports("smithy_http", Set.of("Field", "Fields"));
202+
writer.addImport("smithy_http", "Fields");
203203
writer.write("""
204204
headers = Fields(
205205
[
@@ -234,9 +234,12 @@ private void writeContentType(GenerationContext context, PythonWriter writer, Op
234234
if (optionalContentType.isEmpty() && shouldWriteDefaultBody(context, operation)) {
235235
optionalContentType = Optional.of(getDocumentContentType());
236236
}
237-
optionalContentType.ifPresent(contentType -> writer.write(
238-
"Field(name=\"Content-Type\", values=[$S]),",
239-
contentType));
237+
if (optionalContentType.isPresent()) {
238+
writer.addImport("smithy_http", "Field");
239+
writer.write(
240+
"Field(name=\"Content-Type\", values=[$S]),",
241+
optionalContentType.get());
242+
}
240243
}
241244

242245
private void writeContentLength(GenerationContext context, PythonWriter writer, OperationShape operation) {
@@ -249,6 +252,7 @@ private void writeContentLength(GenerationContext context, PythonWriter writer,
249252
// The requiresLength trait, however, can force a length calculation.
250253
// see: https://smithy.io/2.0/spec/streaming.html#requireslength-trait
251254
if (requiresLength(context, operation)) {
255+
writer.addImport("smithy_http", "Field");
252256
writer.write("Field(name=\"Content-Length\", values=[str(content_length)]),");
253257
}
254258
return;
@@ -262,6 +266,7 @@ private void writeContentLength(GenerationContext context, PythonWriter writer,
262266
.anyMatch(binding -> binding.getLocation() == PAYLOAD || binding.getLocation() == DOCUMENT);
263267

264268
if (hasBodyBindings) {
269+
writer.addImport("smithy_http", "Field");
265270
writer.write("Field(name=\"Content-Length\", values=[str(content_length)]),");
266271
}
267272
}
@@ -331,6 +336,7 @@ private void serializeIndividualHeaders(GenerationContext context, PythonWriter
331336
headers.extend(tuples_to_fields(($S, $L) for e in input.$L$L))
332337
""", binding.getLocationName(), inputValue, pythonName, trailer);
333338
} else {
339+
writer.addImport("smithy_http", "Field");
334340
var dataSource = "input." + pythonName;
335341
var inputValue = target.accept(new HttpMemberSerVisitor(
336342
context,

codegen/plugins/types/src/main/java/software/amazon/smithy/python/codegen/types/DirectedPythonTypeCodegen.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public GenerationContext createContext(CreateContextDirective<PythonSettings, Py
6464
public void customizeBeforeShapeGeneration(CustomizeDirective<GenerationContext, PythonSettings> directive) {
6565
new ServiceErrorGenerator(directive.settings(), directive.context().writerDelegator()).run();
6666
SchemaGenerator.generateAll(directive.context(), directive.connectedShapes().values(), shape -> {
67+
if (shape.isOperationShape() || shape.isServiceShape()) {
68+
return false;
69+
}
6770
if (shape.isStructureShape()) {
6871
return shouldGenerateStructure(directive.settings(), shape);
6972
}

examples/weather/smithy-build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"dependencies": [
66
"software.amazon.smithy:smithy-model:[1.41.0,2.0)",
77
"software.amazon.smithy:smithy-aws-traits:[1.41.0,2.0)",
8-
"software.amazon.smithy.python:smithy-python-codegen:0.1.0"
8+
"software.amazon.smithy.python.codegen:core:0.0.1"
99
]
1010
},
1111
"projections": {

packages/smithy-core/src/smithy_core/documents.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ def __init__(
9494
else:
9595
self._value = value
9696

97-
if self._schema.shape_type is not ShapeType.DOCUMENT:
97+
if self._schema.shape_type not in (
98+
ShapeType.DOCUMENT,
99+
ShapeType.OPERATION,
100+
ShapeType.SERVICE,
101+
):
98102
self._type = self._schema.shape_type
99103
else:
100104
# TODO: set an appropriate schema if one was not provided
@@ -311,6 +315,10 @@ def serialize_contents(self, serializer: ShapeSerializer) -> None:
311315
raise SmithyException(
312316
f"Unexpexcted DOCUMENT shape type for document value: {self.as_value()}"
313317
)
318+
case _:
319+
raise SmithyException(
320+
f"Unexpected {self._type} shape type for document value: {self.as_value()}"
321+
)
314322

315323
def serialize_members(self, serializer: ShapeSerializer) -> None:
316324
for value in self.as_map().values():

packages/smithy-core/src/smithy_core/shapes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ class ShapeType(Enum):
122122

123123
# We won't acutally be using these, probably
124124
# MEMBER = 20
125-
# SERVICE = 21
125+
SERVICE = 21
126126
# RESOURCE = 22
127-
# OPERATION = 23
127+
OPERATION = 23

0 commit comments

Comments
 (0)