A C# Source Generator that uses Refitter for generating a REST API Client using the Refit library from OpenAPI specifications.
$ dotnet add package Refitter.SourceGeneratorRefitter is available as a C# Source Generator that uses the Refitter.Core library for generating a REST API Client using the Refit library. Refitter can generate the Refit interface from OpenAPI specifications
The Refitter source generator is a bit untraditional in a sense that it creates a folder called Generated in the same location as the .refitter file and generates files to disk under the Generated folder. The source generator output should be included in the project and committed to source control. This is done because there is no other way to trigger the Refit source generator to pickup the Refitter generated code
(Translation: I couldn't for the life of me figure how to get that to work, sorry)
The source generator is distributed as a NuGet package and should be installed to the project that will contain the generated code
dotnet add package Refitter.SourceGenerator
This source generator generates code based on any .refitter file included to the project as AdditionalFiles.
The generator can automatically detect all .refitter files inside the project that referenced the Refitter.SourceGenerator package and there is no need to include them manually as AdditionalFiles
The following is an example .refitter file
{
"openApiPath": "/path/to/your/openAPI", // Required
"namespace": "Org.System.Service.Api.GeneratedCode", // Optional. Default=GeneratedCode
"naming": {
"useOpenApiTitle": false, // Optional. Default=true
"interfaceName": "MyApiClient" // Optional. Default=ApiClient
},
"generateContracts": true, // Optional. Default=true
"generateXmlDocCodeComments": true, // Optional. Default=true
"addAutoGeneratedHeader": true, // Optional. Default=true
"addAcceptHeaders": true, // Optional. Default=true
"returnIApiResponse": false, // Optional. Default=false
"generateOperationHeaders": true, // Optional. Default=true
"typeAccessibility": "Public", // Optional. Values=Public|Internal. Default=Public
"useCancellationTokens": false, // Optional. Default=false
"useIsoDateFormat": false, // Optional. Default=false
"multipleInterfaces": "ByEndpoint", // Optional. May be one of "ByEndpoint" or "ByTag"
"additionalNamespaces": [ // Optional
"Namespace1",
"Namespace2"
],
"tag": [ // Optional. OpenAPI Tag to include when generating code
"Pet",
"Store",
"User"
]
}
openApiPath - points to the OpenAPI Specifications file. This can be the path to a file stored on disk, relative to the .refitter file. This can also be a URL to a remote file that will be downloaded over HTTP/HTTPSnamespace - the namespace used in the generated code. If not specified, this defaults to GeneratedCodenaming.useOpenApiTitle - a boolean indicating whether the OpenApi title should be used. Default is truenaming.interfaceName - the name of the generated interface. The generated code will automatically prefix this with I so if this set to MyApiClient then the generated interface is called IMyApiClient. Default is ApiClientgenerateContracts - a boolean indicating whether contracts should be generated. A use case for this is several API clients use the same contracts. Default is truegenerateXmlDocCodeComments - a boolean indicating whether XML doc comments should be generated. Default is trueaddAutoGeneratedHeader - a boolean indicating whether XML doc comments should be generated. Default is trueaddAcceptHeaders - a boolean indicating whether to add accept headers [Headers("Accept: application/json")]. Default is truereturnIApiResponse - a boolean indicating whether to return IApiResponse<T> objects. Default is falsegenerateOperationHeaders - a boolean indicating whether to use operation headers in the generated methods. Default is truetypeAccessibility - the generated type accessibility. Possible values are Public and Internal. Default is PublicuseCancellationTokens - Use cancellation tokens in the generated methods. Default is falseuseIsoDateFormat - Set to true to explicitly format date query string parameters in ISO 8601 standard date format using delimiters (for example: 2023-06-15). Default is falsemultipleInterfaces - Set to ByEndpoint to generate an interface for each endpoint, or ByTag to group Endpoints by their Tag (like SwaggerUI groups them).additionalNamespaces - A collection of additional namespaces to include in the generated file. A use case for this is when you want to reuse contracts from a different namespace than the generated code. Default is emptytag - A collection of tags to use a filter for including endpoints that contain this tag.match-path - A collection of regular expressions used to filter paths.