Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSwag namespace in model names

Tags:

nswag

It's possible to generate client code so that model's class names have full namespaces as prefix?

That should avoid same class name conflicts.

Example

com.foo.MyClass 

and

it.foo.MyClass

Up to now what I got is MyClass and MyClass2 that's not so much meaningful.

Should be better to have, in case of name collision, ComFooMyClass and ItFooMyClass.

like image 664
shadowsheep Avatar asked Jul 21 '17 15:07

shadowsheep


3 Answers

Let me update shadowsheep's answer for a more recent version of NSwag:

services.AddSwaggerDocument(cfg => { cfg.SchemaNameGenerator = new CustomSchemaNameGenerator(); });

With:

internal class CustomSchemaNameGenerator : ISchemaNameGenerator
{
    public string Generate(Type type)
    {
        return type.FullName.Replace(".", "_");
    }
}
like image 56
Dejan Avatar answered Nov 20 '22 17:11

Dejan


When using NSwag via C#, you can provide an own TypeNameGenerator (via the settings object) to customize the way how the class names are generated.

like image 44
Rico Suter Avatar answered Nov 20 '22 17:11

Rico Suter


I've found a solution using a custom SchemaNameGenerator instead of a custom TypeNameGenerator (where I don't have package information).

internal class MySchemaNameGenerator : DefaultSchemaNameGenerator, ISchemaNameGenerator
{
    public override string Generate(Type type)
    {
        string retValue = base.Generate(type);
        // Quite ugly but do fix the concept
        if (retValue.Equals("BaseClass"))
        {
            retValue = type.FullName.Replace(".","_");
        }
        return retValue;
    }
}

Always set through settings:

 app.UseSwaggerUi(typeof(WebApiApplication).Assembly, new SwaggerUiSettings
                {
                    SchemaNameGenerator = new MySchemaNameGenerator(),
                    ...

This way I get something more meaningful

"/api/test/models/base": {
      "get": {
        "tags": [
          "Test"
        ],
        "operationId": "Test_Get2",
        "parameters": [],
        "responses": {
          "200": {
            "description": "",
            "schema": {
              "$ref": "#/definitions/WebApi_Models_BaseClass"
            },
            "x-nullable": true
          }
        }
      }
    },
    "/api/test/models/extended": {
      "get": {
        "tags": [
          "Test"
        ],
        "operationId": "Test_Get3",
        "parameters": [],
        "responses": {
          "200": {
            "description": "",
            "schema": {
              "$ref": "#/definitions/ExtendedClass"
            },
            "x-nullable": true
          }
        }
      }
    },
    "/api/test/modelli/base": {
      "get": {
        "tags": [
          "Test"
        ],
        "operationId": "Test_Get4",
        "parameters": [],
        "responses": {
          "200": {
            "description": "",
            "schema": {
              "$ref": "#/definitions/WebApi_Modelli_BaseClass"
            },
            "x-nullable": true
          }
        }
      }
    },

Even if the discriminator property for polymorphism wants the base name "BaseClass".

like image 5
shadowsheep Avatar answered Nov 20 '22 18:11

shadowsheep