Skip to content

Fix non-deterministic JSON key order causing Swift Testing args to be misidentified#286

Merged
ypopovych merged 5 commits into
mainfrom
fix/sort-json-keys-for-stable-test-args
Jun 23, 2026
Merged

Fix non-deterministic JSON key order causing Swift Testing args to be misidentified#286
ypopovych merged 5 commits into
mainfrom
fix/sort-json-keys-for-stable-test-args

Conversation

@ypopovych

@ypopovych ypopovych commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Parameterized Swift Testing tests were being misidentified across runs because the `test.parameters` tag serialized argument dictionaries with non-deterministic key order.

Root cause: `set(parameters:)` in `Models.swift` encoded a `JSONGeneric.object(Dictionary<String, Self>)` using a plain `JSONEncoder()` with no key ordering configured. Swift dictionaries have no guaranteed iteration order, so the three argument keys (`name`, `value`, `type`) could appear in any order in the JSON string, producing a different fingerprint for the same test across runs.

Fix:

  • `APITypes.swift`: add `.sortedKeys` to `apiEncoder.outputFormatting` for consistent key ordering
  • `Models.swift`: use `JSONEncoder.apiEncoder` (already imported via `EventsExporter`) instead of a plain `JSONEncoder()` in `set(parameters:)`

Test plan

  • Run a parameterized Swift Testing test (e.g. `@Test(arguments: ...)`) multiple times and confirm `test.parameters` produces the same JSON string each run
  • Verify no regression in existing integration tests

🤖 Generated with Claude Code

@ypopovych
ypopovych requested review from a team as code owners June 22, 2026 14:05
@datadog-prod-us1-5

This comment has been minimized.

@ypopovych
ypopovych requested a review from anmarchenko June 23, 2026 11:54
ypopovych and others added 5 commits June 23, 2026 12:57
… misidentified

Add .sortedKeys to apiEncoder outputFormatting so test argument dictionaries
always serialize in a consistent order, preventing the same test from being
treated as different tests across runs.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…Models.swift

The apiEncoder change fixed key ordering in API payloads, but the test
parameters tag is encoded via a separate plain JSONEncoder() in set(parameters:).
Add .sortedKeys there so the JSONGeneric dictionary keys ("name", "value", "type")
always appear in a stable order in test.parameters.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Replace the standalone JSONEncoder with the shared apiEncoder which
already has .sortedKeys (and other settings) configured.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…raw JSON

With apiEncoder's .sortedKeys the serialized test.parameters tag is
fully deterministic, so there is no need to decode into a typed struct.
Sort the raw JSON strings (lexicographic order matches the p1 int values)
and compare directly against expected string literals.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@ypopovych
ypopovych force-pushed the fix/sort-json-keys-for-stable-test-args branch from 09e94da to 35b98e5 Compare June 23, 2026 11:57
@ypopovych
ypopovych merged commit 174c938 into main Jun 23, 2026
11 checks passed
@ypopovych
ypopovych deleted the fix/sort-json-keys-for-stable-test-args branch June 23, 2026 12:33
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.

2 participants