gpt4 book ai didi

c# - Autorest + Swagger 无法正确生成 ENUM

转载 作者:太空狗 更新时间:2023-10-29 22:21:23 25 4
gpt4 key购买 nike

我有两个应用程序 AB。在 A 上,我使用 swagger 来描述 API。在 A 上,我还定义了具有一些枚举属性的类。我想在 B 上生成客户端 API。为此,我使用 Autorest。一切正常,除了一件事 - 枚举。由于某种原因,枚举没有正确生成,并且一种类型的属性(最初是枚举)是字符串或 int 类型(取决于 DescribeAllEnumsAsStrings() 的使用。我在下面的示例中使用了它,所以在这种情况下它是字符串)。

通过 swagger 生成 JSON 的枚举定义:

        "Car": {
"properties": {
"color": {
"enum": [
"Red",
"Blue",
"Green"
],
"type": "string"
}
},
"type": "object"
}

在 Startup.cs 中注册 Swagger

// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Car API", Version = "v1" });
c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "description", Name = "Authorization", Type = "apiKey" });
c.DescribeAllEnumsAsStrings();
});

自动复位命令:

autorest --input-file=http://localhost:55448/swagger/v1/swagger.json --csharp --namespace=Online.Integrations.Api.Service.Car --output-folder=. 

生成的类(字符串而不是枚举):

    public partial class Car
{
/// <summary>
/// Initializes a new instance of the Car class.
/// </summary>
public Car()
{
CustomInit();
}

/// <summary>
/// Initializes a new instance of the Car class.
/// </summary>
/// <param name="application">Possible values include: 'Any', 'Web'
/// ...
/// </param>
public Car(string color = default(string))
{
Color = color;
}

/// <summary>
/// An initialization method that performs custom operations like setting defaults
/// </summary>
partial void CustomInit();

/// <summary>
/// </summary>
[JsonProperty(PropertyName = "color")]
public string Color { get; set; }
}

我找到了这个:https://github.com/Azure/autorest/tree/master/docs/extensions#x-ms-enum

有一些 x-ms-enum 扩展,但我不知道如何使用它。我尝试手动编辑 JSON 定义(我在下面的示例中所做的)但它也不成功。生成的类包含 object color = default(object) 而不是第一个示例中的字符串,而不是枚举。

    "color": {
"type": "string",
"enum": [
"Red",
"Blue",
"Green"
],
"x-ms-enum": {
"name": "Color",
"modelAsString": false
},

我很乐意提供任何帮助。谢谢!

编辑:1) 完整的 Swagger 定义(结果类型:Sting):

Swagger 。在枚举定义之后是 "type": "string"

 {"swagger":"2.0","info":{"version":"v1","title":"Audit Overseer API"},"basePath":"/","paths":{"/api/Audit":{"get":{"tags":["Audit"],"operationId":"ApiAuditGet","consumes":[],"produces":["text/plain","application/json","text/json"],"parameters":[{"name":"Id","in":"query","required":false,"type":"string"},{"name":"Application","in":"query","required":false,"type":"string","enum":["Any","Alfa","Beta"]},{"name":"DatabaseName","in":"query","required":false,"type":"string"}],"responses":{"200":{"description":"Success","schema":{"type":"array","items":{"$ref":"#/definitions/Transaction"}}}}}}},"definitions":{"Transaction":{"type":"object","properties":{"callId":{"type":"string"},"actionName":{"type":"string"},"application":{"enum":["Any","Alfa","Beta"],"type":"string"},"httpStatusCode":{"type":"string"},"dateTime":{"format":"date-time","type":"string"}}}},"securityDefinitions":{"Bearer":{"name":"Authorization","in":"header","type":"apiKey","description":"Please insert JWT with Bearer into field"}}}

自动执行:

autorest execution结果:

result

2) 完整的 Swagger 定义(结果类型:对象):

Swagger 。我在枚举定义后立即对“x-ms-enum”进行实验

{"basePath":"/","definitions":{"Transaction":{"properties":{"actionName":{"type":"string"},"application":{"enum":["Any","Alfa","Beta"],"x-ms-enum":{"modelAsString":false,"name":"Application"}},"callId":{"type":"string"},"dateTime":{"format":"date-time","type":"string"},"httpStatusCode":{"type":"string"}},"type":"object"}},"info":{"title":"Audit Overseer API","version":"v1"},"paths":{"/api/Audit":{"get":{"consumes":[],"operationId":"ApiAuditGet","parameters":[{"in":"query","name":"Id","required":false,"type":"string"},{"enum":["Any","Alfa","Beta"],"in":"query","name":"Application","required":false,"type":"string"},{"in":"query","name":"DatabaseName","required":false,"type":"string"}],"produces":["text/plain","application/json","text/json"],"responses":{"200":{"description":"Success","schema":{"items":{"$ref":"#/definitions/Transaction"},"type":"array"}}},"tags":["Audit"]}}},"securityDefinitions":{"Bearer":{"description":"Please insert JWT with Bearer into field","in":"header","name":"Authorization","type":"apiKey"}},"swagger":"2.0"}

...或者我可以将其留空。结果是一样的

{"basePath":"/","definitions":{"Transaction":{"properties":{"actionName":{"type":"string"},"application":{"enum":["Any","Alfa","Beta"]},"callId":{"type":"string"},"dateTime":{"format":"date-time","type":"string"},"httpStatusCode":{"type":"string"}},"type":"object"}},"info":{"title":"Audit Overseer API","version":"v1"},"paths":{"/api/Audit":{"get":{"consumes":[],"operationId":"ApiAuditGet","parameters":[{"in":"query","name":"Id","required":false,"type":"string"},{"enum":["Any","Alfa","Beta"],"in":"query","name":"Application","required":false,"type":"string"},{"in":"query","name":"DatabaseName","required":false,"type":"string"}],"produces":["text/plain","application/json","text/json"],"responses":{"200":{"description":"Success","schema":{"items":{"$ref":"#/definitions/Transaction"},"type":"array"}}},"tags":["Audit"]}}},"securityDefinitions":{"Bearer":{"description":"Please insert JWT with Bearer into field","in":"header","name":"Authorization","type":"apiKey"}},"swagger":"2.0"}

(现在让我们像以前一样执行相同的 autorest 命令)

结果是:

enter image description here

最佳答案

看起来你可以得到 SwaggerGen自动添加 x-ms-enum使用 SchemaFilter<TFilter> 进行扩展期权法。

在 Startup.cs 中注册 Swagger

// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Car API", Version = "v1" });
c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "description", Name = "Authorization", Type = "apiKey" });
c.ApplyFiltersToAllSchemas();//ignore deprecation warning
c.SchemaFilter<EnumFilter>();
c.DescribeAllEnumsAsStrings();
});

然后定义一个EnumFilter使用以下代码类

public class EnumFilter : ISchemaFilter
{
public void Apply(Schema model, SchemaFilterContext context)
{
if (model == null)
throw new ArgumentNullException("model");

if (context == null)
throw new ArgumentNullException("context");

if (context.SystemType.IsEnum)
model.Extensions.Add("x-ms-enum", new
{
name = context.SystemType.Name,
modelAsString = false
});
}
}

关于c# - Autorest + Swagger 无法正确生成 ENUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46237442/

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