OpenAPI and controllers
Most .NET HTTP APIs are written code first: controllers or minimal APIs define the endpoints, Microsoft.AspNetCore.OpenApi describes them as an OpenAPI document, and tools such as Kiota or NSwag generate clients from it. The contract-first alternative writes an OpenAPI document, by hand or in TypeSpec, and generates code from that.
Our position is that contract first is the better way to build APIs in an organization of many teams, and that Smithy with NSmithy is the better way to do contract first: easier to write and review, able to say more about how an API behaves, and independent of the wire protocol.
Contract first
Section titled “Contract first”When one team builds a service and several others call it, the contract is the thing they share. Contract first lets them review it before the server exists, generate clients and servers from the agreed version, and work in parallel. Each consumer adopts a new contract version when it chooses, and Smithy Diff flags breaking changes in review or CI.
In a code-first API the implementation is the contract. The OpenAPI document is derived from the code, so a renamed property, a changed route, or a serializer setting changes the contract whether or not anyone meant it to. The document describes what the generator infers from attributes and types, while routing, binding, and serialization are configured separately, and clients generated from the document inherit any gap between the two.
Ergonomics
Section titled “Ergonomics”The same operation, a read with a path parameter and a modeled 404, in Smithy:
@readonly@http(method: "GET", uri: "/orders/{orderId}")operation GetOrder { input := { @required @httpLabel orderId: String } output := { @required status: String } errors: [NoSuchOrder]}
@error("client")@httpError(404)structure NoSuchOrder { message: String}and in OpenAPI:
paths: /orders/{orderId}: get: operationId: GetOrder parameters: - name: orderId in: path required: true schema: type: string responses: "200": description: OK content: application/json: schema: $ref: "#/components/schemas/GetOrderOutput" "404": description: NoSuchOrder content: application/json: schema: $ref: "#/components/schemas/NoSuchOrder"components: schemas: GetOrderOutput: type: object required: [status] properties: status: type: string NoSuchOrder: type: object properties: message: type: stringThe Smithy version reads as an operation with an input, an output, and an error. The OpenAPI version reads as an HTTP exchange, and the operation has to be reassembled from paths, parameters, responses, and schema references.
NSmithy adds to that on the .NET side:
- Generation is part of
dotnet build. There is no separate generator command and no generated code to check in. - The server implements the contract. Generated handler interfaces take modeled inputs and return modeled outputs, so a missing operation or a changed signature is a compile error. The runtime does the routing, serialization, and validation the model specifies.
- Client and server come from one generator and one runtime, checked against Smithy’s protocol conformance suites; see Protocol status.
- Contracts are packages. A model ships as a NuGet package or a Maven JAR, and consumers reference it like any other dependency.
Expressiveness
Section titled “Expressiveness”A Smithy model describes how an API behaves, not only the shape of its data. NSmithy generates code from these traits:
| Trait | What NSmithy generates |
|---|---|
@paginated | IAsyncEnumerable paginators over pages and items |
@waitable | Waiters that poll an operation until a resource reaches a state |
@retryable | Retry and throttling classification for modeled errors |
@idempotencyToken | A token the client fills in, so retries are safe |
| Constraint traits | Server-side validation before the handler runs |
@examples | Fake clients and fake handlers for tests |
@smokeTests | Smoke tests against a running service |
@endpointRuleSet | Endpoint resolution from client parameters |
Waiters, fake clients, smoke tests, and endpoint resolvers are generated for C# only so far; see F#.
OpenAPI’s schema keywords cover the constraints. The rest has no standard OpenAPI equivalent and is usually expressed through vendor extensions, which each generator reads differently or not at all. Organizations can add their own traits the same way Smithy defines these.
Protocol agnostic
Section titled “Protocol agnostic”OpenAPI describes HTTP, so an OpenAPI contract is an HTTP contract. A Smithy model keeps the service separate from its wire protocol: the same operations can be served as REST JSON, RPC v2 CBOR or JSON, and gRPC, from one handler set. An organization with REST and gRPC services keeps one contract format for both, and Smithy still generates OpenAPI for HTTP tooling that needs it.
Where code first or OpenAPI is the better fit
Section titled “Where code first or OpenAPI is the better fit”- One API, one consumer. An API used only by a frontend in the same repository gains little from a separate contract. Controllers or minimal APIs are less ceremony.
- The OpenAPI document is the deliverable. If partners consume the document directly and its exact shape matters, authoring OpenAPI keeps full control over it. Smithy’s OpenAPI output is a translation of the model. TypeSpec is designed for that case.
- An existing API. Moving a large controller-based API to a model means modeling every endpoint as it behaves today.
- Build requirements. Generation runs the Smithy CLI, a Java application,
during
dotnet build. The optionalNSmithy.SmithyClipackage provides it with its Java runtime, so nothing is installed separately, but it adds to build time. - Maturity. NSmithy is in preview. Known limitations lists what is missing.