1. The Smithy model#
The Smithy model describes the Smithy semantic model and the files used to create it. Smithy models are used to describe services and data structures.
1.1. Smithy overview#
Smithy is a framework that consists of a semantic model, file formats used to define a model, and a build process used to validate models and facilitate model transformations.
- Semantic model
- The in-memory model used by tools. The semantic model may be serialized into one or more model file representations.
- Model File
A file on the file system, in a particular representation. The model files that make up a semantic model MAY be split across multiple files to improve readability or modularity, and those files are not required to use the same representation. Model files do not explicitly include other model files; this responsibility is left to tooling to ensure that all necessary model files are merged together to form a valid semantic model.
One or more model files can be assembled (or merged) together to form a semantic model.
- Representation
A particular model file format such as the Smithy IDL or JSON AST. Representations are loaded into the semantic model by mapping the representation to concepts in the semantic model.
- Smithy IDL: a human-readable format that aims to streamline authoring and reading models.
- JSON AST: a machine-readable JSON-based format.
1.2. The semantic model#
Smithy's semantic model is an in-memory model used by tools. It is independent of any particular serialized representation. The semantic model contains metadata and a graph of shapes connected by shape IDs.
- Shape
- Shapes are named data definitions that describe the
structure of an API. Shapes are referenced and connected by shape IDs.
Relationships between shapes are formed by members that
target other shapes, properties of shapes like the
input
andoutput
properties of an operation, and applied traits that attach a trait to a shape. - Shape ID
- A shape ID is used to identify shapes defined in a
model. For example,
smithy.example#MyShape
,smithy.example#Foo$bar
, andBaz
are all different kinds of shape IDs. - Trait
- Traits are specialized shapes that form the basis of Smithy's meta-model. Traits are applied to shapes to associate metadata to a shape. They are typically used by tools to influence validation, serialization, and code generation.
- Applied trait
- An applied trait is an instance of a trait applied to a shape, configured using a node value.
- Model metadata
- Metadata is a schema-less extensibility mechanism used to associate metadata to an entire model.
- Prelude
- The prelude defines various simple shapes and every trait defined in the core specification. All Smithy models automatically include the prelude.
1.3. Model metadata#
Metadata is a schema-less extensibility mechanism used to associate metadata to an entire model. For example, metadata is used to define validators and model-wide suppressions. Metadata is defined using a node value. The following example configures a model validator:
$version: "2"
metadata validators = [
{
name: "EmitEachSelector"
id: "OperationInputName"
message: "This shape is referenced as input but the name does not end with 'Input'"
configuration: {
selector: "operation -[input]-> :not([id|name$=Input i])"
}
}
]
Metadata conflicts
When a conflict occurs between top-level metadata key-value pairs, the following conflict resolution logic is used:
- If both values are arrays, the values of both arrays are concatenated into a single array.
- Otherwise, if both values are exactly equal, the conflict is ignored.
- Otherwise, the conflict is invalid.
Given the following two Smithy models:
$version: "2"
metadata "foo" = ["baz", "bar"]
metadata "qux" = "test"
metadata "validConflict" = "hi!"
$version: "2"
metadata "foo" = ["lorem", "ipsum"]
metadata "lorem" = "ipsum"
metadata "validConflict" = "hi!"
Merging model-a.smithy
and model-b.smithy
produces the following
model:
$version: "2"
metadata "foo" = ["baz", "bar", "lorem", "ipsum"]
metadata "qux" = "test"
metadata "lorem" = "ipsum"
metadata "validConflict" = "hi!"
1.4. Node values#
Node values are JSON-like values used to define metadata and the value of an applied trait.
The following example defines metadata using a node value:
metadata foo = "hello"
The following example defines a trait using a node value:
$version: "2"
namespace smithy.example
@length(min: 1, max: 10)
string MyString
Node value types
Node values have the same data model as JSON; they consist of the following kinds of values:
Type | Description |
---|---|
null | The lack of a value |
string | A UTF-8 string |
number | A double precision floating point number |
boolean | A Boolean, true or false value |
array | A list of heterogeneous node values |
object | A map of string keys to heterogeneous node values |
1.5. Merging model files#
Multiple model files can be used to create a semantic model. Implementations MUST take the following steps when merging two or more model files:
- Merge the metadata objects of all model files. If top-level metadata key-value pairs conflict, merge the metadata if possible or fail.
- Shapes defined in a single model file are added to the semantic model as-is.
- Shapes with the same shape ID defined in multiple model files are
reconciled using the following rules:
- All conflicting shapes MUST have the same shape type.
- Conflicting aggregate shape types MUST contain the same members that target the same shapes.
- Conflicting service shape types MUST contain the same properties and target the same shapes.
- Conflicting traits defined in shape definitions or through apply statements are reconciled using trait conflict resolution.
1.6. Shapes#
Smithy models are made up of shapes. Shapes are named definitions of types.
Shapes are visualized using the following diagram:
1.6.1. Shape types#
Shape types are grouped into three categories:
- Simple types
Simple types are types that do not contain nested types or shape references.
Type Description blob Uninterpreted binary data boolean Boolean value type string UTF-8 encoded string enum A string with a fixed set of values. byte 8-bit signed integer ranging from -128 to 127 (inclusive) short 16-bit signed integer ranging from -32,768 to 32,767 (inclusive) integer 32-bit signed integer ranging from -2^31 to (2^31)-1 (inclusive) intEnum An integer with a fixed set of values. long 64-bit signed integer ranging from -2^63 to (2^63)-1 (inclusive) float Single precision IEEE-754 floating point number double Double precision IEEE-754 floating point number bigInteger Arbitrarily large signed integer bigDecimal Arbitrary precision signed decimal number timestamp An instant in time with no UTC offset or timezone. document Open content that functions as a kind of "any" type. - Aggregate types
Aggregate types contain configurable member references to others shapes.
- Service types
Types that define the organization and operations of a service.
1.6.2. Member shapes#
Members are defined in shapes to reference other shapes using
a shape ID. Members are found in enum,
intEnum, list, map,
structure, and union shapes. The shape
referenced by a member is called its "target". A member MUST NOT target a
trait, operation,
resource, service, or member
.
The following example defines a list that contains a member shape. Further examples can be found in the documentation for shape types.
$version: "2"
namespace smithy.example
list UserNameList {
member: UserName
}
1.6.3. Shape ID#
A shape ID is used to refer to shapes in the model. All shapes have an assigned shape ID.
The following example defines a shape in the smithy.example
namespace named
MyString
, giving the shape a shape ID of smithy.example#MyString
:
$version: "2"
namespace smithy.example
string MyString
Shape IDs have the following syntax:
- Absolute shape ID
- An absolute shape ID starts with a
namespace
, followed by "#
", followed by a relative shape ID. For example,smithy.example#Foo
andsmithy.example#Foo$bar
are absolute shape IDs. - Relative shape ID
- A relative shape ID contains a
shape name
and an optionalmember name
. The shape name and member name are separated by the "$
" symbol if a member name is present. For example,Foo
andFoo$bar
are relative shape IDs. - Namespace
A namespace is a mechanism for logically grouping shapes in a way that makes them reusable alongside other models without naming conflicts. A semantic model MAY contain shapes defined across multiple namespaces. The IDL representation supports zero or one namespace per model file, while the JSON AST representation supports zero or more namespaces per model file.
Models SHOULD use a single namespace to model a single logical domain. Limiting the number of namespaces used to define a logical grouping of shapes limits the potential for ambiguity if the shapes are used by the same service or need to be referenced within the same model.
- Shape name
The name of the shape within a namespace.
Consumers of a Smithy model MAY choose to inflect shape names, structure member names, and other facets of a Smithy model in order to expose a more idiomatic experience to particular programming languages. In order to make this easier for consumers of a model, model authors SHOULD utilize a strict form of PascalCase in which only the first letter of acronyms, abbreviations, and initialisms are capitalized. For example, prefer
UserId
overUserID
, andArn
overARN
.- Root shape ID
- A root shape ID is a shape ID that does not contain a member.
For example,
smithy.example#Foo
andFoo
are root shape IDs.
1.6.3.1. Shape ID ABNF#
Shape IDs are formally defined by the following ABNF:
ShapeId =RootShapeId
[ShapeIdMember
] RootShapeId =AbsoluteRootShapeId
/Identifier
AbsoluteRootShapeId =Namespace
"#"Identifier
Namespace =Identifier
*("."Identifier
) Identifier =IdentifierStart
*IdentifierChars
IdentifierStart = (1*"_" (ALPHA / DIGIT)) / ALPHA IdentifierChars = ALPHA / DIGIT / "_" ShapeIdMember = "$"Identifier
1.6.3.2. Shape ID conflicts#
While shape ID references within the semantic model are case-sensitive, no
two shapes in the semantic model can have the same case-insensitive shape ID.
This restriction makes it easier to use Smithy models for code generation in
programming languages that do not support case-sensitive identifiers or that
perform some kind of normalization on generated identifiers (for example,
a Python code generator might convert all member names to lower snake case).
To illustrate, com.Foo#baz
and com.foo#BAZ
are not allowed in the
same semantic model. This restriction also extends to member names:
com.foo#Baz$bar
and com.foo#Baz$BAR
are in conflict.
See also
Merging model files for information on how conflicting shape definitions for the same shape ID are handled when assembling the semantic model from multiple model files.
1.7. Traits#
Traits are model components that can be attached to shapes to describe additional information about the shape; shapes provide the structure and layout of an API, while traits provide refinement and style.
1.7.1. Applying traits#
An instance of a trait applied to a shape is called an applied trait. Only a single instance of a trait can be applied to a shape. The way in which a trait is applied to a shape depends on the model file representation.
Traits are applied to shapes in the IDL using smithy:TraitStatements
that
immediately precede a shape. The following example applies the
length trait and documentation trait to MyString
:
$version: "2"
namespace smithy.example
@length(min: 1, max: 100)
@documentation("Contains a string")
string MyString
- Refer to the IDL specification for a description of how traits are applied in the IDL.
- Refer to the JSON AST specification for a description of how traits are applied in the JSON AST.
Scope of member traits
Traits that target members apply only in the context of the member shape and do not affect the shape targeted by the member. Traits applied to a member supersede traits applied to the shape targeted by the member and do not inherently conflict.
In the following example, the range trait applied to numberOfItems
takes precedence over the trait applied to PositiveInteger
.
structure ShoppingCart {
// This trait supersedes the PositiveInteger trait.
@range(min: 7, max:12)
numberOfItems: PositiveInteger
}
@range(min: 1)
integer PositiveInteger
1.7.1.1. Applying traits externally#
Both the IDL and JSON AST model representations allow traits to be applied
to shapes outside of a shape's definition. This is done using an
apply
statement in the IDL, or the
apply type in the JSON AST. For example, this can be
useful to allow different teams within the same organization to independently
own different facets of a model; a service team could own the model that
defines the shapes and traits of the API, and a documentation team could
own a model that applies documentation traits to the shapes.
The following example applies the documentation trait and
length trait to the smithy.example#MyString
shape:
$version: "2"
namespace smithy.example
apply MyString @documentation("This is my string!")
apply MyString @length(min: 1, max: 10)
Note
In the semantic model, applying traits outside of a shape definition is treated exactly the same as applying the trait inside of a shape definition.
1.7.1.2. Trait conflict resolution#
Trait conflict resolution is used when the same trait is applied multiple times to a shape. Duplicate traits applied to shapes are allowed in the following cases:
- If the trait is a
list
orset
shape, then the conflicting trait values are concatenated into a single trait value. - If both values are exactly equal, then the conflict is ignored.
All other instances of trait collisions are prohibited.
The following model definition is valid because the length
trait is
duplicated on the MyList
shape with the same values:
$version: "2"
namespace smithy.example
@length(min: 0, max: 10)
list MyList {
member: String
}
apply MyList @length(min: 0, max: 10)
The following model definition is valid because the tags
trait
is a list. The resulting value assigned to the tags
trait on the
Hello
shape is a list that contains "a", "b", and "c".
$version: "2"
namespace smithy.example
@tags(["a", "b"])
string Hello
apply Hello @tags(["c"])
The following model definition is invalid because the length
trait is
duplicated on the MyList
shape with different values:
$version: "2"
namespace smithy.example
@length(min: 0, max: 10)
list MyList {
member: String
}
apply MyList @length(min: 10, max: 20)
1.7.1.3. Trait node values#
The value provided for a trait MUST be compatible with the shape
of the
trait. The following table defines each shape type that is available to
target from traits and how their values are defined in
node
values.
Smithy type | Node type | Description |
---|---|---|
blob | string | A string value that is base64 encoded. |
boolean | boolean | Can be set to true or false . |
byte | number | The value MUST fall within the range of -128 to 127 |
short | number | The value MUST fall within the range of -32,768 to 32,767 |
integer | number | The value MUST fall within the range of -2^31 to (2^31)-1. |
long | number | The value MUST fall within the range of -2^63 to (2^63)-1. |
float | string | number | The value MUST be either a normal JSON number or one of the following
string values: "NaN" , "Infinity" , "-Infinity" . |
double | string | number | The value MUST be either a normal JSON number or one of the following
string values: "NaN" , "Infinity" , "-Infinity" . |
bigDecimal | string | number | bigDecimal values can be serialized as strings to avoid rounding issues when parsing a Smithy model in various languages. |
bigInteger | string | number | bigInteger values can be serialized as strings to avoid truncation issues when parsing a Smithy model in various languages. |
string | string | The provided value SHOULD be compatible with the mediaType of the
string shape if present; however, this is not validated by Smithy. |
timestamp | number | string | If a number is provided, it represents Unix epoch seconds with optional
millisecond precision. If a string is provided, it MUST be a valid
RFC 3339 string with no UTC offset and optional fractional
precision (for example, 1985-04-12T23:20:50.52Z ). |
list | array | Each value in the array MUST be compatible with the targeted member. |
map | object | Each key MUST be compatible with the key member of the map, and
each value MUST be compatible with the value member of the map. |
structure | object | All members marked as required MUST be provided in a corresponding key-value pair. Each key MUST correspond to a single member name of the structure. Each value MUST be compatible with the member that corresponds to the member name. |
union | object | The object MUST contain a single key-value pair. The key MUST be one of the member names of the union shape, and the value MUST be compatible with the corresponding shape. |
Constraint traits
Trait values MUST be compatible with the required trait and any associated constraint traits.
1.7.2. Defining traits#
Traits are defined by applying smithy.api#trait to a shape. This trait can only be applied to simple types and aggregate types. By convention, trait shape names SHOULD use a lowercase name so that they visually stand out from normal shapes.
The following example defines a trait with a shape ID of
smithy.example#myTraitName
and applies it to smithy.example#MyString
:
$version: "2"
namespace smithy.example
@trait(selector: "*")
structure myTraitName {}
@myTraitName
string MyString
The following example defines two custom traits: beta
and
structuredTrait
:
$version: "2"
namespace smithy.example
/// A trait that can be applied to a member.
@trait(selector: "structure > member")
structure beta {}
/// A trait that has members.
@trait(selector: "string", conflicts: [beta])
structure structuredTrait {
@required
lorem: StringShape
@required
ipsum: StringShape
dolor: StringShape
}
// Apply the "beta" trait to the "foo" member.
structure MyShape {
@required
@beta
foo: StringShape
}
// Apply the structuredTrait to the string.
@structuredTrait(
lorem: "This is a custom trait!"
ipsum: "lorem and ipsum are both required values.")
string StringShape
Prelude traits
When using the IDL, built-in traits defined in the Smithy
prelude namespace, smithy.api
, are automatically
available in every Smithy model and namespace through relative shape IDs.
References to traits
The only valid reference to a trait is through applying a trait to a shape. Members and references within a model MUST NOT target shapes.
1.7.2.1. trait
trait#
- Summary
- Marks a shape as a trait.
- Trait selector
:is(simpleType, list, map, set, structure, union)
This trait can only be applied to simple types,
list
,map
,set
,structure
, andunion
shapes.- Value type
structure
Trait properties
smithy.api#trait
is a structure that supports the following members:
Property | Type | Description |
---|---|---|
selector | string |
A valid selector that defines where the trait
can be applied. For example, a selector set to :test(list, map)
means that the trait can be applied to a list or
map shape. This value defaults to * if not set,
meaning the trait can be applied to any shape. |
conflicts | [string ] |
Defines the shape IDs of traits that MUST NOT be applied to the same shape as the trait being defined. This allows traits to be defined as mutually exclusive. Provided shape IDs MAY target unknown traits that are not defined in the model. |
structurallyExclusive | string |
One of "member" or "target". When set to "member", only a single member of a structure can be marked with the trait. When set to "target", only a single member of a structure can target a shape marked with this trait. |
breakingChanges | [BreakingChangeRule] | Defines the backward compatibility rules of the trait. |
1.7.2.2. Annotation traits#
A structure trait with no members is called an annotation trait. It's hard to predict what information a trait needs to capture when modeling a domain; a trait might start out as a simple annotation, but later might benefit from additional information. By defining an annotation trait rather than a boolean trait, the trait can safely add optional members over time as needed.
The following example defines an annotation trait named foo
:
$version: "2"
namespace smithy.example
@trait
structure foo {}
A member can be safely added to an annotation trait if the member is not
marked as required. The applications of the foo
trait in the previous example and the following example are all valid even
after adding a member to the foo
trait:
$version: "2"
namespace smithy.example
@trait
structure foo {
baz: String
}
@foo(baz: "bar")
string MyString4
1.7.2.3. Breaking change rules#
Backward compatibility rules of a trait can be defined in the breakingChanges
member of a trait definition. This member is a list of diff rules. Smithy
tooling that performs semantic diff analysis between two versions of the same
model can use these rules to detect breaking or risky changes.
Note
Not every kind of breaking change can be described using the
breakingChanges
property. Such backward compatibility rules SHOULD
instead be described through documentation and ideally enforced through
custom diff tooling.
Property | Type | Description |
---|---|---|
change | string |
Required. The type of change. This value can be set to one of the following:
|
path | string |
A JSON pointer as described in RFC 6901 that points to the values
to compare from the original model to the updated model. If omitted
or if an empty string is provided ("" ), the entire trait is used
as the value for comparison. The provided pointer MUST correctly
correspond to shapes in the model. |
severity | string |
Defines the severity of the change. This value can be set to:
|
message | string |
Provides an optional plain text message that provides information about why the detected change could be problematic. |
It is a backward incompatible change to add the following trait to an existing shape:
@trait(breakingChanges: [{change: "add"}])
structure cannotAdd {}
Note
The above trait definition is equivalent to the following:
@trait(
breakingChanges: [
{
change: "add",
path: "",
severity: "ERROR"
}
]
)
structure cannotAdd {}
It is a backward incompatible change to add or remove the following trait from an existing shape:
@trait(breakingChanges: [{change: "presence"}])
structure cannotToAddOrRemove {}
It is very likely backward incompatible to change the "foo" member of the following trait or to remove the "baz" member:
@trait(
breakingChanges: [
{
change: "update",
path: "/foo",
severity: "DANGER"
},
{
change: "remove",
path: "/baz",
severity: "DANGER"
}
]
)
structure fooBaz {
foo: String,
baz: String
}
So for example, if the following shape:
@fooBaz(foo: "a", baz: "b")
string Example
Is changed to:
@fooBaz(foo: "b")
string Example
Then the change to the foo
member from "a" to "b" is backward
incompatible, as is the removal of the baz
member.
Referring to list members
The JSON pointer can path into the members of a list using a member
segment.
In the following example, it is a breaking change to change values of lists
or sets in instances of the names
trait:
@trait(
breakingChanges: [
{
change: "update",
path: "/names/member"
}
]
)
structure names {
names: NameList
}
@private
list NameList {
member: String
}
So for example, if the following shape:
@names(names: ["Han", "Luke"])
string Example
Is changed to:
@names(names: ["Han", "Chewy"])
string Example
Then the change to the second value of the names
member is
backward incompatible because it changed from Luke
to Chewy
.
Referring to map members
Members of a map shape can be referenced in a JSON pointer using
key
and value
.
The following example defines a trait where it is backward incompatible to remove a key value pair from a map:
@trait(
breakingChanges: [
{
change: "remove",
path: "/key"
}
]
)
map jobs {
key: String,
value: String
}
So for example, if the following shape:
@jobs(Han: "Smuggler", Luke: "Jedi")
string Example
Is changed to:
@jobs(Luke: "Jedi")
string Example
Then the removal of the "Han" entry of the map is flagged as backward incompatible.
The following example detects when values of a map change.
@trait(
breakingChanges: [
{
change: "update",
path: "/value"
}
]
)
map jobs {
key: String,
value: String
}
So for example, if the following shape:
@jobs(Han: "Smuggler", Luke: "Jedi")
string Example
Is changed to:
@jobs(Han: "Smuggler", Luke: "Ghost")
string Example
Then the change to Luke's mapping from "Jedi" to "Ghost" is backward incompatible.
Note
- Using the "update"
change
type with a map key has no effect. - Using any
change
type other than "update" with map values has no effect.
1.8. Prelude#
All Smithy models automatically include a prelude. The prelude defines various simple shapes and every trait defined in the core specification. When using the IDL, shapes defined in the prelude can be referenced from within any namespace using a relative shape ID.
$version: "2"
namespace smithy.api
string String
blob Blob
bigInteger BigInteger
bigDecimal BigDecimal
timestamp Timestamp
document Document
boolean Boolean
byte Byte
short Short
integer Integer
long Long
float Float
double Double
/// The single unit type shape, similar to Void and None in other
/// languages, used to represent no meaningful value.
@unitType
structure Unit {}
@default(false)
boolean PrimitiveBoolean
@default(0)
byte PrimitiveByte
@default(0)
short PrimitiveShort
@default(0)
integer PrimitiveInteger
@default(0)
long PrimitiveLong
@default(0)
float PrimitiveFloat
@default(0)
double PrimitiveDouble
1.8.1. Unit type#
Smithy provides a singular unit type named smithy.api#Unit
. The unit
type in Smithy is similar to Void
and None
in other languages. It is
used when the input or output of an operation has no
meaningful value or if a union member has no meaningful value.
smithy.api#Unit
MUST NOT be referenced in any other context.
The smithy.api#Unit
shape is defined in Smithy's prelude
as a structure shape marked with the smithy.api#unitType
trait to
differentiate it from other structures. It is the only such structure in the
model that can be marked with the smithy.api#unitType
trait.