External References ($ref)
Referencing Schemas
The $ref keyword lets you reuse definitions instead of duplicating them. Reference schemas within the same file, from external files, or from URLs.
Key Concepts
- • Internal refs - Point to definitions in the same file
- • External file refs - Point to separate YAML/JSON files
- • URL refs - Point to hosted specifications
- • JSON Pointer - The path syntax after the #
Internal References
Most CommonReference definitions within the same OpenAPI document using JSON Pointer notation.
Standard Paths
Complete Example
External File References
ModularReference schemas from separate files. Useful for organizing large specifications or sharing common schemas across multiple APIs.
Relative Path
File Structure Example
Main Spec with External Refs
URL References
RemoteReference schemas from remote URLs. Useful for shared organizational schemas or referencing third-party type definitions.
Examples
Considerations
- • URL must be accessible (CORS enabled for browser-based tools)
- • Consider caching - remote changes affect your spec
- • For stability, pin to specific versions or commits
JSON Pointer Syntax
The path after # uses JSON Pointer (RFC 6901) syntax to navigate the document structure.
Syntax Rules
- /Separates path segments
- ~0Escaped tilde (~)
- ~1Escaped forward slash (/)
- 0, 1, 2...Array index (zero-based)
Examples
Referenceable Components
OpenAPI defines specific locations where $ref can be used:
| Component Type | Path | Use For |
|---|---|---|
| Schemas | #/components/schemas/... | Data models |
| Responses | #/components/responses/... | Common responses (errors) |
| Parameters | #/components/parameters/... | Query/path parameters |
| Request Bodies | #/components/requestBodies/... | Shared request payloads |
| Headers | #/components/headers/... | Response headers |
| Security Schemes | #/components/securitySchemes/... | Auth definitions |
| Links | #/components/links/... | Response links |
| Callbacks | #/components/callbacks/... | Async callbacks |
Circular References
Handle With CareSchemas can reference themselves (self-referential) or form cycles. This is valid but requires tools to handle recursion properly.
Self-Reference (Tree Node)
Mutual Reference
Tigrister Handling
Tigrister properly resolves circular references for validation and display. The Preview and Visual Designer show the structure without infinite loops.
Tigrister Resolution Features
Tigrister includes a full-featured $ref resolver with the following capabilities:
Resolution Behavior
- •Max depth: 10 - Nested refs are resolved up to 10 levels deep
- •Caching - External documents are cached to prevent duplicate fetches
- •Circular detection - Circular refs return empty schema to prevent infinite loops
- •Auto-detect - YAML and JSON are automatically detected and parsed
Error Handling
- •Graceful fallback - On error, original $ref is preserved
- •Error collection - All resolution errors are collected and reported
- •Nested refs - Refs inside external docs are recursively resolved
- •Network errors - HTTP errors are caught with descriptive messages
Best Practices
Use Descriptive Names
Name schemas clearly: UserResponse vs Userwhen they differ.
Prefer Internal Refs
External refs add complexity. Use them for truly shared schemas, not convenience.
Bundle for Distribution
When sharing your spec, resolve external refs into a single file for reliability.
Avoid Deep Nesting
Keep reference chains shallow. Deep nesting makes specs hard to understand.