Commit 6746289
committed
feat(osgen)!: decode unions by the spec's discriminator, and name them after their schema
osgen never read the OpenAPI `discriminator`. Eight schemas in the spec declare
one, and every branch of all eight resolves to a distinct value, so 146 subtypes
that the payload names outright were instead decoded by guesswork: a JSON
token-class heuristic, or for the ambiguous ones no decoding at all until a
caller picked a branch by hand.
The discriminator is how OpenAPI expresses polymorphism, and the spec encodes
the hierarchy through allOf: a subtype is `allOf[Base, {type: enum[keyword]}]`,
so resolving a discriminant means walking allOf transitively -- the constant
sits on the narrowing member while the inherited fields come from the base.
discriminatorValues does that, and refuses the whole union unless every branch
resolves distinctly, so a partially-understood schema falls back rather than
emitting a decoder with an unreachable case.
Six emitted unions gain a real Type(), discriminant consts, and an UnmarshalJSON
that reads one property and decodes exactly that branch: CommonMappingProperty
(on `type`, with the spec's x-default naming the implicit object mapping),
CommonAnalysis{Analyzer,CharFilterDefinition,TokenFilterDefinition,
TokenizerDefinition,Normalizer}, plus ClusterRemoteInfoCluster on `mode`. A
value naming no branch is an error rather than a silent mis-decode.
MovingAverageAggregation also declares one but is request-body-only and never
emitted.
A $ref'd union was keyed by the caller's field path, so a shared schema was
re-emitted once per referencing field and its identity -- along with its
discriminator -- was discarded: _common.analysis___Analyzer became
IndicesIndexSettingsAnalysisAnalyzerValue, named after the single field that
happened to reach it. A $ref'd schema now keeps its own name and is emitted
once. That collapses 212 unions to 122, and the removals are consolidations
rather than losses: 138 redundant copies of 87 distinct shapes become 35 copies
of 91 shapes, with more distinct shapes surviving than before. Spec $ref counts
show the fan-in -- Script 33, StringOrStringArray 19, FieldValue 17, Sort 17.
ClusterRemoteInfoResp drops out of the json.RawMessage allowlist: it was an
untyped map and is now the typed, discriminated union.
Accessors on a request-selected union no longer lie. AsSum() against histogram
bytes returned a zero Sum with a nil error, indistinguishable from a real zero,
because encoding/json ignores unknown keys and a decode-error check cannot see
the mismatch; a required-property probe now rejects it. Constructors for a
discriminated branch also left the discriminant field empty, so a
marshal-unmarshal round trip failed on an empty discriminator.
Branch names come from the schema key's local segment rather than the qualified
Go type, so a const does not restate the group prefix its union name already
carries. Where a `<Union><Branch>Type` const would collide with a `<Union>Type`
enum type -- three analysis unions with a `Definition` branch beside a
`<Thing>Definition` union -- the branch is renamed, scoped to $ref branches so
inline branches keep their construction.
Vocabulary follows the spec instead of the decode mechanism. IsLazy and
LazyAccessors named a strategy while conflating two unrelated situations, "the
spec provides no discriminator" with "we never read the discriminator", and
become RequestSelected: the aggregation and suggest unions carry
x-supports-typed-keys and genuinely cannot be discriminated, since the branch is
chosen by the request and echoed only in the response map key. TypeLazyUnion
becomes TypeAmbiguousWire, unionNeedsTryEach becomes
branchesCollideOnTokenClass, resolveParentScopedUnion becomes resolveRefUnion.
TokenClass stays, documented as the fallback it is rather than the primary
strategy.
BREAKING CHANGE: 135 union types are removed and 45 added as $ref'd schemas
consolidate onto their spec names. MGetRespBodyDocsItem becomes MGetRespItem,
MSearchMultiSearchResultResponsesItem becomes MSearchRespItem, SortResultsItem
and CommonAggregationsCompositeAggregateKeyValue become FieldValue,
ErrorCauseHeaderValue becomes StringOrStringArray,
SearchResultAggregationsValue becomes CommonAggregationsAggregate, and
CatRecoveryRecord{Start,Stop}TimeMillis with
Replication{,Index}FollowerStatusTotalWriteTimeMillis become
StringifiedEpochTimeUnitMillis. SearchHitsMetadataTotal.SearchTotalHits becomes
TotalHits. Branch accessors lose the group prefix the union name already
carries, so MGetMultiGetError becomes MultiGetError. The six discriminated
unions gain a Type() and consts they did not have. UnionBranchError gains Err
and Unwrap, and Got may be "incompatible payload" when the payload cannot be
the requested branch.
Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>1 parent 9edf259 commit 6746289
88 files changed
Lines changed: 14866 additions & 28998 deletions
File tree
- cmd/osgen
- emit
- ir
- internal/build
- opensearchapi
- plugins
- flow_framework
- ml
- replication
- search_relevance
- sql
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | | - | |
13 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
14 | 26 | | |
15 | 27 | | |
16 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
202 | 202 | | |
203 | 203 | | |
204 | 204 | | |
205 | | - | |
206 | | - | |
| 205 | + | |
| 206 | + | |
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| |||
212 | 212 | | |
213 | 213 | | |
214 | 214 | | |
215 | | - | |
| 215 | + | |
216 | 216 | | |
217 | 217 | | |
218 | 218 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
| 75 | + | |
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
| 98 | + | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
| 101 | + | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
236 | | - | |
| 236 | + | |
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
| |||
288 | 288 | | |
289 | 289 | | |
290 | 290 | | |
291 | | - | |
| 291 | + | |
292 | 292 | | |
293 | 293 | | |
294 | 294 | | |
295 | 295 | | |
296 | 296 | | |
297 | | - | |
| 297 | + | |
298 | 298 | | |
299 | 299 | | |
300 | 300 | | |
| |||
654 | 654 | | |
655 | 655 | | |
656 | 656 | | |
657 | | - | |
| 657 | + | |
658 | 658 | | |
659 | 659 | | |
660 | 660 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
278 | | - | |
| 278 | + | |
279 | 279 | | |
280 | 280 | | |
281 | 281 | | |
| |||
446 | 446 | | |
447 | 447 | | |
448 | 448 | | |
449 | | - | |
| 449 | + | |
450 | 450 | | |
451 | 451 | | |
452 | 452 | | |
| |||
460 | 460 | | |
461 | 461 | | |
462 | 462 | | |
463 | | - | |
| 463 | + | |
464 | 464 | | |
465 | 465 | | |
466 | 466 | | |
| |||
473 | 473 | | |
474 | 474 | | |
475 | 475 | | |
476 | | - | |
| 476 | + | |
477 | 477 | | |
478 | 478 | | |
479 | 479 | | |
| |||
507 | 507 | | |
508 | 508 | | |
509 | 509 | | |
510 | | - | |
| 510 | + | |
511 | 511 | | |
512 | 512 | | |
513 | 513 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
130 | | - | |
| 130 | + | |
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
| |||
0 commit comments