From 32b7a53c91021f5150e698054746628e631b181a Mon Sep 17 00:00:00 2001 From: Hayden Baker Date: Fri, 7 Mar 2025 16:37:53 -0800 Subject: [PATCH 1/3] Update weather example to use correct dependency --- examples/weather/smithy-build.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/weather/smithy-build.json b/examples/weather/smithy-build.json index becbde21a..8a291f616 100644 --- a/examples/weather/smithy-build.json +++ b/examples/weather/smithy-build.json @@ -5,7 +5,7 @@ "dependencies": [ "software.amazon.smithy:smithy-model:[1.41.0,2.0)", "software.amazon.smithy:smithy-aws-traits:[1.41.0,2.0)", - "software.amazon.smithy.python:smithy-python-codegen:0.1.0" + "software.amazon.smithy.python.codegen:core:0.0.1" ] }, "projections": { From ce03fcba1f8e3c96581c971b63a03f4d3334fb78 Mon Sep 17 00:00:00 2001 From: Hayden Baker Date: Fri, 7 Mar 2025 16:39:21 -0800 Subject: [PATCH 2/3] Only import `Field` when single-value `httpHeader` is actually in the model squish --- .../integrations/HttpBindingProtocolGenerator.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/integrations/HttpBindingProtocolGenerator.java b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/integrations/HttpBindingProtocolGenerator.java index 8042663b9..367712c58 100644 --- a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/integrations/HttpBindingProtocolGenerator.java +++ b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/integrations/HttpBindingProtocolGenerator.java @@ -199,7 +199,7 @@ private void serializeHeaders( ) { writer.pushState(new SerializeFieldsSection(operation)); writer.addDependency(SmithyPythonDependency.SMITHY_HTTP); - writer.addImports("smithy_http", Set.of("Field", "Fields")); + writer.addImport("smithy_http", "Fields"); writer.write(""" headers = Fields( [ @@ -234,9 +234,12 @@ private void writeContentType(GenerationContext context, PythonWriter writer, Op if (optionalContentType.isEmpty() && shouldWriteDefaultBody(context, operation)) { optionalContentType = Optional.of(getDocumentContentType()); } - optionalContentType.ifPresent(contentType -> writer.write( - "Field(name=\"Content-Type\", values=[$S]),", - contentType)); + if (optionalContentType.isPresent()) { + writer.addImport("smithy_http", "Field"); + writer.write( + "Field(name=\"Content-Type\", values=[$S]),", + optionalContentType.get()); + } } private void writeContentLength(GenerationContext context, PythonWriter writer, OperationShape operation) { @@ -249,6 +252,7 @@ private void writeContentLength(GenerationContext context, PythonWriter writer, // The requiresLength trait, however, can force a length calculation. // see: https://smithy.io/2.0/spec/streaming.html#requireslength-trait if (requiresLength(context, operation)) { + writer.addImport("smithy_http", "Field"); writer.write("Field(name=\"Content-Length\", values=[str(content_length)]),"); } return; @@ -262,6 +266,7 @@ private void writeContentLength(GenerationContext context, PythonWriter writer, .anyMatch(binding -> binding.getLocation() == PAYLOAD || binding.getLocation() == DOCUMENT); if (hasBodyBindings) { + writer.addImport("smithy_http", "Field"); writer.write("Field(name=\"Content-Length\", values=[str(content_length)]),"); } } @@ -331,6 +336,7 @@ private void serializeIndividualHeaders(GenerationContext context, PythonWriter headers.extend(tuples_to_fields(($S, $L) for e in input.$L$L)) """, binding.getLocationName(), inputValue, pythonName, trailer); } else { + writer.addImport("smithy_http", "Field"); var dataSource = "input." + pythonName; var inputValue = target.accept(new HttpMemberSerVisitor( context, From 9e1bc1881e8acbd0f25b9638beba0f62e7880cda Mon Sep 17 00:00:00 2001 From: Hayden Baker Date: Mon, 10 Mar 2025 08:22:40 -0700 Subject: [PATCH 3/3] Add support for generating schemas for operations and service shapes --- .../python/codegen/generators/SchemaGenerator.java | 3 +-- .../codegen/types/DirectedPythonTypeCodegen.java | 3 +++ packages/smithy-core/src/smithy_core/documents.py | 10 +++++++++- packages/smithy-core/src/smithy_core/shapes.py | 4 ++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/SchemaGenerator.java b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/SchemaGenerator.java index 8e863fc31..7711d97ba 100644 --- a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/SchemaGenerator.java +++ b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/SchemaGenerator.java @@ -83,8 +83,7 @@ public static void generateAll( var index = TopologicalIndex.of(context.model()); Stream.concat(index.getOrderedShapes().stream(), index.getRecursiveShapes().stream()) .filter(shapes::contains) - .filter(shape -> !shape.isOperationShape() && !shape.isResourceShape() - && !shape.isServiceShape() + .filter(shape -> !shape.isResourceShape() && !shape.isMemberShape() && !Prelude.isPreludeShape(shape)) .filter(filter::apply) diff --git a/codegen/plugins/types/src/main/java/software/amazon/smithy/python/codegen/types/DirectedPythonTypeCodegen.java b/codegen/plugins/types/src/main/java/software/amazon/smithy/python/codegen/types/DirectedPythonTypeCodegen.java index 852a7e432..41fde17dc 100644 --- a/codegen/plugins/types/src/main/java/software/amazon/smithy/python/codegen/types/DirectedPythonTypeCodegen.java +++ b/codegen/plugins/types/src/main/java/software/amazon/smithy/python/codegen/types/DirectedPythonTypeCodegen.java @@ -64,6 +64,9 @@ public GenerationContext createContext(CreateContextDirective directive) { new ServiceErrorGenerator(directive.settings(), directive.context().writerDelegator()).run(); SchemaGenerator.generateAll(directive.context(), directive.connectedShapes().values(), shape -> { + if (shape.isOperationShape() || shape.isServiceShape()) { + return false; + } if (shape.isStructureShape()) { return shouldGenerateStructure(directive.settings(), shape); } diff --git a/packages/smithy-core/src/smithy_core/documents.py b/packages/smithy-core/src/smithy_core/documents.py index 4f01d6916..b9af55cf5 100644 --- a/packages/smithy-core/src/smithy_core/documents.py +++ b/packages/smithy-core/src/smithy_core/documents.py @@ -94,7 +94,11 @@ def __init__( else: self._value = value - if self._schema.shape_type is not ShapeType.DOCUMENT: + if self._schema.shape_type not in ( + ShapeType.DOCUMENT, + ShapeType.OPERATION, + ShapeType.SERVICE, + ): self._type = self._schema.shape_type else: # TODO: set an appropriate schema if one was not provided @@ -311,6 +315,10 @@ def serialize_contents(self, serializer: ShapeSerializer) -> None: raise SmithyException( f"Unexpexcted DOCUMENT shape type for document value: {self.as_value()}" ) + case _: + raise SmithyException( + f"Unexpected {self._type} shape type for document value: {self.as_value()}" + ) def serialize_members(self, serializer: ShapeSerializer) -> None: for value in self.as_map().values(): diff --git a/packages/smithy-core/src/smithy_core/shapes.py b/packages/smithy-core/src/smithy_core/shapes.py index 1eebfac15..3f9505165 100644 --- a/packages/smithy-core/src/smithy_core/shapes.py +++ b/packages/smithy-core/src/smithy_core/shapes.py @@ -122,6 +122,6 @@ class ShapeType(Enum): # We won't acutally be using these, probably # MEMBER = 20 - # SERVICE = 21 + SERVICE = 21 # RESOURCE = 22 - # OPERATION = 23 + OPERATION = 23