Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Testing with Swagger Editor, it specifies the follwoing error:

"$ref values must be RFC3986-compliant percent-encoded URIs".

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?

like image 362
kenander Avatar asked Sep 07 '26 21:09

kenander


1 Answers

I had a similar issue when trying to publish a service with api management.

Parsing error(s): The key 'Namespace.Class+NestedClass' in 'schemas' of components MUST match the regular expression '^[a-zA-Z0-9.-_]+$'. [#/components]

In my case using custom schema ids and replacing the character that causes the error solved the problem:

services.AddSwaggerGen(options =>
{
    options.CustomSchemaIds(type => type.FullName.Replace("+", "_"));
});
like image 188
Raul Arrieta Avatar answered Sep 10 '26 12:09

Raul Arrieta