How to remove back quote from swagger.json

Viewed 200

In my web application written in ASPNET Core 3.1 is used Swashbuckle.AspNetCore (5.2) for generate OpenApi. I have turned on generate derived classes by

GeneratePolymorphicSchemas()

In swagger.json I get classes with back quote (`). So one of my classes

public abstract class CommandResult
{
    public abstract IsSuccess {get;}
}

public class SuccessCommandResult
{
    public override IsSuccess => true;
}

public class SuccessCommandResult<TData> : SuccessCommand
{
    public TData Data {get;set;}
}

For last of these classes I get their name as 'SuccessCommandResult``1' (there is single back quote character, I doubled for keep pretty post fromatting), which i problematic for generate TypeScript api client. For generate frontend client I use open-api-generator.

My question I how to resolve generation problem?

2 Answers

Generics is not natively supported by OpenApi like that, but you can customize the generated name by using the SchemaId method, e.g.

c.SchemaId(t => t.FullName.Contains('`') 
    ? t.FullName.Substring(0, t.FullName.IndexOf('`')) + "OfT" 
    : t.FullName);

The above is actually from the official documentation, see "Modifying Generated Schemas".

To add on the the above answer, as is, I ran into an issue with

SchemaId already used for different type

when I had specific classes specified in the generic type field on the class documentation. ie.

[ProducesResponseType(typeof(PagedResult<FirstViewModel>), StatusCodes.Status200OK)] [ProducesResponseType(typeof(PagedResult<SecondViewModel>), StatusCodes.Status200OK)]

To fix this, i did the following


        private string GetSchemaId(Type type)
        {
            var schemaBase = type.Name.Contains('`') 
                ? type.Name.Substring(0, type.Name.IndexOf('`')) + "OfT" 
                : type.Name;

            if (!type.IsGenericType)
            {
                return $"{schemaBase}";
            }
            
            string? schemaGeneric = "";
            if (type.GenericTypeArguments.Length == 0)
            {
                schemaGeneric = $"<{Guid.NewGuid()}>";
            }

            schemaGeneric += "<";
            for (var i = 0; i < type.GenericTypeArguments.Length; i++)
            {
                var typeArg = type.GenericTypeArguments.ElementAt(i);

                schemaGeneric += GetSchemaId(typeArg);
                
                if (i == type.GenericTypeArguments.Length - 1) continue;

                schemaGeneric += ", ";
            }

            schemaGeneric += ">";
            return schemaBase + schemaGeneric;
        }

Then call it like so

        public void Configure(SwaggerGenOptions options)
        {
            options.CustomSchemaIds(type =>
            {
                return GetSchemaId(type);
            });
        }  

This will generate schemas like so in swagger

This works well for importing the swagger.json into Visual Studios Connected Services to auto generate the API Client

Related