Skip to content

Commit

Permalink
Fix broken references in OAS3 (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
aminalaee authored Nov 18, 2021
1 parent 2e9dc56 commit 23dccba
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 167 deletions.
2 changes: 1 addition & 1 deletion docs/json_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TypeSystem can convert Schema or Field instances to/from JSON Schema.
All references should be of the style `{"$ref": "#/components/schemas/..."}`.

Using hyperlinked references, relative references, or references to parts
of the document other than "definitions" is not supported.
of the document other than "components/schemas" is not supported.

Let's define a schema, and dump it out into a JSON schema document:

Expand Down
70 changes: 36 additions & 34 deletions docs/references.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,42 +53,44 @@ definitions["Album"] = album_schema
document = typesystem.to_json_schema(definitions)
print(json.dumps(document, indent=4))
# {
# "definitions": {
# "Artist": {
# "type": "object",
# "properties": {
# "name": {
# "type": "string",
# "minLength": 1,
# "maxLength": 100
# }
# },
# "required": [
# "name"
# ]
# },
# "Album": {
# "type": "object",
# "properties": {
# "title": {
# "type": "string",
# "minLength": 1,
# "maxLength": 100
# },
# "release_date": {
# "type": "string",
# "minLength": 1,
# "format": "date"
# "components":{
# "schemas":{
# "Artist":{
# "type":"object",
# "properties":{
# "name":{
# "type":"string",
# "minLength":1,
# "maxLength":100
# }
# },
# "artist": {
# "$ref": "#/definitions/Artist"
# }
# "required":[
# "name"
# ]
# },
# "required": [
# "title",
# "release_date",
# "artist"
# ]
# "Album":{
# "type":"object",
# "properties":{
# "title":{
# "type":"string",
# "minLength":1,
# "maxLength":100
# },
# "release_date":{
# "type":"string",
# "minLength":1,
# "format":"date"
# },
# "artist":{
# "$ref":"#/components/schemas/Artist"
# }
# },
# "required":[
# "title",
# "release_date",
# "artist"
# ]
# }
# }
# }
# }
Expand Down
26 changes: 19 additions & 7 deletions tests/jsonschema/draft7/definitions.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
[
{
"description": "valid definition",
"schema": {"$ref": "http://json-schema.org/draft-07/schema#"},
"schema": {
"$ref": "http://json-schema.org/draft-07/schema#"
},
"tests": [
{
"description": "valid definition schema",
"data": {
"definitions": {
"foo": {"type": "integer"}
"components": {
"schemas": {
"foo": {
"type": "integer"
}
}
}
},
"valid": true
Expand All @@ -16,17 +22,23 @@
},
{
"description": "invalid definition",
"schema": {"$ref": "http://json-schema.org/draft-07/schema#"},
"schema": {
"$ref": "http://json-schema.org/draft-07/schema#"
},
"tests": [
{
"description": "invalid definition schema",
"data": {
"definitions": {
"foo": {"type": 1}
"components": {
"schemas": {
"foo": {
"type": 1
}
}
}
},
"valid": false
}
]
}
]
]
54 changes: 39 additions & 15 deletions tests/jsonschema/draft7/definitionsRef.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
{
"description": "nested refs",
"schema": {
"definitions": {
"a": {"type": "integer"},
"b": {"$ref": "#/components/schemas/a"},
"c": {"$ref": "#/components/schemas/b"}
"components": {
"schemas": {
"a": {
"type": "integer"
},
"b": {
"$ref": "#/components/schemas/a"
},
"c": {
"$ref": "#/components/schemas/b"
}
}
},
"$ref": "#/components/schemas/c"
},
Expand All @@ -25,9 +33,11 @@
{
"description": "ref overrides any sibling keywords",
"schema": {
"definitions": {
"reffed": {
"type": "array"
"components": {
"schemas": {
"reffed": {
"type": "array"
}
}
},
"properties": {
Expand All @@ -40,17 +50,27 @@
"tests": [
{
"description": "ref valid",
"data": { "foo": [] },
"data": {
"foo": []
},
"valid": true
},
{
"description": "ref valid, maxItems ignored",
"data": { "foo": [ 1, 2, 3] },
"data": {
"foo": [
1,
2,
3
]
},
"valid": true
},
{
"description": "ref invalid",
"data": { "foo": "string" },
"data": {
"foo": "string"
},
"valid": false
}
]
Expand All @@ -59,8 +79,10 @@
"description": "$ref to boolean schema true",
"schema": {
"$ref": "#/components/schemas/bool",
"definitions": {
"bool": true
"components": {
"schemas": {
"bool": true
}
}
},
"tests": [
Expand All @@ -75,8 +97,10 @@
"description": "$ref to boolean schema false",
"schema": {
"$ref": "#/components/schemas/bool",
"definitions": {
"bool": false
"components": {
"schemas": {
"bool": false
}
}
},
"tests": [
Expand All @@ -87,4 +111,4 @@
}
]
}
]
]
Loading

0 comments on commit 23dccba

Please sign in to comment.