简体   繁体   English

c# swagger 嵌套类时 openapi 错误引用

[英]c# swagger openapi erronous refs when nested classes

Default swagger generation for my API gives me errors when uploading my OpenAPI to Azure API Management.我的 API 的默认 swagger 生成在将我的 OpenAPI 上传到 Azure ZDB974238714CA8ADE634A 管理时出现错误。 Testing with Swagger Editor, it specifies the follwoing error:使用 Swagger 编辑器进行测试,它指定了以下错误:

"$ref values must be RFC3986-compliant percent-encoded URIs". “$ref 值必须是符合 RFC3986 的百分比编码 URI”。

It seems my data types with nested classes is transformed to:我的嵌套类的数据类型似乎转换为:

$ref: >-
              #/components/schemas/Namespace.Class+NestedClass

I guess it is the '+'-sign that either needs to be removed or encoded.我想这是需要删除或编码的“+”号。 Any suggestions on how to achieve this?关于如何实现这一目标的任何建议?

I had a similar issue when trying to publish a service with api management.在尝试使用 api 管理发布服务时,我遇到了类似的问题。

Parsing error(s): The key 'Namespace.Class+NestedClass' in 'schemas' of components MUST match the regular expression '^[a-zA-Z0-9.-_]+$'.解析错误:组件的“模式”中的键“Namespace.Class+NestedClass”必须匹配正则表达式“^[a-zA-Z0-9.-_]+$”。 [#/components] [#/成分]

In my case using custom schema ids and replacing the character that causes the error solved the problem:在我的情况下,使用自定义模式 ID 并替换导致错误的字符解决了问题:

services.AddSwaggerGen(options =>
{
    options.CustomSchemaIds(type => type.FullName.Replace("+", "_"));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM