I am getting the following error in the swagger 2.0 json that gets auto-generated by Swashbuckle WebApi (ASP.NET WEB API .NET Framework project), when I import it into any API Management platform:
Error: Validation failed. Definition names should match against: /^[a-zA-Z0-9.-_]+$/
The reason is presence of [ and ] brackets in the $ref.
I implemented IOperationFilter and have modified [ and ] brackets in Operation Response Definition Schema to characters allowed by Swagger 2.0 specs.
E.g.
My original Operation definition as auto-generated by Swashbuckle is as follows:
"/v1/api/configuration/notifications/schedules":{"get":{"tags":["Notifications"],"consumes":[],"produces":["application/json","text/json"],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/MyCompany.ItemsDTO [ MyCompany.Web.API.Notifications.DTOs.NotificationModel ]"}}}}}
Notice the [ and ] brackets in bold in $ref above.
I modified the $ref by implemeting IOperation filter.
Modified Operation:
"/v1/api/configuration/notifications/schedules":{"get":{"tags":["Notifications"],"consumes":[],"produces":["application/json","text/json"],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/MyCompany.ItemsDTO_ MyCompany.Web.API.Notifications.DTOs.NotificationModel"}}}}}
Please note that the [ has been replaced by _ (underscore) , while ] has been removed.
But I also need to make similar changes in Model Definition and any $refs in the Model Definition.
E.g. Model Definition:
"definitions":{"MyCompany.ItemsDTO [ MyCompany.Web.API.Notifications.DTOs.NotificationModel ] ":{"type":"object","properties":{"MyNotificationTemplate":{"$ref":"#/definitions/MyCompany.ItemsDTO [ DTOs.NotificationTemplateDTO ] "}}}}
[] brackets in the above Model definition and its $ref highlighted in bold.
I have implemented ISchemaFilter and have created the Apply method:
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
{
}
I can check for [ and ] brackets in the schema object in the Apply method. But I will only be able to modify [ and ] in Model Definition's $ref but not the Model definition name (Key) because the Model Definition name (Key) is not a part of schema object.
Model Definition names(Keys) show up in the schemaRegistry object. But it is a Dictionary of all schemas and model is dynamically added to the schemaRegistry, whenever a new model is found.
If I do not modify the Model Definition name(Key) to match the $ref of Operation definition, then I will get "missing reference" error.
Any pointers on how to make the necessary changes in the Model Definition Schema.
I have already tried the following:
- URL encoding [ and ] by %5B and %5D. Get the same error when importing the swagger json in APIM
- Using HttpConfiguration.EnableSwagger(c => c.SchemaId(type => type.ToString().Replace("[", "").Replace("]", "").Replace(",", "-")); --> It does not make any changes to Schema Definition
- Manually updating the swagger json to replace [ by _ and removing ] in the Model definition and it's $refs. I was able to successfully import this file in APIM. Now I am trying to automate the manual steps that I performed in #3.