Skip to content

Commit e456a98

Browse files
committed
Added a more complete explanation of the problems created by disallowing nulls by default.
1 parent 7d94b7c commit e456a98

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

proposals/003_Clarify-Nullable.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,35 @@ The documentation specifies that `nullable: false` is the default, but doesn't c
7373

7474
One reasonable interpretation suggests that null values are disallowed unless `nullable` is explicitly set to `true`. This breaks a fundamental rule of JSON Schema, which states that an empty object `{}` is a valid schema that permits all values, with no constraints. Breaking that rule takes OpenAPI's Schema Object even further out of alignment with JSON Schema's processing model.
7575

76-
However, the OpenAPI 3.0 specification doesn't explicitly say that untyped schemas disallow null values.
76+
For example, if null values are disallowed by default, does the following `UTCDate` schema accept `null`?
77+
78+
```yaml
79+
components:
80+
81+
schemas:
82+
83+
OptionalDate:
84+
type: string
85+
format: date
86+
nullable: true
87+
88+
UTCDate:
89+
allOf:
90+
$ref: "#/components/schemas/OptionalDate"
91+
not:
92+
type: string
93+
pattern: "^.*Z.*$"
94+
```
95+
96+
`UTCDate` does not specify a type of its own, and does not directly specify `nullable: true`. So if `null` is disallowed by default, even for untyped schemas, then `UTCDate` won't accept nulls. If we want it to accept nulls, we have to repeat `nullable: true` in `UTCDate`. This is not at all intuitive for API designers, and it breaks with JSON Schema's rule that any value is allowed unless it's explicitly disallowed.
97+
98+
On the other hand, we could say that `UTCDate` inherits `nullable: true` from `OptionalDate`, therefore null values are allowed. But this kind of inheritance logic is completely foreign to JSON Schema. So this behavior is also counterintuitive, though for a different reason. It's also difficult to implement. Any JSON Schema validator would need to be hacked in highly disruptive ways to retrofit this behavior. Or a preprocessor would have to be introduced to propagate the effect of `nullable: true` through the `*Of` inheritance hierarchy.
99+
100+
Whichever semantics we choose, it gets very messy.
101+
102+
### A closer look at `nullable: false`
103+
104+
In fact, the OpenAPI 3.0 specification doesn't explicitly say that untyped schemas disallow null values.
77105

78106
Here are the relevant parts:
79107

0 commit comments

Comments
 (0)