Skip to content

Commit 0148373

Browse files
authored
Merge pull request #3795 from handrews/generics-320
Explain dynamic refs for generics (3.2.0 port of #3714)
2 parents 1496e07 + ff21dee commit 0148373

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
@@ -2420,6 +2420,15 @@ There are two ways to define the value of a discriminator for an inheriting inst
24202420
- Override the schema name by overriding the property with a new value. If a new value exists, this takes precedence over the schema name.
24212421
As such, inline schema definitions, which do not have a given id, *cannot* be used in polymorphism.
24222422

2423+
###### Generic (Template) Data Structures
2424+
2425+
Implementations MAY support defining generic or template data structures using JSON Schema's dynamic referencing feature:
2426+
2427+
* `$dynamicAnchor` identifies a set of possible schemas (including a default placeholder schema) to which a `$dynamicRef` can resolve
2428+
* `$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
2429+
2430+
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.
2431+
24232432
###### XML Modeling
24242433

24252434
The [xml](#schemaXml) property allows extra definitions when translating the JSON definition to XML.
@@ -2763,6 +2772,119 @@ components:
27632772
- packSize
27642773
```
27652774
2775+
###### Generic Data Structure Model
2776+
2777+
```JSON
2778+
{
2779+
"components": {
2780+
"schemas": {
2781+
"genericArrayComponent": {
2782+
"$id": "fully_generic_array",
2783+
"type": "array",
2784+
"items": {
2785+
"$dynamicRef": "#generic-array"
2786+
},
2787+
"$defs": {
2788+
"allowAll": {
2789+
"$dynamicAnchor": "generic-array"
2790+
}
2791+
}
2792+
},
2793+
"numberArray": {
2794+
"$id": "array_of_numbers",
2795+
"$ref": "fully_generic_array",
2796+
"$defs": {
2797+
"numbersOnly": {
2798+
"$dynamicAnchor": "generic-array",
2799+
"type": "number"
2800+
}
2801+
}
2802+
},
2803+
"stringArray": {
2804+
"$id": "array_of_strings",
2805+
"$ref": "fully_generic_array",
2806+
"$defs": {
2807+
"stringsOnly": {
2808+
"$dynamicAnchor": "generic-array",
2809+
"type": "string"
2810+
}
2811+
}
2812+
},
2813+
"objWithTypedArray": {
2814+
"$id": "obj_with_typed_array",
2815+
"type": "object",
2816+
"required": ["dataType", "data"],
2817+
"properties": {
2818+
"dataType": {
2819+
"enum": ["string", "number"]
2820+
}
2821+
},
2822+
"oneOf": [{
2823+
"properties": {
2824+
"dataType": {"const": "string"},
2825+
"data": {"$ref": "array_of_strings"}
2826+
}
2827+
}, {
2828+
"properties": {
2829+
"dataType": {"const": "number"},
2830+
"data": {"$ref": "array_of_numbers"}
2831+
}
2832+
}]
2833+
}
2834+
}
2835+
}
2836+
}
2837+
```
2838+
2839+
```YAML
2840+
components:
2841+
schemas:
2842+
genericArrayComponent:
2843+
$id: fully_generic_array
2844+
type: array
2845+
items:
2846+
$dynamicRef: "#generic-array"
2847+
$defs:
2848+
allowAll:
2849+
$dynamicAnchor: generic-array
2850+
numberArray:
2851+
$id: array_of_numbers
2852+
$ref: fully_generic_array
2853+
$defs:
2854+
numbersOnly:
2855+
$dynamicAnchor: generic-array
2856+
type: number
2857+
stringArray:
2858+
$id: array_of_strings
2859+
$ref: fully_generic_array
2860+
$defs:
2861+
stringsOnly:
2862+
$dynamicAnchor: generic-array
2863+
type: string
2864+
objWithTypedArray:
2865+
$id: obj_with_typed_array
2866+
type: object
2867+
required:
2868+
- dataType
2869+
- data
2870+
properties:
2871+
dataType:
2872+
enum:
2873+
- string
2874+
- number
2875+
oneOf:
2876+
- properties:
2877+
dataType:
2878+
const: string
2879+
data:
2880+
$ref: array_of_strings
2881+
- properties:
2882+
dataType:
2883+
const: number
2884+
data:
2885+
$ref: array_of_numbers
2886+
```
2887+
27662888
#### <a name="discriminatorObject"></a>Discriminator Object
27672889
27682890
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)