-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Description
JSON Schema does not define a concept of $ref usage in data values, neither it provides a way to ensure if resolved data value is valid. Nevertheless using $ref in data is popular and is used in OpenAPI.
If it is not possible to validate the resolved value, we can at least have best effort validation that reference leads to a correct place.
OpenAPI spec since v3 has a pre-defined place to store some kinds of referenced values: components.
For example responses are defined as follows:
responses:
type: object
patternProperties:
'^[a-zA-Z0-9\.\-_]+$':
oneOf:
- $ref: '#/definitions/Reference'
- $ref: '#/definitions/Response'The problem here is that current schema allows $ref to lead to any place, not only to #/components/responses/....
Semantically incorrect (but valid) schema with confused references:
paths:
/pets:
get:
parameters:
- $ref: "#/components/responses/UnexpectedError"We can make it harder to go wrong by restricting local references of schema components, for example ResponseReference:
ResponseReference:
type: object
required:
- $ref
patternProperties:
'^\$ref$':
type: string
format: uri-reference
oneOf:
- pattern: '^#/components/responses/'
- not:
pattern: '^#/'Such restriction (pattern: '#/components/responses/') could also be employed for external references, though it may have too much of negative impact on flexibility.