question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Circular references in Definitions Object

See original GitHub issue

Syntactically, 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:closed
  • Created 7 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

17reactions
tedepsteincommented, May 7, 2018

Just read through this, and I think a few points are worth adding, or reiterating:

  • Circular references in the schemas do not necessarily imply circular references in the data. It’s possible for Person to have properties like spouse, children, or parents that also refer to Person, but will never realistically form a cycle in an actual message.
  • Where two or more references form a cycle, making each of those references 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 in pattern 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.
  • What’s being called “ambiguity” in the OpenAPI and/or JSON Schema specification is not really ambiguity. It’s a partitioning of responsibilities, leaving it up to the user to ensure that schemas are satisfiable and logically consistent. Of course, tools can try to detect and flag logical inconsistencies. The specification doesn’t require tools to do this, but it doesn’t preclude it.
3reactions
loganfreemancommented, Feb 20, 2019

It is sad that circular reference could result in Java heap space error.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found