Circular references in Definitions Object
See original GitHub issueSyntactically, it seems possible to construct an infinite circular reference using Definitions Objects.
Obviously, such a reference could never actually be called or returned. I can’t find any conclusive answer on this in the specs. The editor at http://editor.swagger.io/ seems to accept it as valid syntax, but produces an undefined
Schema.
How should such a situation be treated?
Example
{
"swagger": "2.0",
"info": {
"title": "vanderlee/PHPSwaggerGen",
"version": "2.3.7"
},
"host": "example.com",
"basePath": "\/base",
"paths": {
"\/customers": {
"get": {
"tags": [
"Test"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#\/definitions\/Person"
}
}
}
}
}
},
"definitions": {
"Address": {
"type": "object",
"required": [
"city",
"occupant"
],
"properties": {
"city": {
"type": "string"
},
"occupant": {
"$ref": "#\/definitions\/Person"
}
}
},
"Person": {
"type": "object",
"required": [
"name",
"home"
],
"properties": {
"name": {
"type": "string"
},
"home": {
"$ref": "#\/definitions\/Address"
}
}
}
},
"tags": [
{
"name": "Test"
}
]
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:17 (9 by maintainers)
Top Results From Across the Web
Circular reference - Wikipedia
A circular reference is a series of references where the last object references the first, resulting in a closed loop. Circular reference (in...
Read more >What is a Circular Reference? - Definition from Techopedia
A circular reference in computer programming is a situation in which two values reference each other. This type of reference can be extremely...
Read more >What is Circular Reference in Python? - Pencil Programmer
When two objects in Python holds reference of each other, it is termed as cyclic or circular reference. This occurs when object A's...
Read more >code quality - What's wrong with circular references?
Circular object references can crash naïve recursive algorithms (such as serializers, visitors and pretty-printers) with stack overflows.
Read more >How to handle circular references between related objects ...
To get an idea of how to structure your code you could think in terms of composition, aggregation, association.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just read through this, and I think a few points are worth adding, or reiterating:
Person
to have properties likespouse
,children
, orparents
that also refer toPerson
, but will never realistically form a cycle in an actual message.required
does indeed make the schema unsatisfiable. There is no finite message that could possibly conform to the schema. This is just one of many ways to create an unsatisfiable schema. Some of these conditions are potentially easy to detect, but others (like conflicting regular expressions inpattern
constraints) are a lot harder to prove satisfiable or not. I suspect this is why JSON Schema (and therefore OpenAPI) decided to leave it up to users.It is sad that circular reference could result in Java heap space error.