Skip to content

chore: sort resource messages for deterministic generation#16756

Merged
ohmayr merged 3 commits intomainfrom
sort-resource-names
Apr 22, 2026
Merged

chore: sort resource messages for deterministic generation#16756
ohmayr merged 3 commits intomainfrom
sort-resource-names

Conversation

@ohmayr
Copy link
Copy Markdown
Contributor

@ohmayr ohmayr commented Apr 21, 2026

sort resource messages for deterministic generation.

Towards b/505425328

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces deterministic ordering for resource messages by sorting them before they are returned, changing several return types from frozenset to tuple or sorted OrderedDict. This change ensures consistent output across different runs. A review comment suggests refining the sorting logic in wrappers.py by using a composite key to prevent non-deterministic behavior in case of attribute ties.

Comment on lines +2319 to +2322
sorted_messages = sorted(
unique_messages,
key=lambda m: m.resource_type_full_path or m.name
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The current sorting key m.resource_type_full_path or m.name may not guarantee deterministic ordering if multiple messages share the same resource type path or if a message name conflicts with a resource type path. Since the input is a frozenset (which is unordered), any tie in the sort key will result in non-deterministic output order across different runs.

Consider using a tuple as a sort key to provide a consistent tie-breaker, such as (m.resource_type_full_path or "", m.name).

Suggested change
sorted_messages = sorted(
unique_messages,
key=lambda m: m.resource_type_full_path or m.name
)
sorted_messages = sorted(
unique_messages,
key=lambda m: (m.resource_type_full_path or "", m.name)
)
References
  1. To ensure dictionary keys remain sorted without manual effort, programmatically sort the dictionary before returning it instead of relying on manual ordering in the code.

@ohmayr ohmayr force-pushed the sort-resource-names branch from 8620200 to bab5a9f Compare April 22, 2026 18:38
@ohmayr ohmayr marked this pull request as ready for review April 22, 2026 18:44
@ohmayr ohmayr requested a review from a team as a code owner April 22, 2026 18:44
@ohmayr ohmayr force-pushed the sort-resource-names branch from bab5a9f to 3cbe6b4 Compare April 22, 2026 19:10
Comment thread packages/gapic-generator/gapic/schema/wrappers.py
Comment thread packages/gapic-generator/gapic/schema/api.py
@ohmayr ohmayr merged commit aa43c83 into main Apr 22, 2026
98 checks passed
@ohmayr ohmayr deleted the sort-resource-names branch April 22, 2026 20:38
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.

3 participants