如何为OpenApi标头设置字符串列表

huangapple go评论98阅读模式
英文:

How can I set a list of string for an OpenApi header

问题

I can help you with the translation:

我正在为 Swagger-UI 添加语言头,并尝试将其设置为下拉列表。出现了一个类型转换错误的问题。

以下是相关代码:

public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
    if (operation.Parameters == null)
        operation.Parameters = new List<OpenApiParameter>();

    var enumList = (_serviceProvider.GetService(typeof(IOptions<RequestLocalizationOptions>)) as IOptions<RequestLocalizationOptions>)?
                .Value?.SupportedCultures?.Select(c => new OpenApiString(c.TwoLetterISOLanguageName)).ToList();

    operation.Parameters.Add(new OpenApiParameter
    {
        Name = "Accept-Language",
        In = ParameterLocation.Header,
        Description = "Supported languages",
        Schema = new OpenApiSchema
        {   
            Default = new OpenApiString("en"), 
            Type = "string",
            Enum = (IList<IOpenApiAny>)enumList
        },
        Required = false
    });
}

这是我得到的错误(在运行时):

无法将类型为 'System.Collections.Generic.List[Microsoft.OpenApi.Any.OpenApiString]' 的对象强制转换为类型 'System.Collections.Generic.IList[Microsoft.OpenApi.Any.IOpenApiAny]'。

如果不包含 enum,一切都正常工作,头部会显示在Swagger UI的每个请求中,作为一个带有默认值的文本框。

我花了好几个小时尝试在文档中找到答案,但没有成功。

英文:

I'm adding a language header to the Swagger-UI and trying to have it as a drop-down list. For some reason, it keeps giving me a cast error.
Here is the relevant code:

 public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
                operation.Parameters = new List&lt;OpenApiParameter&gt;();

            var enumList = (_serviceProvider.GetService(typeof(IOptions&lt;RequestLocalizationOptions&gt;)) as IOptions&lt;RequestLocalizationOptions&gt;)?
                        .Value?.SupportedCultures?.Select(c =&gt; new OpenApiString(c.TwoLetterISOLanguageName)).ToList();

            operation.Parameters.Add(new OpenApiParameter
            {
                Name = &quot;Accept-Language&quot;,
                In = ParameterLocation.Header, // &quot;header&quot;,
                Description = &quot;Supported languages&quot;,
                Schema = new OpenApiSchema
                {   
                    Default = new OpenApiString(&quot;en&quot;), 
                    Type = &quot;string&quot;


                    //TODO: Can&#39;t get this conversion to work and I don&#39;t know why...
                    ,
                    Enum = (IList&lt;IOpenApiAny&gt;)enumList

                },
                Required = false
            }) ;
        }

And here is the error I'm getting (at runtime):
>Unable to cast object of type 'System.Collections.Generic.List[Microsoft.OpenApi.Any.OpenApiString]' to type 'System.Collections.Generic.IList[Microsoft.OpenApi.Any.IOpenApiAny]'.

Without the enum, everything works great, and the header shows up on each request in the Swagger UI as a text box with the default.

I spent hours trying to find the answer in the docs, but I couldn't.

答案1

得分: 2

好的,我明白了。

enumList = (_serviceProvider.GetService(typeof(IOptions<RequestLocalizationOptions>)) as IOptions<RequestLocalizationOptions>)?
                            .Value?.SupportedCultures?.Select(c => OpenApiAnyFactory.CreateFor(new OpenApiSchema() { Type: "string" }, c.Name)).ToList();
英文:

Ok, I figured it out.

var enumList = (_serviceProvider.GetService(typeof(IOptions&lt;RequestLocalizationOptions&gt;)) as IOptions&lt;RequestLocalizationOptions&gt;)?
                        .Value?.SupportedCultures?.Select(c =&gt; OpenApiAnyFactory.CreateFor(new OpenApiSchema() { Type = &quot;string&quot; }, c.Name)).ToList();

huangapple
  • 本文由 发表于 2020年1月3日 22:09:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定