fix(compilers/openapi): read a repeated mapping key as its last value - #95
Merged
Merged
Conversation
A mapping that declares the same key twice was read first-key-wins by the cycle
pre-scan, which is the opposite of what the document it protects does. Speakeasy
does not refuse a duplicate key: it reports a validation-duplicate-key warning
and unmarshals every occurrence in turn, so the resolver works from the last
value. Reading the first therefore hid whatever the last one declared.
That is enough to lose a real cycle. In
schemas:
B: {type: object}
B: {contentSchema: {$ref: '#/components/schemas/B/contentSchema'}}
the scan saw only the concrete first B, called the document clean, and let the
self-referential second one reach the resolver — which dies with the fatal,
unrecoverable stack overflow the scan exists to prevent. The same reversal
applied to a repeated $ref within one schema.
Explicit keys now resolve to the last occurrence, at that occurrence's position.
Merge-key precedence is unchanged and deliberately kept separate: a merged key
still yields to an explicit one and to an earlier merge source, so the two rules
point in opposite directions and cannot share one deduplication pass.
FuzzCycleDetector found this while mutating the cycle reproducers; its input is
committed as a corpus entry, alongside a readable cycle_duplicate_key.yaml
registered with the other reproducers so both the detector-level and full-Compile
tests cover it. Reverting only the precedence rule crashes the test binary on
that fixture, which is what makes it a regression guard rather than a fixture
that merely parses.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The cycle pre-scan read a mapping that declares the same key twice as first-key-wins. That is the
opposite of what the library reading the document does, and the difference is enough to lose a real
cycle.
Speakeasy does not refuse a duplicate key. It reports a
validation-duplicate-keywarning andunmarshals every occurrence in turn, so the last value is what the resolver then works from — for a
map member and for a repeated
$refalike:The scan saw only the concrete first
B, reported the document clean, and let the self-referentialsecond one through to the resolver, which dies with
fatal error: stack overflow— the unrecoverablecrash the scan exists to prevent (GitHub #12, #26).
The change
Explicit keys now resolve to the last occurrence, at that occurrence's position.
Merge-key precedence is unchanged and is now deliberately a separate step. The two rules point in
opposite directions — a repeated explicit key resolves to its last value, while a merged key yields
to an explicit one and to any earlier merge source — so they cannot share one deduplication pass.
dedupeLastWinshandles the former,appendUnseenthe latter, anddedupeFirstWinsis now just themerge-sequence case expressed in terms of it.
How it was found
FuzzCycleDetector, mutating the committed cycle reproducers. It reached the failure in ~2 minutes,reported as
fuzzing process hung or terminated unexpectedly— a worker dying on the stack overflowrather than failing an assertion.
Test plan
compilers/openapi/testdata/fuzz/FuzzCycleDetector/…, so aplain
go testreplays it.testdata/openapi/cycle_duplicate_key.yamlis a readable hand-written version of the same shape,registered in
cycleReproducersso bothTestDetectCycles_ReproducersandTestCompile_CyclicSpecDoesNotCrashcover it, and in the harness corpus'sknownInvalidset.dedupeLastWinsback todedupeFirstWins— crashes the testbinary on that fixture. That is what makes it a regression guard rather than a fixture that merely
parses.
occurrence's position, and that a merged key still yields to a repeated explicit key (the two rules
composing).
gofmt -l .,go vet ./...,golangci-lint run(0 issues),go test ./..., and./scripts/check-coverage.sh(100.0% total, 100.0% every package) all pass.