Click here to Skip to main content
15,664,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating automatic type-safe REST API (Refit) from Swagger document. I'm Creating Interface and model class manually like below.

I'm having huge number of Swagger files. So I want to do this dynamically, Please help me if anyone know.

What I have tried:

I'm Creating Interface and model class manually like below.
Swagger file:
<pre>"paths": {
"/configuration/v1/devices/{device_serial}": {
      "get": {
        "tags": [
        "summary": "Get variablised template for a Switch.",
        "description": " Response information.",
        "x-deployed": true,
        "operationId": "api.devices.get_device",
        "produces": [
        "parameters": [
            "in": "path",
            "name": "device_serial",
            "description": "Serial number of the device.",
            "required": true,
            "type": "string"
        "responses": {
           "200": {
            "description": "Successful operation.",
            "schema": {
              "type": "object",
              "properties": {
                "total": {
                  "type": "integer"
                "data": {
                  "type": "string"

Interface code:
/// <summary>
        /// Get variablised template for a Switch.
        /// </summary>
        /// <remarks>Response information</remarks>
        /// <exception cref="Swagger.Client.ApiException">Thrown when fails to make API call</exception>
        /// <param name="deviceSerial"></param>
        /// <param name="cancellationToken"></param>
        /// <returns>Task of String</returns>
        Task<DevicesResponse> GetDeviceVariabilisedTemplateAsync(
            [AliasAs("device_serial")] string deviceSerial,
            CancellationToken cancellationToken = default);

Model Class:
    public class DevicesResponse
        [DataMember(Name = "total", EmitDefaultValue = false)]
        public int? Total { get; set; } = default!;

        [DataMember(Name = "data", EmitDefaultValue = false)]
        public string? Data { get; set; } = default!;
Updated 14-Mar-23 0:02am

A quick Google showed up Swagger Codegen | Swagger[^] which can be used to generate models from OpenAPI specifications. Apparently it's open source and free to use as well, so might accomplish what you're looking for.

Also the OpenAPI itself has a repository for generation tools: OpenAPITools/openapi-generator[^] which might also be a viable option. All I had to search for was "C# build models from Swagger"
Share this answer
Durga Ganesh from Chennai 5-Feb-22 1:51am    
Swagger Codegen is not creating Refit code. Please let me know to create Refit automatically
Chris Copeland 7-Feb-22 4:26am    
I don't know what that means (and yikes this is an old question!), I'm not sure what you're after if creating the C# code isn't enough? Again, a very quick Google found me this GitHub repository but I'm not sure if that's what you're after?
You can Generate Refit interfaces from OpenAPI specifications using a tool called Refitter

You can do this via the command line using Refitter directly or from Visual Studio using the extension REST API Client Code Generator
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900