Unsaved
Ready
Loading Monaco Editor...
Preview
Styling
Class Diagram
All Documentation templates

API Versioning Class Model — Mermaid Class Diagram

Class diagram documenting an API versioning strategy with versioned controllers, DTOs, and migration utilities.

What this diagram shows

This Mermaid class diagram is how one router serves two API versions. APIRouter keeps a map of version strings to handlers and registers them. VersionHandler is an interface with handle and validate. V1Handler and V2Handler implement it. V1 speaks V1DTO, which is a flat name, email, and phone. V2 speaks V2DTO, which has a full name, a ContactInfo object, and metadata. V2Handler also exposes migrate from V1DTO to V2DTO. VersionMigrator holds a map of transforms and is used by the v2 handler. ContactInfo is composed inside V2DTO.

Use it when a breaking change is being discussed as a new route prefix and you need to see who owns the conversion. The migrator is not on the router. V2Handler points at it, and V2Handler is the only class that knows both DTOs besides the migrator itself. V1Handler never sees V2DTO. That is the compatibility boundary.

Composition versus dependency is drawn on purpose. V2DTO contains ContactInfo (filled diamond). The handlers only depend on the DTOs (dashed arrows).

Mermaid syntax in this template

classDiagram. <|.. realizes the interface. *-- is composition. ..> is a dependency. The router association uses multiplicities.

  • APIRouter "1" --> "*" VersionHandler

    One router, many handlers, stored in the versions map.

  • VersionHandler <|.. V2Handler

    V2Handler is a VersionHandler. The interface marks version, handle, and validate.

  • V2DTO *-- ContactInfo

    Composition. A v2 contact does not exist without its DTO in this model.

  • +migrate(v1Data: V1DTO) V2DTO

    A method on V2Handler, separate from VersionMigrator.migrate, which takes from and to version strings. The overlap is worth resolving.

Variations and ways to customize it

Keep migration off the handler

If every conversion should live in one place, delete migrate from V2Handler and let only VersionMigrator see both DTOs. The handler then asks the migrator.

V2Handler --> VersionMigrator
VersionMigrator ..> V1DTO
VersionMigrator ..> V2DTO

Add a v3 handler

Copy V2Handler, set version to v3, and decide whether it migrates from v2 or from v1. Register it on the router. Do not make v1 depend on v3.

class V3Handler {
    +string version = "v3"
    +handle(request: Request) Response
}
VersionHandler <|.. V3Handler

Show the HTTP path

The router stores version strings. Add a note that v1 means the /v1 prefix if the team also has header-based versioning and the diagram should pick one.

Diagram type

Class Diagram

Tags

APIversioningRESTmigrationdocumentation
Open in Editor

Frequently asked questions

Which class converts a v1 body into a v2 body?

V2Handler.migrate takes a V1DTO and returns a V2DTO. VersionMigrator also depends on both DTOs and exposes migrate(data, from, to). The template draws both, so pick one owner before you implement them.

Does the v1 handler know about v2?

No. V1Handler depends only on V1DTO. The router knows handlers by the VersionHandler interface. The v2 types stay on V2Handler, V2DTO, and the migrator.

What changed between the DTOs?

V1 has name, email, and phone as flat strings. V2 has fullName, a ContactInfo object with email, phone, and preferredChannel, plus metadata. ContactInfo is composed inside V2DTO.

How does the router choose a handler?

It keeps Map of version string to VersionHandler and route(request) selects one. registerVersion adds an entry. The diagram does not say whether the version comes from the path or a header.

Related templates and guides

All Documentation templates