Namespace TypedRest.CodeGeneration.CSharp
Generates C# source code for TypedRest .NET clients from OpenAPI/Swagger documents.
Note
NuGet package: TypedRest.CodeGeneration.CSharp
Usage
var reader = new OpenApiStreamReader(new OpenApiReaderSettings().AddTypedRest());
var doc = reader.Read(File.OpenRead("myapi.yml"), out _);
foreach (var type in doc.GenerateTypedRest(new GenerationOptions("MyService")
{
Namespace = "MyCompany.MyService",
GenerateInterfaces = true,
GenerateDtos = true
}))
type.WriteToDirectory("myclient/");
GenerateTypedRest uses the endpoints described by the document's x-typedrest extension, or infers them from the paths if there is no such extension.
Extension points
GenerateTypedRest takes an optional PatternRegistry controlling what is inferred, and an optional BuilderRegistry controlling what is emitted:
var types = doc.GenerateTypedRest(options, patterns, builders);
Implement IBuilder<TEndpoint> to change the code emitted for an endpoint kind. A BuilderRegistry holds at most one builder per kind, so replacing a built-in one means composing the registry yourself rather than adding to BuilderRegistry.Default.
To change how types and properties are named, derive from NamingStrategy and pass it to GenerateTypedRestEndpoints.
API
Namespaces
Classes
- GenerationOptions
Options controlling the generation of a TypedRest client.
- KeyExtensions
Helpers for transforming OpenAPI path/property keys before they become C# identifiers.