Skip to content

Commit 4e9c5d6

Browse files
authored
Merge pull request #4111 from handrews/extended
Annotated enums and extended validation
2 parents 2a94b47 + d269e39 commit 4e9c5d6

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

scripts/format-markdown.sh

100644100755
File mode changed.

versions/3.1.1.md

+63-3
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,31 @@ JSON Schema implementations MAY choose to treat keywords defined by the OpenAPI
27582758

27592759
This object MAY be extended with [Specification Extensions](#specification-extensions), though as noted, additional properties MAY omit the `x-` prefix within this object.
27602760

2761+
##### Extended Validation with Annotations
2762+
2763+
JSON Schema Draft 2020-12 supports [collecting annotations](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-7.7.1), including [treating unrecognized keywords as annotations](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-6.5).
2764+
OAS implementations MAY use such annotations, including [extensions](https://spec.openapis.org/registry/extension/) not recognized as part of a declared JSON Schema vocabulary, as the basis for further validation.
2765+
Note that JSON Schema Draft 2020-12 does not require an `x-` prefix for extensions.
2766+
2767+
###### Non-validating constraint keywords
2768+
2769+
The [`format` keyword (when using default format-annotation vocabulary)](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-7.2.1) and the [`contentMediaType`, `contentEncoding`, and `contentSchema` keywords](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-8.2) define constraints on the data, but are treated as annotations instead of being validated directly.
2770+
Extended validation is one way that these constraints MAY be enforced.
2771+
2772+
###### Validating `readOnly` and `writeOnly`
2773+
2774+
The `readOnly` and `writeOnly` keywords are annotations, as JSON Schema is not aware of how the data it is validating is being used.
2775+
Validation of these keywords MAY be done by checking the annotation, the read or write direction, and (if relevant) the current value of the field.
2776+
[JSON Schema Validation Draft 2020-12 §9.4](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-9.4) defines the expectations of these keywords, including that a resource (described as the "owning authority") MAY either ignore a `readOnly` field or treat it as an error.
2777+
2778+
Fields that are both required and read-only are an example of when it is beneficial to ignore a `readOnly: true` constraint in a PUT, particularly if the value has not been changed.
2779+
This allows correctly requiring the field on a GET and still using the same representation and schema with PUT.
2780+
Even when read-only fields are not required, stripping them is burdensome for clients, particularly when the JSON data is complex or deeply nested.
2781+
2782+
Note that the behavior of `readOnly` in particular differs from that specified by version 3.0 of this specification.
2783+
2784+
##### Data Modeling Techniques
2785+
27612786
###### Composition and Inheritance (Polymorphism)
27622787

27632788
The OpenAPI Specification allows combining and extending model definitions using the `allOf` keyword of JSON Schema, in effect offering model composition.
@@ -2781,12 +2806,18 @@ Implementations MAY support defining generic or template data structures using J
27812806

27822807
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.
27832808

2809+
###### Annotated Enumerations
2810+
2811+
The Schema Object's `enum` keyword does not allow associating descriptions or other information with individual values.
2812+
2813+
Implementations MAY support recognizing a `oneOf` or `anyOf` where each subschema in the keyword's array consists of a `const` keyword and annotations such as `title` or `description` as an enumerated type with additional information. The exact behavior of this pattern beyond what is required by JSON Schema is implementation-defined.
2814+
27842815
###### XML Modeling
27852816

27862817
The [xml](#schema-xml) field allows extra definitions when translating the JSON definition to XML.
27872818
The [XML Object](#xml-object) contains additional information about the available options.
27882819

2789-
###### Specifying Schema Dialects
2820+
##### Specifying Schema Dialects
27902821

27912822
It is important for tooling to be able to determine which dialect or meta-schema any given resource wishes to be processed with: JSON Schema Core, JSON Schema Validation, OpenAPI Schema dialect, or some custom meta-schema.
27922823

@@ -2886,6 +2917,35 @@ additionalProperties:
28862917
$ref: '#/components/schemas/ComplexModel'
28872918
```
28882919

2920+
###### Model with Annotated Enumeration
2921+
2922+
```json
2923+
{
2924+
"oneOf": [
2925+
{
2926+
"const": "RGB",
2927+
"title": "Red, Green, Blue",
2928+
"description": "Specify colors with the red, green, and blue additive color model"
2929+
},
2930+
{
2931+
"const": "CMYK",
2932+
"title": "Cyan, Magenta, Yellow, Black",
2933+
"description": "Specify colors with the cyan, magenta, yellow, and black subtractive color model"
2934+
}
2935+
]
2936+
}
2937+
```
2938+
2939+
```yaml
2940+
oneOf:
2941+
- const: RGB
2942+
title: Red, Green, Blue
2943+
description: Specify colors with the red, green, and blue additive color model
2944+
- const: CMYK
2945+
title: Cyan, Magenta, Yellow, Black
2946+
description: Specify colors with the cyan, magenta, yellow, and black subtractive color model
2947+
```
2948+
28892949
###### Model with Example
28902950

28912951
```json
@@ -4465,7 +4525,7 @@ This keeps it outside of the processes governed by this specification.
44654525

44664526
## Appendix F: Resolving Security Requirements in a Referenced Document
44674527

4468-
This appendix shows how to retrieve an HTTP-accessible multi-document OpenAPI Description (OAD) and resolve a [Security Requirement Object](#security-requirement-object) in the referenced (non-entry) document. See [Resolving Implicit Connections](#resolving-implicit-connections) for more information.
4528+
This appendix shows how to retrieve an HTTP-accessible multi-document OpenAPI Description (OAD) and resolve a [Security Requirement Object](#security-requirement-object) in the referenced (non-entry) document. See [Resolving Implicit Connections](#resolving-implicit-connections) for more information.
44694529

44704530
First, the [entry document](#openapi-description-structure) is where parsing begins. It defines the `MySecurity` security scheme to be JWT-based, and it defines a Path Item as a reference to a component in another document:
44714531

@@ -4510,7 +4570,7 @@ paths:
45104570
$ref: 'other#/components/pathItems/Foo'
45114571
```
45124572
4513-
This entry document references another document, `other`, without using a file extension. This gives the client the flexibility to choose an acceptable format on a resource-by-resource basis, assuming both representations are available:
4573+
This entry document references another document, `other`, without using a file extension. This gives the client the flexibility to choose an acceptable format on a resource-by-resource basis, assuming both representations are available:
45144574

45154575
```HTTP
45164576
GET /api/description/other HTTP/1.1

0 commit comments

Comments
 (0)