Skip to content

API contracts standard

This page sets the rules for the contracts a ConnectSoft service exposes over REST and gRPC: paths, versions, data types, errors and the mapping between the wire and the domain. It is written for engineers who add or change endpoints, DTOs and mapping profiles, and for their reviewers. The concepts are explained in REST API, gRPC, Service Model and API Design and Management.

The contract is the ServiceModel package

A service publishes its contracts, the REST DTOs and the gRPC service interfaces, in its *.ServiceModel package. Gateways, front ends and other services reference that package and deserialize its types. A change to any type in it, or to a path, is a contract change for every consumer that references the package, whether or not the consumer's code names the changed type.

Rules

CS-API-001: Use lowercase, hyphenated, plural paths for new REST contracts

Severity: major · Applies to: service, api-client, template · Enforced by: manual · Status: active

Why

One path style across services makes routes predictable and follows common REST guidelines. Changing an existing path breaks every client, so existing paths stay as they are. The rule is settled by ADR-0105 §6.

Do

  • Write new resource paths in lowercase, with hyphens between words and plural resource names, for example /api/orders/{orderId}/line-items.
  • In an existing service, follow this rule for a new controller unless it extends a resource family that already uses another casing.
  • Use nouns for resources and HTTP methods for actions.

Don't

  • Rename an existing path to follow this rule.
  • Use api/[controller] for a new controller, because it produces PascalCase paths.
  • Put verbs in a path, as in /api/create-order.

Example

// Do
[Route("api/orders")]
public sealed class OrdersController : ControllerBase

// Don't, in a new contract
[Route("api/[controller]")]
public sealed class OrdersController : ControllerBase

How to check

  • New [Route] templates in the change are lowercase, hyphenated and plural.
  • No new controller uses the api/[controller] route template.
  • No new route has a verb segment, such as create- in /api/create-order.

CS-API-002: Version internal contracts through the ServiceModel package, not the URL

Severity: major · Applies to: service, api-client, template · Enforced by: manual · Status: active

Why

Internal consumers reference the service's *.ServiceModel package and pin a compatible version range, so the package version already says which contract they speak. A version segment in every internal URL adds routes to maintain without adding protection. Public clients cannot pin a package, so a public gateway versions its URLs.

Do

  • In an internal service, publish contract changes as new versions of the *.ServiceModel package, with no version segment in the path.
  • In a public API gateway, version the public routes with Asp.Versioning URL segments, for example /api/v1/orders.
  • Announce the retirement of a public version with deprecation and Sunset headers before removing it.

Don't

  • Add /v1/ to the routes of an internal service.
  • Use the retired Microsoft.AspNetCore.Mvc.Versioning package.

How to check

  • Routes of internal services in the change have no version segment.
  • Public gateway routes use Asp.Versioning and carry a version segment.
  • When a change removes a public gateway version, a released version already sent deprecation and Sunset headers for it.

CS-API-003: Release every contract change as a ServiceModel version

Severity: major · Applies to: service, api-client, template · Enforced by: manual · Status: active

Why

Consumers pin a range of the ServiceModel package. When a breaking change ships as a minor or patch version, consumers pick it up without a code change and fail at run time on deserialization or on a missing route.

Do

  • Raise the major version of *.ServiceModel for a breaking change: a removed or renamed field, path or operation, a changed type, or a changed meaning.
  • Raise the minor version for an additive change: a new optional field, a new endpoint or a new operation.
  • Add fields as optional, so that older consumers can ignore them.

Don't

  • Remove or rename a field, a path or an operation in a minor or patch version.

How to check

The change states the contract change and the version it ships in, and the version step matches the kind of change.

CS-API-004: Send enums as strings and treat a new member as a contract change

Severity: major · Applies to: service, api-client, template · Enforced by: manual · Status: active

Why

Enum values sent as strings are readable in logs and stay correct when members are reordered. A consumer that deserializes a DTO carrying an enum fails on a member it does not know, even when its code never names the enum type. A search for the enum's name therefore misses consumers that only reach the enum through a DTO, and the failure can stay hidden until a response first contains the new value.

