NSwag, how to avoid duplicate enums?

31 views Asked by At

I'm using NSwag.AspNetCore v14.0.3.

I have the following operation filter, the purpose of it is to add the header "Accept-Language" to the generated SDK with the values supported by my application. In this case EN and FR.

public class AcceptedLanguageOperationFilter : IOperationProcessor
{
    public bool Process(OperationProcessorContext context)
    {
        var parameter = new OpenApiParameter
        {
            Name = "Accept-Language",
            Kind = OpenApiParameterKind.Header,
            Description = "Language preference for the response.",
            IsRequired = true,
            IsNullableRaw = true,
            Default = Languages.EnUs,
            Schema = new JsonSchema
            {
                Type = JsonObjectType.String,
                Default = new OpenApiString(Languages.EnUs)
            }
        };

        foreach (var supportedLanguages in typeof(Languages).GetAllPublicConstantValues<string>())
        {
            parameter.Schema.Enumeration.Add(supportedLanguages);
        }

        context.OperationDescription.Operation.Parameters.Add(parameter);

        return true;
    }
}

The issue is that I have duplicate enum in my generated SDK. I have a total of 17 endpoints in my application so it goes until AcceptLanguage17....

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.0.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
    public enum AcceptLanguage
    {

        [System.Runtime.Serialization.EnumMember(Value = @"en-US")]
        EnUS = 0,

        [System.Runtime.Serialization.EnumMember(Value = @"fr-FR")]
        FrFR = 1,

    }

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.0.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
    public enum AcceptLanguage2
    {

        [System.Runtime.Serialization.EnumMember(Value = @"en-US")]
        EnUS = 0,

        [System.Runtime.Serialization.EnumMember(Value = @"fr-FR")]
        FrFR = 1,

    }

How can I get only one enum AcceptLanguage in my generated SDK ?

0

There are 0 answers