Skip to content

fix(compilers/openapi): read a repeated mapping key as its last value - #95

Merged
OmarAlJarrah merged 1 commit into
mainfrom
fix/openapi-duplicate-key-precedence
Jul 27, 2026
Merged

fix(compilers/openapi): read a repeated mapping key as its last value#95
OmarAlJarrah merged 1 commit into
mainfrom
fix/openapi-duplicate-key-precedence

Conversation

@OmarAlJarrah

Copy link
Copy Markdown
Member

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-key warning and
unmarshals every occurrence in turn, so the last value is what the resolver then works from — for a
map member and for a repeated $ref alike:

components:
  schemas:
    B: {type: object}
    B: {contentSchema: {$ref: '#/components/schemas/B/contentSchema'}}

The scan saw only the concrete first B, reported the document clean, and let the self-referential
second one through to the resolver, which dies with fatal error: stack overflow — the unrecoverable
crash 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.
dedupeLastWins handles the former, appendUnseen the latter, and dedupeFirstWins is now just the
merge-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 overflow
rather than failing an assertion.

Test plan

  • The fuzzer's input is committed at compilers/openapi/testdata/fuzz/FuzzCycleDetector/…, so a
    plain go test replays it.
  • testdata/openapi/cycle_duplicate_key.yaml is a readable hand-written version of the same shape,
    registered in cycleReproducers so both TestDetectCycles_Reproducers and
    TestCompile_CyclicSpecDoesNotCrash cover it, and in the harness corpus's knownInvalid set.
  • Reverting only the precedence rule — dedupeLastWins back to dedupeFirstWins — crashes the test
    binary on that fixture. That is what makes it a regression guard rather than a fixture that merely
    parses.
  • New whitebox cases for the expansion: last-wins on a repeated key, that it keeps the last
    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.

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.
@OmarAlJarrah
OmarAlJarrah merged commit 824ab02 into main Jul 27, 2026
1 check passed
@OmarAlJarrah
OmarAlJarrah deleted the fix/openapi-duplicate-key-precedence branch July 27, 2026 02:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant