OpenAPI.NET icon indicating copy to clipboard operation
OpenAPI.NET copied to clipboard

OpenApiDiagnostic error when key contains square brackets for components

Open Shwetap05 opened this issue 7 years ago • 2 comments

If a V2 json generated using swashbuckle contains square brackets in the key of the #definitions, OpenApiDiagnostics throw below error. Is there a reason why square brackets are not allowed? This validation is blocking one of the internal tool that consumes OpenApi.Net

Message:"The key 'Core.Collections.ResourceCollection[Services.Products.V2.Models.Availability]' in 'schemas' of components MUST match the regular expression '^[a-zA-Z0-9\.\-_]+$'." Pointer: "#/components" RuleName: "KeyMustBeRegularExpression"

Sample Json which is failing OpenApi.NET validation but renders fine in swagger editor:

{ "swagger": "2.0", "info": { "version": "v1", "title": "TestService", "description": "Test service" }, "host": "test.net", "basePath": "/test/WebApiV2", "schemes": [ "https" ], "paths": { "/v1/products/{productId}/skus/{skuId}/availabilities": { "get": { "tags": [ "Availability" ], "operationId": "Get /v1/products/{productId}/skus/{skuId}/availabilities?country={country_value}&targetSegment={targetSegment_value}", "consumes": [], "produces": [ "application/json", "text/json", "application/xml", "text/xml" ], "parameters": [ { "name": "productId", "in": "path", "required": true, "type": "string" }, { "name": "skuId", "in": "path", "required": true, "type": "string" }, { "name": "country", "in": "query", "required": false, "type": "string" }, { "name": "targetSegment", "in": "query", "required": false, "type": "string" }, { "name": "Authorization", "in": "header", "description": "Authorization Bearer token", "required": true, "type": "string", "default": "Bearer " } ], "responses": { "200": { "description": "The request was successful. The availabilities are returned in the response body.", "schema": { "$ref": "#/definitions/Core.Collections.ResourceCollection[Services.Products.V2.Models.Availability]" } } } } } }, "definitions": { "Core.Collections.ResourceCollection[Services.Products.V2.Models.Availability]": { "type": "object", "properties": { "totalCount": { "format": "int32", "type": "integer", "readOnly": true } } } }, "securityDefinitions": { "Authorization": { "type": "apiKey", "description": "Authorization Bearer token", "name": "Authorization", "in": "header" } } }

Shwetap05 avatar May 10 '18 18:05 Shwetap05

I'm not sure what the best approach here is. Square brackets are not allowed in OpenAPI V3 component names. We really need to translate any invalid component names into something valid. For example we could translate any invalid character to a -. This does mean that round tripping wouldn't be exact.

darrelmiller avatar May 10 '18 19:05 darrelmiller

See #198 / #254

An alternative we have is to provide rulesets for different Vs of the document.

PerthCharern avatar May 10 '18 19:05 PerthCharern