Commit ee052fd
validate value of 'const' properties (helps with discriminated unions) (#1024)
For 'const' properties, the parsed value will now be checked against the
value in the schema at runtime and a ValueError will be raised if they
do not match. Until now, the values would just be `cast` to a `Literal`
without checking.
This helps with with parsing `oneOf` when matching a const property is
the only way to resolve which of the types should be returned. i.e. if a
`oneOf` contains two schemas, but the properties in one are a subset of
the other, then the only way to know which one to return is to validate
the `const` value.
I know that for the most part this library parses but doesn't validate,
but I don't see a way around it in this case. The spec itself mentions
validation when talking about `oneOf`:
```
In OAS 3.0, a response payload MAY be described to be exactly one of any number of types:
[...]
which means the payload MUST, by validation, match exactly one of the schemas described by Cat, Dog, or Lizard.
```
Note that this also addresses some of the use cases for discriminated
unions. Per the spec, the `a discriminator MAY act as a "hint" to
shortcut validation and selection of the matching schema which may be a
costly operation, depending on the complexity of the schema.` so
implementing discriminators would be a speedup but with this change
we'll at least construct the correct type.
I'm happy to take any feedback, or abandon this if you don't like fact
that it is validation. I originally went to try to implement
discriminated unions fully, but this seemed like a much easier step
towards that that gets the same result.
---------
Co-authored-by: Dylan Anthony <dbanty@users.noreply.github.com>1 parent 5d138fd commit ee052fd
File tree
5 files changed
+34
-4
lines changed- .changeset
- end_to_end_tests/test-3-1-golden-record/test_3_1_features_client
- api/const
- models
- openapi_python_client
- parser/properties
- templates/property_templates
5 files changed
+34
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
49 | 53 | | |
50 | 54 | | |
51 | 55 | | |
| |||
Lines changed: 12 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
54 | 62 | | |
55 | 63 | | |
56 | 64 | | |
57 | 65 | | |
58 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
59 | 69 | | |
60 | 70 | | |
61 | 71 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
0 commit comments