Do

  • Serialize enums as strings with JsonStringEnumConverter, as the templates configure.
  • When you add a member to an enum that any contract DTO carries, list the consumers from the DTO graph: every service that references the package and deserializes any response or message type that carries the enum.
  • Release the change as a ServiceModel version and update those consumers before the service starts sending the new value.

Don't

  • Serialize contract enums as numbers.
  • Decide that no consumer is affected because no repository mentions the enum's name.

How to check

  • New contract enums are serialized as strings.
  • A change that adds an enum member lists the consumers found from the DTO graph and their package updates.

CS-API-005: Return errors as ProblemDetails through the shared mappings

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Why

Clients, gateways and tests read one error shape, ProblemDetails as defined by RFC 9457, from every service. Mapping exceptions in one place keeps the status codes consistent and keeps exception details out of responses outside Development.

Do

  • Map each domain exception to a status code in the service's ProblemDetails configuration, next to the shared mappings that the templates provide.
  • Return ValidationProblemDetails for validation failures.
  • Map refusals as CS-AUZ-004 requires.

Don't

  • Return error strings, booleans or custom error envelopes.
  • Catch exceptions in a controller to build an error response by hand.

How to check

Every new exception type that can leave a domain service has a mapping, and a test calls the endpoint and asserts the status and the ProblemDetails body.

CS-API-006: Define gRPC contracts code-first in the ServiceModel

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Why

ConnectSoft services expose the same contract over REST and gRPC. With code-first ServiceModel.Grpc, the C# interface in *.ServiceModel is the gRPC contract, and clients call that interface through ClientFactory, so there is no second contract in .proto files to keep in step.

Do

  • Declare gRPC services as C# interfaces with [ServiceContract] and [OperationContract] in *.ServiceModel, with [DataContract] DTOs.
  • Keep *.ServiceModel.Grpc for server-side adapter classes only.
  • Wrap a third-party .proto surface in an anti-corruption layer outside *.ServiceModel.

Don't

  • Add .proto files, Grpc.Tools or <Protobuf Include> items to a ConnectSoft-owned contract.

How to check

The architecture tests that assert the absence of .proto files and Grpc.Tools pass.

CS-API-007: Round-trip the whole record in a full-replace update

Severity: blocker · Applies to: service, blazor, api-client · Enforced by: manual · Status: active

Why

A PUT replaces the stored record with the request. A field the client leaves out, or sends from a display model that rounded, formatted or never loaded it, is overwritten. A small action built on a full-replace endpoint, such as "end this assignment", then wipes every other field. Tests that stub the payload do not show it.

Do

  • Build every edit on a full-replace endpoint from a client model that holds every field of the record without loss.
  • Add a test that saves an unchanged record and asserts that every field is still the same.

Don't

  • Build an edit request from a display or view model that formats, rounds or omits fields.

How to check

  • For each edit path on a PUT endpoint, compare the DTO with the client model and the request builder; every field is carried.
  • Each full-replace edit path has a test that saves an unchanged record and asserts that every field is still the same.

CS-API-008: Map every field through every hop

Severity: major · Applies to: service, api-client, blazor · Enforced by: manual · Status: active

Why

A request or response can pass through several maps: the gateway's view model, the gateway's output type, the service's contract and the service's domain model. A field missing from one of them is dropped without an error, because an unused source member is legal. A null collection that means "keep" becomes an empty list in every map that does not allow null, so a unit test of one service passes while the end-to-end path clears the data.

Do

  • When adding a field to a response, add it to every type on the path, including the wire view model of a gateway.
  • When a null collection means "keep the current values", allow null for it in every mapping profile on the path.
  • Prove a new field or a null-means-keep collection with an end-to-end test through the real services.

Don't

  • Rely on one service's mapping test to prove a field reaches the client.

How to check

  • For a new field, list the types on the path and check each one and each mapping profile in the change.
  • An end-to-end test through the real services proves that each new field reaches the client and that each null-means-keep collection is kept.

CS-API-009: Never let a request body set a server-stamped field

Severity: blocker · Applies to: service, template · Enforced by: manual · Status: active

Why

