Skip to content

Commit a602c46

Browse files
authored
Merge pull request #3714 from handrews/generics
Explain that dynamic refs can implement generics
2 parents 8c6c0dd + ff9bbdb commit a602c46

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

versions/3.1.1.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,15 @@ While composition offers model extensibility, it does not imply a hierarchy betw
24052405
To support polymorphism, the OpenAPI Specification adds the `discriminator` keyword.
24062406
When used, the `discriminator` indicates the name of the property that hints which schema definition is expected to validate the structure of the model.
24072407

2408+
###### Generic (Template) Data Structures
2409+
2410+
Implementations MAY support defining generic or template data structures using JSON Schema's dynamic referencing feature:
2411+
2412+
* `$dynamicAnchor` identifies a set of possible schemas (including a default placeholder schema) to which a `$dynamicRef` can resolve
2413+
* `$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
2414+
2415+
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.
2416+
24082417
###### XML Modeling
24092418

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

0 commit comments

Comments
 (0)