英文:
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<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, // "header",
Description = "Supported languages",
Schema = new OpenApiSchema
{
Default = new OpenApiString("en"),
Type = "string"
//TODO: Can't get this conversion to work and I don't know why...
,
Enum = (IList<IOpenApiAny>)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<RequestLocalizationOptions>)) as IOptions<RequestLocalizationOptions>)?
.Value?.SupportedCultures?.Select(c => OpenApiAnyFactory.CreateFor(new OpenApiSchema() { Type = "string" }, c.Name)).ToList();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论