opyeb openapilab

openapilab · References

Resolving $ref and living with circular references

How $ref uses JSON Pointer, what external files cannot do, and how self-referencing schemas are expanded safely.

A $ref is a string that points at another place in the same document. #/components/schemas/Book means: from the document root go to components, then schemas, then Book. That path notation is JSON Pointer. ~1 stands for / and ~0 for ~, so names containing slashes can still be addressed.

The order of resolution

Why cycles appear

An employee whose manager is also an employee, a comment with replies that are comments, an organisation chart of tree nodes: any schema that refers to its own type creates a cycle. That is normal modelling, not a mistake, but expanding it without a limit freezes screens and memory.

This tool builds a reference graph between component schemas, looks for a path back to the same schema, and marks each such schema with a note. Tree expansion stops where the same reference appears twice or the depth limit is reached, and says so.

The same guard in mock generation

The mock generator stops at the same point and reduces an object to {} and an array to [], so the JSON stays finite. A schema with a cycle still produces a preview, and the result never grows without bound.

External references such as common.yaml#/components/schemas/Error are not resolved, because the browser cannot read another file. Merge the files and turn them into internal references to check them.

Questions

Does a cycle mean the spec is wrong?

No. Self-reference is a normal schema pattern. The document's readers merely need a depth limit so they do not loop forever.

May I put a description beside $ref?

3.1 evaluates it, but 3.0 ignores keys next to $ref. This tool warns when a 3.0 document has sibling keys.

Can reference names use non-ASCII characters?

They can, but tools may handle them differently. This tool recommends A-Z a-z 0-9 . _ - and warns about other characters in component names.

Load the circular $ref preset to see where expansion stops for Employee and ChartNode.

Open the tool

Written against the OpenAPI Specification 3.1.0 (2021-02), 3.0.3 and JSON Schema 2020-12. Every example spec and host is synthetic.