openapi-spec-validator
openapi-spec-validator copied to clipboard
OAS30 object schema fails with $defs
I have an OAS3 file that uses $ref
for external JSON schemas. The schemas themselves have a $defs
section for child objects definitions that are reused. These schemas pass the schema validators for openapi_schema_validator.OAS30Validator
and jsonschema's Draft 4, 6, 7, and 2020-12, but when referenced by an OAS3 spec they cause openapi-spec-validator
to raise exceptions. This goes away if I remove the $defs
section. Below is a simplified test case that shows this symptom.
conda list | grep openapi
openapi-schema-validator 0.2.3 pyhd8ed1ab_0 conda-forge
openapi-spec-validator 0.4.0 pyhd8ed1ab_0 conda-forge
test_schema.py
def test_validator():
from openapi_spec_validator import validate_v3_spec
from openapi_spec_validator.readers import read_from_filename
spec_dict, spec_url = read_from_filename("openapi-test.yml")
# If no exception is raised by validate_spec(), the spec is valid.
validate_v3_spec(spec_dict, spec_url)
### EXCEPTION ###
def test_schema():
import json
from openapi_schema_validator import validate
with open("schema.json") as f:
json_schema = json.load(f)
validate({"name1": "John", "name2": "Jane"}, json_schema)
### No errors ###
openapi-test.yml
openapi: "3.0.2"
info:
title: API Title
version: "1.0"
servers:
- url: https://api.server.test/v1
paths:
/test:
post:
requestBody:
required: true
content:
application/json:
schema:
#type: string # this schema passes
$ref: "schema.json" # this schema fails
responses:
"200":
description: OK
schema.json
{
"type": "object",
"properties": {
"name1": {
"$ref": "#/$defs/name"
},
"name2": {
"$ref": "#/$defs/name"
}
},
"$defs": {
"name": {
"type": "string"
}
}
}
Here is the full error list (file paths are partially redacted):
openapi-spec-validator --errors=all openapi-test.yml
# Validation Error
{'required': True, 'content': {'application/json': {'schema': {'$ref': 'schema.json', 'x-scope': ['file:///Users/.../openapi-test.yml']}}}} is not valid under any of the given schemas
Failed validating 'oneOf' in schema['properties']['paths']['patternProperties']['^\\/']['patternProperties']['^(get|put|post|delete|options|head|patch|trace)$']['properties']['requestBody']:
{'oneOf': [{'$ref': '#/definitions/RequestBody'},
{'$ref': '#/definitions/Reference'}]}
On instance['paths']['/test']['post']['requestBody']:
{'content': {'application/json': {'schema': {'$ref': 'schema.json',
'x-scope': ['file:///Users/.../openapi-test.yml']}}},
'required': True}
# Due to one of those errors
## {'type': 'object', 'properties': {'name1': {'$ref': '#/$defs/name', 'x-scope': ['file:///Users/.../openapi-test.yml', 'file:///Users/.../schema.json']}, 'name2': {'$ref': '#/$defs/name', 'x-scope': ['file:///Users/.../openapi-test.yml', 'file:///Users/.../schema.json']}}, '$defs': {'name': {'type': 'string'}}} is not valid under any of the given schemas
Failed validating 'oneOf' in schema[0]['properties']['content']['additionalProperties']['properties']['schema']:
{'oneOf': [{'$ref': '#/definitions/Schema'},
{'$ref': '#/definitions/Reference'}]}
On instance['content']['application/json']['schema']:
{'$ref': 'schema.json',
'x-scope': ['file:///Users/.../openapi-test.yml']}
## '$ref' is a required property
Failed validating 'required' in schema[1]:
{'patternProperties': {'^\\$ref$': {'format': 'uri-reference',
'type': 'string'}},
'required': ['$ref'],
'type': 'object'}
On instance:
{'content': {'application/json': {'schema': {'$ref': 'schema/schema.json',
'x-scope': ['file:///Users/.../openapi-test.yml']}}},
'required': True}
Having the same problem.
It works well with
openapi-schema-validator 0.6.2
openapi-spec-validator 0.7.0
hence closing.