Skip to content

fix: Ensure autopatch processes properties in the same order every time - #1079

Closed
lucaspopp-wbd wants to merge 2 commits into
danielgtaylor:mainfrom
lucaspopp-wbd:autopatch-determinism
Closed

fix: Ensure autopatch processes properties in the same order every time#1079
lucaspopp-wbd wants to merge 2 commits into
danielgtaylor:mainfrom
lucaspopp-wbd:autopatch-determinism

Conversation

@lucaspopp-wbd

Copy link
Copy Markdown
Contributor

Since map iterators return properties in a random order, autopatch schemas can fluctuate when multiple fields use the same type.

This change ensures fields are processed in a deterministic order (sorted by name) to avoid type name fluctuations.

@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.23%. Comparing base (22a3cb0) to head (4558c17).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1079   +/-   ##
=======================================
  Coverage   93.22%   93.23%           
=======================================
  Files          23       23           
  Lines        4976     4982    +6     
=======================================
+ Hits         4639     4645    +6     
  Misses        271      271           
  Partials       66       66           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@wolveix

wolveix commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR, but I don't think this change can affect the generated schema.

makeOptionalSchema is side-effect free, and its only action here is inserting into optionalSchema.Properties, where map insertion order isn't observable. Schema.MarshalJSON emits properties as a map[string]*Schema, so encoding/json already sorts the keys, and nothing in this path touches the registry. I dumped the spec 500 times on 22a3cb0 with several same-typed inline fields across four autopatched resources: one distinct output, keys already sorted.

The tests only exercise sortedMapIterator itself, so they'd pass with makeOptionalSchema untouched. Could you add a test that reproduces the fluctuation, i.e. one that fails on main, or share the setup where names fluctuate?

Two things that may be closer to what you're seeing:

  1. makeOptionalSchema doesn't copy s.Ref, so every $ref property (any nested struct field) collapses to {} in the merge-patch body.
  2. for _, path := range oapi.Paths in AutoPatch is real map-order iteration, so PATCH operations register in random order.

If we do keep the sort, I'd inline it rather than add a helper, matching schema.go:

for _, k := range slices.Sorted(maps.Keys(s.Properties)) {
	optionalSchema.Properties[k] = makeOptionalSchema(s.Properties[k])
}

@lucaspopp-wbd

Copy link
Copy Markdown
Contributor Author

Good point! Closed the PR :)

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