[Proposal / Idea] Explicit Template Metadata for Generic Schema Patterns in OpenAPI #5236
Falco94
started this conversation in
Enhancements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
TL;DR
OpenAPI currently lacks a clear way to describe generic or template-like schema structures such as
Response<T>orPage<T>.While JSON Schema dynamic references (
$dynamicRef) technically allow substitution patterns similar to generics, they do not provide explicit metadata describing template parameters or template instantiations. As a result, tooling cannot reliably detect or reconstruct generic structures.This proposal suggests introducing two optional schema keywords:
templateParametersinstantiatesThese keywords allow tooling to detect template definitions and template instantiations while remaining fully compatible with JSON Schema and existing OpenAPI tooling.
The proposal is additive, language-neutral, and could initially be introduced as vendor extensions.
Context
I have been working with OpenAPI for several years in the context of API tooling and SDK generation.
While implementing client generation workflows, I repeatedly encountered difficulties modeling generic or template-like structures such as
Response<T>orPage<T>in OpenAPI specifications.This proposal represents an attempt to describe a possible approach for modeling such structures in a way that remains compatible with existing OpenAPI and JSON Schema mechanisms.
I may be missing context from previous design discussions or decisions made by the OpenAPI TSC or JSON Schema working group. If so, I would very much appreciate pointers or corrections.
Table of Contents
$refBackground
Generic or template-based data structures are widely used in modern APIs and programming languages.
Examples include:
Response<T>Page<T>Envelope<T>Dictionary<TKey, TValue>Result<TValue, TError>These patterns appear across many languages including:
However, when APIs using these patterns are described in OpenAPI today, the generic structure is typically materialized into concrete schemas such as:
While this representation is valid and interoperable, it introduces several limitations:
For example, a type such as:
is often flattened into:
which loses structural information.
Prior Discussion
Generic wrapper structures and template-like patterns have been discussed multiple times in the OpenAPI community.
Relevant discussions include:
#3601
Recommend describing templates / generic types using
$dynamicRef#957
Representing Java generics in Swagger/OpenAPI
#555
Envelope / wrapper schema patterns
Since OpenAPI 3.1 aligned with JSON Schema 2020-12, the specification includes support for dynamic references:
$dynamicRef$dynamicAnchorThese allow substitution patterns similar to generics.
A detailed explanation is available here:
https://json-schema.org/blog/posts/dynamicref-and-generics/
However,
$dynamicRefonly enables schema substitution, not explicit template semantics.It does not describe:
Goal
The goal of this proposal is not to replace
$dynamicRef, but to introduce explicit metadata that allows tools to detect template structures.Design goals:
Real-World Example
Consider a simple Book API.
Endpoints:
Typical return types:
Current OpenAPI Representation
Today these might be described as:
Although valid, the structural relationship between these schemas is not machine-detectable.
Proposed Template Metadata
Introduce two schema properties:
These describe template definitions and template instantiations.
Template Definition
Example template schema:
Semantically equivalent to:
Another template:
Equivalent to:
Template Instantiation
Concrete schemas bind template parameters.
Template Instantiation Algorithm
Resolve the referenced template schema.
Validate that all template parameters are bound exactly once.
Traverse the template schema recursively.
For each
$dynamicRefreferencing a template parameter:replace the reference with the corresponding argument schema.
Replacement MUST occur for all occurrences of the parameter.
Traversal includes:
The result MUST be a fully materialized schema.
Allowed Argument Types
Template arguments MUST be either:
Example:
or:
Nested Templates
Template arguments MAY themselves be template instantiations.
Example:
Recursive Instantiations
Recursive instantiations MAY occur.
Example:
Such recursion follows existing JSON Schema recursion rules.
Interaction with
$refA schema MUST NOT contain both:
because
$refreplaces the entire schema.JSON Schema Compatibility
JSON Schema allows unknown keywords unless restricted by a vocabulary.
Therefore the following keywords can safely be ignored by validators:
Tools that do not recognize these keywords can still process the schema.
Substitution Mechanism
This proposal does not mandate
$dynamicRef.It only introduces metadata describing template structure.
Implementations MAY rely on:
$dynamicRef$refwhen materializing templates.
Compatibility Strategy
For backwards compatibility, template instantiations SHOULD resolve to a fully materialized schema.
Tools that do not support template metadata MAY:
Possible Evolution Path
A practical adoption path could be:
Non-Goals
This proposal intentionally does not attempt to model:
The goal is only to represent template structure in a language-neutral way.
Prior Art
Generic structures are common in many ecosystems:
Conclusion
OpenAPI already provides the technical mechanism for schema substitution via JSON Schema dynamic references.
Introducing explicit template metadata would allow tooling to:
while remaining fully compatible with existing tooling and specifications.
Beta Was this translation helpful? Give feedback.
All reactions