Skip to content

fix(go): ensure deterministic ordering in generated requests.go and requests_test.go#13339

Merged
patrickthornton merged 6 commits intomainfrom
kunal/fix-go-generator-nondeterministic-ordering
Mar 13, 2026
Merged

fix(go): ensure deterministic ordering in generated requests.go and requests_test.go#13339
patrickthornton merged 6 commits intomainfrom
kunal/fix-go-generator-nondeterministic-ordering

Conversation

@developerkunal
Copy link
Collaborator

Description

Fix non-deterministic ordering in Go SDK generator output. Go maps randomize iteration order by design, so when the IR JSON is deserialized into Go maps, the insertion order is lost. This causes requests.go, requests_test.go, and other generated files to have different type orderings across runs — producing noisy PR diffs.

Changes Made

  • Added sortedMapKeys and sortedFileInfoKeys generic helpers using slices.SortFunc/cmp.Compare to extract and sort map keys before iteration
  • Applied sorted iteration to 12 map range loops in generators/go/internal/generator/generator.go that were iterating Go maps non-deterministically
  • Created a new go-deterministic-ordering Go-only test fixture with duplicate endpoint names (create, get, list) across 3 services to reproduce and prevent regression
  • Added ir-to-jsonschema snapshots and IR test definitions for the new fixture
  • Bumped version to 1.29.2 in generators/go/sdk/versions.yml

Testing

  • Unit tests added/updated — existing Go generator tests pass (go test ./...)
  • Manual testing completed:
    • Verified bug reproduces without fix (unfixed code shows request types swapping order across runs)
    • Verified fix produces deterministic output (10 consecutive seed runs, zero diffs)
    • Verified no regressions across all exhaustive fixtures (3/3) and examples fixtures (10/10)

Copilot AI review requested due to automatic review settings March 11, 2026 14:06
@github-actions
Copy link
Contributor

🌱 Seed Test Selector

Select languages to run seed tests for:

  • Python
  • TypeScript
  • Java
  • Go
  • Ruby
  • C#
  • PHP
  • Swift
  • Rust
  • OpenAPI
  • Postman

How to use: Click the ⋯ menu above → "Edit" → check the boxes you want → click "Update comment". Tests will run automatically and snapshots will be committed to this PR.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes non-deterministic ordering in the Go SDK generator output by sorting map keys prior to iteration, reducing noisy diffs in generated Go files.

Changes:

  • Adds sorted key iteration helpers and applies them across map range loops in the Go generator to make generation deterministic.
  • Introduces a new Go-only fixture (go-deterministic-ordering) to reproduce and prevent regressions for ordering issues.
  • Bumps the Go generator version to 1.29.2 and adds/updates related snapshots.

Reviewed changes

Copilot reviewed 182 out of 229 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
generators/go/sdk/versions.yml Version bump to 1.29.2 documenting the deterministic-ordering fix.
seed/go-sdk/go-deterministic-ordering/go.mod Adds a Go module for the new deterministic-ordering fixture.
seed/go-sdk/go-deterministic-ordering/.github/workflows/ci.yml Adds fixture-local CI workflow for build/lint/test.
seed/go-sdk/go-deterministic-ordering/internal/error_decoder.go Adds an error decoder utility used by generated clients.
seed/go-sdk/go-deterministic-ordering/internal/error_decoder_test.go Adds unit tests for the error decoder behavior.
seed/go-sdk/go-deterministic-ordering/internal/extra_properties.go Adds helper for extracting/marshaling extra JSON properties.
seed/go-sdk/go-deterministic-ordering/** Adds generated Go SDK + wiremock tests + dynamic snippets for the new fixture.
packages/cli/fern-definition/ir-to-jsonschema/src/test/snapshots/go-deterministic-ordering/** Adds JSON schema snapshots for the new fixture/types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…equests_test.go

  Go maps randomize iteration order, causing non-deterministic type ordering
  in requests.go when exportAllRequestsAtRoot is enabled. This sorts map keys
  before iteration using slices.SortFunc/cmp.Compare, and adds exhaustive
  test fixtures with duplicate endpoint names to reproduce and prevent regression.
…dering fixture

  Move duplicate-names services out of the shared exhaustive fixture into a
  new go-deterministic-ordering fixture with the full exhaustive IR definition.
  Remove export-all-requests-at-root from exhaustive since the ordering test
  now lives in the dedicated fixture. This avoids breaking C# and other
  generators that share the exhaustive test definitions.
@fern-support fern-support force-pushed the kunal/fix-go-generator-nondeterministic-ordering branch from b0cbf45 to f616487 Compare March 13, 2026 19:01
@patrickthornton patrickthornton merged commit 35aa1fe into main Mar 13, 2026
302 checks passed
@patrickthornton patrickthornton deleted the kunal/fix-go-generator-nondeterministic-ordering branch March 13, 2026 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants