Skip to content

fix(models): preserve tag collection types and comparers in copy constructors - #3141

Open
Hardik K (hardikkaurani) wants to merge 1 commit into
microsoft:mainfrom
hardikkaurani:fix/copy-constructors-preserve-tag-collections
Open

Hardik K (hardikkaurani) wants to merge 1 commit into
microsoft:mainfrom
hardikkaurani:fix/copy-constructors-preserve-tag-collections

Conversation

@hardikkaurani

@hardikkaurani Hardik K (hardikkaurani) commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When copying an \OpenApiOperation\ or \OpenApiDocument\ via copy constructors (
ew OpenApiOperation(operation)\ /
ew OpenApiDocument(document)), the \Tags\ property was previously converted into a default
ew HashSet(...)\ /
ew HashSet(...).

This caused two issues:

  1. If the source operation or document used custom tag collections (such as \SortedSet, custom comparer \HashSet, or \ImmutableSortedSet), copying the instance stripped the collection type and destroyed any custom ordering or custom equality comparers.
  2. In \OpenApiOperation,
    ew HashSet(operation.Tags)\ did not pass \OpenApiTagComparer.Instance, causing an unnecessary secondary re-allocation in the \Tags\ property setter.

Changes

  • Updated \OpenApiOperation(OpenApiOperation)\ copy constructor to pattern-match \operation.Tags\ preserving \SortedSet, custom comparer \HashSet, \ImmutableSortedSet, or falling back to \HashSet\ with \OpenApiTagComparer.Instance.
  • Updated \OpenApiDocument(OpenApiDocument?)\ copy constructor to similarly pattern-match \document?.Tags\ preserving collection types and comparers.
  • Added comprehensive unit tests in \OpenApiOperationTests.cs\ and \OpenApiDocumentTests.cs\ verifying preservation of collection types, comparers, ordering, and deep copy isolation.

Copilot AI balanced review requested due to automatic review settings October 21, 2026 05:54
@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner October 21, 2026 05:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet)! Following up on the custom tag collection support in #3128, this PR ensures that copy constructors in both OpenApiOperation and OpenApiDocument also preserve custom tag collection types (SortedSet, custom comparer HashSet, ImmutableSortedSet) and maintain collection deep-copy isolation. Unit tests covering all scenarios are included. Whenever you have a moment, please take a look. Thank you!

@baywet

Copy link
Copy Markdown
Member

Hardik K (@hardikkaurani) can you please solve the conflicts here?

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Vincent Biret (@baywet) Yes thanks for the review !

Just doing the chnages.

@hardikkaurani
Hardik K (hardikkaurani) force-pushed the fix/copy-constructors-preserve-tag-collections branch 2 times, most recently from 28b65dd to d9f48dd Compare October 21, 2026 15:19
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

I have rebased the branch onto the latest \main\ and resolved the conflicts caused by the squash-merge of #3128. The branch is now clean and up to date.

Thank you!

Comment thread src/Microsoft.OpenApi/Models/OpenApiOperation.cs Outdated
Comment thread src/Microsoft.OpenApi/Models/OpenApiDocument.cs Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No blocking issues were identified in the focused changes; regression tests cover the changed behavior but were not executed.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 21, 2026 17:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

I have updated the PR in commit 9730962 addressing your review feedback:

  • Refactored the tag normalization and cloning logic in OpenApiOperation into a shared static method CopyTags(ISet<OpenApiTagReference>? tags, bool clone = false), used by both the Tags property setter and the copy constructor.
  • Similarly refactored OpenApiDocument to use a shared static CopyTags(ISet<OpenApiTag>? tags, bool clone = false) for both the property setter and copy constructor.
  • Eliminated code duplication while preserving custom tag collection types (SortedSet, custom-comparer HashSet, ImmutableSortedSet) and deep copy independence.

Ready for your review! Thank you!

@baywet

Copy link
Copy Markdown
Member

Hardik K (@hardikkaurani) can you please solve the conflicts? (there were multiple parallel PRs on this topic, keep the minimal amount of changes in this PR)

Copilot AI balanced review requested due to automatic review settings October 21, 2026 19:00
@hardikkaurani
Hardik K (hardikkaurani) force-pushed the fix/copy-constructors-preserve-tag-collections branch from 9730962 to eb6ec97 Compare October 21, 2026 19:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

I have rebased the branch onto the latest main and resolved the conflicts.

As suggested, since #3116 was merged and already handles OpenApiOperation, I kept the changes in this PR minimal and strictly scoped to OpenApiDocument:

  • Refactored OpenApiDocument to use a common static CopyTags(ISet<OpenApiTag>? tags, bool copy = false) for both the Tags property setter and copy constructor.
  • Preserved custom tag collection types and comparers (SortedSet, custom comparer HashSet, ImmutableSortedSet) during copy construction.
  • Added corresponding unit tests in OpenApiDocumentTests.cs covering custom comparer, SortedSet, and ImmutableSortedSet preservation.

The PR is now clean, minimal, and mergeable. Ready for your review! Thank you!

This branch has not been deployed

No deployments
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