This is a sample project for using TypedRest for .NET. It provides a simple REST API for storing contacts in an address book and uses SQLite for persistance.
The code is split into:
- Service/: an ASP.NET Core Web API Service
- Client/: a handwritten TypedRest client library
- GeneratedClient/: a TypedRest client library generated from the Service's OpenAPI document
- Dto/: DTOs shared by the Client and the Service
- UnitTests/: Tests that ensure both clients and the Service work together
The Service describes itself with an OpenAPI document, served at /openapi/v1.json together with an interactive API reference at /scalar.
The same document is also written to Service/AddressBook.json on every build, so changes to the API show up in diffs.
The GeneratedClient/ project uses the TypedRest source generator to build a client from Service/AddressBook.json during compilation:
<TypedRestOpenApi Include="..\Service\AddressBook.json" ServiceName="AddressBook" Serializer="system-text-json" />The project depends on the Service only for build ordering, so the document is regenerated before the client is compiled. Endpoints, DTOs and a constructor taking the base URI (configured for System.Text.Json) are generated. AddressBookClient.cs adds one more constructor in a partial class, which takes a custom HttpClient for testing. The generated code is written to GeneratedClient/obj/ for inspection.
Compare it with the handwritten Client/, which produces the same endpoint structure but shares its DTOs with the Service.