我正在尝试将多个OpenAPI规范合并到一个OpenAPI文档中。然而,我试图合并的一些OpenAPI规范包含了具有相同密钥名但值/方案不同的Components.Schemes。我的最初解决方案是检查密钥是否已经存在,如果存在,我只需将密钥名更改为"KeyName2“、"KeyName3”等等。这是当前的代码:
// Components --> Schemas
int iterator = 2
foreach (var schemas_ite in importedOpenApiDocument.Components.Schemas)
{
if (document.Components.Schemas.ContainsKey(schemas_ite.Key))
{
string newSchemesString = schemas_ite.Key + iterator;
document.Components.Schemas.Add(newSchemesString, schemas_ite.Value);
iterator += 1
}
else
{
document.Components.Schemas.Add(schemas_ite.Key, schemas_ite.Value);
}
}当im序列化合并的OpenAPI规范时,新键按预期存在,但新键的值只是引用前一个相同的键,尽管方案的值/内容不同。
合并的OpenAPI文档的输出如下:
components:
schemas:
ProblemDetails:
type: object
properties:
title2:
Subscription:
type: string
ProblemDetails2:
$ref: '#/components/schemas/ProblemDetails'
Subscription2:
$ref: '#/components/schemas/Subscription'如您所见,ProblemDetails2和Subscription2只是使用ref方案创建的,尽管内容是不同的。我该如何解决这个问题?我知道有一个叫做SerializeAsV3WithoutReferences的函数,但是我不知道如何使用它或者我应该使用什么。
发布于 2022-05-01 22:28:01
看起来您正在使用.net,也许可以尝试将以下内容添加到您的Startup.cs类中
services.ConfigureSwaggerGen(opt =>
{
opt.CustomSchemaIds(x => x.FullName);
});或SwaggerConfig文件夹中的App_Start
GlobalConfiguration
.Configuration
.EnableSwagger(c =>
{
c.PrettyPrint();
c.UseFullTypeNameInSchemaIds();
});https://stackoverflow.com/questions/72080711
复制相似问题