nswag

NSwag namespace in model names


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.


Solution

  • 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".