8

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.

shadowsheep
  • 10,376
  • 3
  • 44
  • 65

3 Answers3

5

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 polimorphism wants the base name "BaseClass".

shadowsheep
  • 10,376
  • 3
  • 44
  • 65
  • Here I've got two BaseClass (in my question was MyClass) in two different namespaces: WebApi.Models and WebApi.Modelli – shadowsheep Jul 27 '17 at 16:04
5

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(".", "_");
    }
}
Dejan
  • 6,349
  • 4
  • 51
  • 88
3

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.

Rico Suter
  • 10,556
  • 3
  • 57
  • 89
  • I tried setting my TypeNameGenerator with TypeNameGenerator = new MyTypeNameGenerator() but in generate I've only these parameters: JsonSchema4 schema, string typeNameHint, IEnumerable reservedTypeNames so that I cannot infer package information tha's right the information I need. – shadowsheep Jul 26 '17 at 06:47
  • When using c# OpenAPI connected service ver 1.2.2 Is it possible to (always) use full type names like “com.foo.MyClass“ or “it.foo.MyClass” in generated client code. Im facing the same issue as OP and would hate to rename some classes just to avoid this renaming to MyClass2. Not to mention all that confusion that can result from renamed classes.. – Olavi Vaino Nov 05 '19 at 16:43