gpt4 book ai didi

c# - 如何为 OpenApi header 设置字符串列表

转载 作者:行者123 更新时间:2023-12-05 05:06:56 31 4
gpt4 key购买 nike

我正在向 Swagger-UI 添加一个语言 header ,并尝试将其作为一个下拉 列表。出于某种原因,它一直给我一个转换错误。相关代码如下:

 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
}) ;
}

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

Unable to cast object of type 'System.Collections.Generic.List[Microsoft.OpenApi.Any.OpenApiString]' to type 'System.Collections.Generic.IList[Microsoft.OpenApi.Any.IOpenApiAny]'.

没有 enum,一切都很好,并且 header 在 Swagger UI 中的每个请求上显示为默认的文本框。

我花了几个小时试图在文档中找到答案,但我找不到。

最佳答案

好的,我明白了。

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

关于c# - 如何为 OpenApi header 设置字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59580002/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com