kenander
kenander

Reputation: 75

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?

Upvotes: 6

Views: 4244

Answers (1)

Raul Arrieta
Raul Arrieta

Reputation: 136

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("+", "_"));
});

Upvotes: 11

Related Questions