Skip to content

Commit ff21dee

Browse files
handrewsralfhandl
andcommitted
Explain that dynamic refs can implement generics
Includes an example. This intentionally does not explain how dynamic referencing works, as there are better resources available in both the spec and (more readably) the official JSON Schema blog. Thanks to Ralf for review feedback fixes on the original 3.2.0 PR Co-authored-by: Ralf Handl <[email protected]>
1 parent 4365723 commit ff21dee

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

versions/3.2.0.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,15 @@ There are two ways to define the value of a discriminator for an inheriting inst
23982398
- Override the schema name by overriding the property with a new value. If a new value exists, this takes precedence over the schema name.
23992399
As such, inline schema definitions, which do not have a given id, *cannot* be used in polymorphism.
24002400

2401+
###### Generic (Template) Data Structures
2402+
2403+
Implementations MAY support defining generic or template data structures using JSON Schema's dynamic referencing feature:
2404+
2405+
* `$dynamicAnchor` identifies a set of possible schemas (including a default placeholder schema) to which a `$dynamicRef` can resolve
2406+
* `$dynamicRef` resolves to the first matching `$dynamicAnchor` encountered on its path from the schema entry point to the reference, as described in the JSON Schema specification
2407+
2408+
An example is included in the "Schema Object Examples" section below, and further information can be found on the Learn OpenAPI site's ["Dynamic References"](https://learn.openapis.org/referencing/dynamic.html) page.
2409+
24012410
###### XML Modeling
24022411

24032412
The [xml](#schemaXml) property allows extra definitions when translating the JSON definition to XML.
@@ -2741,6 +2750,119 @@ components:
27412750
- packSize
27422751
```
27432752
2753+
###### Generic Data Structure Model
2754+
2755+
```JSON
2756+
{
2757+
"components": {
2758+
"schemas": {
2759+
"genericArrayComponent": {
2760+
"$id": "fully_generic_array",
2761+
"type": "array",
2762+
"items": {
2763+
"$dynamicRef": "#generic-array"
2764+
},
2765+
"$defs": {
2766+
"allowAll": {
2767+
"$dynamicAnchor": "generic-array"
2768+
}
2769+
}
2770+
},
2771+
"numberArray": {
2772+
"$id": "array_of_numbers",
2773+
"$ref": "fully_generic_array",
2774+
"$defs": {
2775+
"numbersOnly": {
2776+
"$dynamicAnchor": "generic-array",
2777+
"type": "number"
2778+
}
2779+
}
2780+
},
2781+
"stringArray": {
2782+
"$id": "array_of_strings",
2783+
"$ref": "fully_generic_array",
2784+
"$defs": {
2785+
"stringsOnly": {
2786+
"$dynamicAnchor": "generic-array",
2787+
"type": "string"
2788+
}
2789+
}
2790+
},
2791+
"objWithTypedArray": {
2792+
"$id": "obj_with_typed_array",
2793+
"type": "object",
2794+
"required": ["dataType", "data"],
2795+
"properties": {
2796+
"dataType": {
2797+
"enum": ["string", "number"]
2798+
}
2799+
},
2800+
"oneOf": [{
2801+
"properties": {
2802+
"dataType": {"const": "string"},
2803+
"data": {"$ref": "array_of_strings"}
2804+
}
2805+
}, {
2806+
"properties": {
2807+
"dataType": {"const": "number"},
2808+
"data": {"$ref": "array_of_numbers"}
2809+
}
2810+
}]
2811+
}
2812+
}
2813+
}
2814+
}
2815+
```
2816+
2817+
```YAML
2818+
components:
2819+
schemas:
2820+
genericArrayComponent:
2821+
$id: fully_generic_array
2822+
type: array
2823+
items:
2824+
$dynamicRef: "#generic-array"
2825+
$defs:
2826+
allowAll:
2827+
$dynamicAnchor: generic-array
2828+
numberArray:
2829+
$id: array_of_numbers
2830+
$ref: fully_generic_array
2831+
$defs:
2832+
numbersOnly:
2833+
$dynamicAnchor: generic-array
2834+
type: number
2835+
stringArray:
2836+
$id: array_of_strings
2837+
$ref: fully_generic_array
2838+
$defs:
2839+
stringsOnly:
2840+
$dynamicAnchor: generic-array
2841+
type: string
2842+
objWithTypedArray:
2843+
$id: obj_with_typed_array
2844+
type: object
2845+
required:
2846+
- dataType
2847+
- data
2848+
properties:
2849+
dataType:
2850+
enum:
2851+
- string
2852+
- number
2853+
oneOf:
2854+
- properties:
2855+
dataType:
2856+
const: string
2857+
data:
2858+
$ref: array_of_strings
2859+
- properties:
2860+
dataType:
2861+
const: number
2862+
data:
2863+
$ref: array_of_numbers
2864+
```
2865+
27442866
#### <a name="discriminatorObject"></a>Discriminator Object
27452867
27462868
When request bodies or response payloads may be one of a number of different schemas, a `discriminator` object can be used to aid in serialization, deserialization, and validation. The discriminator is a specific object in a schema which is used to inform the consumer of the document of an alternative schema based on the value associated with it.

0 commit comments

Comments
 (0)