Some input fields are set by the server from the caller's token, such as who changed or decided something. When the request map fills them from the body, a caller can claim another identity's authority.

Do

  • Stamp such fields in the controller or use case from the validated token.
  • Mark them Ignore() explicitly in the request mapping profile, so the mapping validation shows that the body cannot set them.

Don't

  • Expose a server-stamped field on a request DTO.
  • Turn off unmapped-member validation to make the mapping pass.

How to check

Every server-stamped input field has an explicit Ignore() in its request map and is set from the token.

CS-API-010: Log infrastructure failures that are mapped to a 4xx status

Severity: major · Applies to: service, template · Enforced by: manual · Status: retired

Replaced by: CS-ERR-003. Logging of infrastructure failures mapped to a 4xx status is kept on the error handling page.

CS-API-011: Read a paged endpoint to the end, by its total count

Severity: major · Applies to: service, api-client, blazor, template · Enforced by: manual · Status: active

Why

A server clamps the page size it is asked for, and a search has a default page size. Code that stops on a short page, or that treats one page as the whole set, returns partial totals, partial exclusion lists and missing lookups that appear only once the data grows.

Do

  • When the full set is needed, pass an explicit page size and walk pages until the number of rows read reaches the total count.
  • Stop the walk when a page returns no rows, so a changing total cannot loop forever.

Don't

  • Stop a walk because a page is shorter than requested.
  • Join or look up against the first page of a paged search.

How to check

Every loop over a paged endpoint in the change stops on the total count and has a no-progress guard, and a test runs it against a fake server that clamps the page size.

CS-API-012: Name the child a command changes

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Why

When a parent can hold several children, a command that names only the parent forces the service to guess which child to change. It picks one, or refuses because of another.

Do

  • Carry the child's id in every command that changes the state of one child.
  • Refuse the command with 404 when the child does not belong to the named parent.

Don't

  • Resolve the target child inside the service from the parent id alone.

How to check

  • Each changed command that alters a child's state carries the child's id, and a test with two children asserts that only the targeted one changes.
  • A test sends the id of a child that belongs to another parent and asserts 404.

CS-API-013: Read internal service responses through the shared response guard

Severity: major · Applies to: service · Enforced by: manual · Status: active

Why

A refusal from another service carries a status and a ProblemDetails body that the caller needs. Helpers that throw on any non-success status, such as GetFromJsonAsync, discard both, and the user sees a generic failure.

Do

  • Send calls to internal services through one shared guard that passes the downstream status and ProblemDetails on to the caller.
  • Read the body only after the guard has handled a non-success status.

Don't

  • Call GetFromJsonAsync, EnsureSuccessStatusCode() or similar helpers on a call whose refusal must reach the caller.

How to check

Each new call to an internal service uses the shared guard, and a test with a stubbed downstream 4xx asserts the status and reason the caller receives.

CS-API-014: Leave a response body readable in a delegating handler

Severity: major · Applies to: service, library · Enforced by: manual · Status: active

Why

A DelegatingHandler that reads the response body to inspect it consumes a forward-only stream. The caller then fails while reading the body, and the original status is lost.

Do

  • Buffer the content with LoadIntoBufferAsync before reading it, or replace the content with a buffered copy.
  • Let a read failure propagate instead of catching it inside the handler.

Don't

  • Read response.Content in a handler without leaving it readable for the caller.

How to check

A handler test with a forward-only StreamContent asserts that the caller can read the body after the handler ran.

CS-API-015: Remove a compensating check only after the replacing downstream is deployed

Severity: blocker · Applies to: service · Enforced by: manual · Status: active

Why

A caller often filters or checks something itself until a downstream service can do it. If the caller's check is removed in the same release that starts sending the new parameter, and the running downstream predates that parameter, the parameter is ignored and the check is gone.

Do

  • Ship the downstream change first, and confirm that the deployed version honours the new parameter.
  • Remove the caller's compensating check in a later change, with an end-to-end test against the deployed downstream.

Don't

  • Delete a compensating filter or check in the same change that adds the parameter that replaces it.

How to check

A change that removes a compensating check names the downstream version that replaces it, and that version is already deployed to the target environment.