Skip to content

Conversation

@jbelkins
Copy link
Contributor

@jbelkins jbelkins commented Nov 5, 2025

Description of changes

  • Adds the Swift Schema type, and related types for representing the Smithy model such as ShapeID and Node.
  • Adds schemas for all Smithy primitive / "prelude" types.
  • Adds code generation of schemas for all modeled shapes that would require one.

To illustrate, here's the generated schema for com.amazonaws.arcregionswitch#Approval, a two-case Smithy enum:

var schema__com_amazonaws_arcregionswitch__Approval: Smithy.Schema {
    .init(
        id: .init("com.amazonaws.arcregionswitch", "Approval"),
        type: .enum,
        members: [
            .init(
                id: .init("com.amazonaws.arcregionswitch", "Approval", "APPROVE"),
                type: .member,
                traits: [
                    .init("smithy.api", "enumValue"): "approve",
                ],
                target: Smithy.Prelude.unitSchema,
                index: 0
            ),
            .init(
                id: .init("com.amazonaws.arcregionswitch", "Approval", "DECLINE"),
                type: .member,
                traits: [
                    .init("smithy.api", "enumValue"): "decline",
                ],
                target: Smithy.Prelude.unitSchema,
                index: 1
            ),
        ]
    )
}

Scope

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

import software.amazon.smithy.swift.codegen.SwiftWriter
import software.amazon.smithy.swift.codegen.swiftmodules.SmithyTypes
import kotlin.jvm.optionals.getOrNull

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Shape.schemaVar() method generates the Swift variable name for referencing a schema.

Depending on the namespace of the shape being referenced, it may either resolve to a code-generated schema or to a schema contained in the prelude.

The prelude schemas are in the Smithy runtime module and are defined below in Prelude.swift.

}

private fun ShapeId.schemaVarName(): String {
assert(this.member.getOrNull() == null)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Members are never generated into their own variable; they are only generated inline into another schema's members array, hence this assertion.

@@ -0,0 +1,12 @@
package software.amazon.smithy.swift.codegen.integration.serde.schema

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Below is the list of traits that are included in the schema. There is a list in the spec of all the ones that should eventually be included, but for now I'm including only the most basic ones.


LOGGER.info("Generating Swift client for service ${directive.settings().service}")

var shouldGenerateTestTarget = false
Copy link
Contributor Author

Choose a reason for hiding this comment

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

unused property

//
// SPDX-License-Identifier: Apache-2.0
//

Copy link
Contributor Author

@jbelkins jbelkins Nov 5, 2025

Choose a reason for hiding this comment

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

This type holds the actual content of a trait.

.testTarget(
name: "SmithyTests",
dependencies: ["Smithy"]
),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a test target to the Smithy target since none previously existed.


private fun resolveInputShapes(ctx: ProtocolGenerator.GenerationContext): Map<Shape, Map<ShapeMetadata, Any>> {
var shapesInfo: MutableMap<Shape, Map<ShapeMetadata, Any>> = mutableMapOf()
val shapesInfo: MutableMap<Shape, Map<ShapeMetadata, Any>> = mutableMapOf()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixes a warning about an unmodified var.

return resolved
}

private fun resolveShapesNeedingSchema(ctx: ProtocolGenerator.GenerationContext): Set<Shape> {
Copy link
Contributor Author

@jbelkins jbelkins Nov 5, 2025

Choose a reason for hiding this comment

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

This function (and the private function below it) gets all the shapes needing schemas: inputs, outputs, errors, and their nested descendants, excluding schemas that are already provided in the Smithy prelude.

package software.amazon.smithy.swift.codegen.utils

import software.amazon.smithy.swift.codegen.SwiftSettings

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like Models, schemas may either be rendered into individual files or one big Schemas.swift file.

//

import XCTest
import Smithy
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simple test on ShapeID to ensure it renders to String properly.

import software.amazon.smithy.swift.codegen.swiftmodules.SmithyTypes
import kotlin.jvm.optionals.getOrNull

class SchemaGenerator(
Copy link
Contributor Author

@jbelkins jbelkins Nov 5, 2025

Choose a reason for hiding this comment

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

This type code-generates a schema for a Smithy model shape. A Schema is just a Swift-native type that holds info about a Smithy shape that is needed for serialization.

A couple notes:

  • Schemas are generated as computed, global variables, with a naming scheme that ensures each schema has a unique name. Computed properties and reference types are used to avoid circular reference compile errors on self-referencing Smithy shapes.
  • Schema and related types are being marked Sendable so that they can be assigned to global vars, but I don't expect them to have any sort of async access or functions.

@jbelkins jbelkins marked this pull request as ready for review November 7, 2025 20:26
@jbelkins jbelkins requested review from dayaffe and sichanyoo November 7, 2025 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants