-
Notifications
You must be signed in to change notification settings - Fork 263
feat: Add writer settings to enable collection sorting using a comparer #2363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MaggieKimani1
merged 15 commits into
main
from
fix/revert-to-IDictionary-and-allow-sorting
Jun 2, 2025
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1b4ad07
fix: revert to using IDictionary for collections
MaggieKimani1 8a861c9
feat: Add writer settings to enable sorting collections using a comparer
MaggieKimani1 54fed37
chore: add tests and update public API
MaggieKimani1 b38fc79
Merge branch 'main' into fix/revert-to-IDictionary-and-allow-sorting
MaggieKimani1 1e65ae1
fix: resolve merge conflict errors
MaggieKimani1 392e5a7
chore: revert collections to use interface types
MaggieKimani1 58cb4ac
fix: rename class; add logic for sorting IEnumerable collections
MaggieKimani1 7f0e561
chore: add tests to validate
MaggieKimani1 b7eeadd
chore: clean up tests
MaggieKimani1 75f21b4
Merge branch 'main' into fix/revert-to-IDictionary-and-allow-sorting
MaggieKimani1 064b6ca
chore: WIP on fix/revert-to-IDictionary-and-allow-sorting
MaggieKimani1 85cd4b9
chore: update memory allocation baselines
MaggieKimani1 8198cbf
Merge branch 'main' into fix/revert-to-IDictionary-and-allow-sorting
MaggieKimani1 90edf8e
chore: resolve merge conflicts
MaggieKimani1 25d45ed
chore: rename setting; make extension class/methods internal
MaggieKimani1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.OpenApi.Extensions | ||
{ | ||
/// <summary> | ||
/// Dictionary extension methods | ||
/// </summary> | ||
public static class DictionaryExtensions | ||
{ | ||
/// <summary> | ||
/// Returns a new dictionary with entries sorted by key using the default comparer. | ||
/// </summary> | ||
public static IDictionary<TKey, TValue> Sort<TKey, TValue>( | ||
this IDictionary<TKey, TValue> source) | ||
where TKey : notnull | ||
{ | ||
#if NET7_0_OR_GREATER | ||
ArgumentNullException.ThrowIfNull(nameof(source)); | ||
#else | ||
if (source == null) | ||
throw new ArgumentNullException(nameof(source)); | ||
#endif | ||
|
||
return source.OrderBy(kvp => kvp.Key) | ||
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a new dictionary with entries sorted by key using a custom comparer. | ||
/// </summary> | ||
public static IDictionary<TKey, TValue> Sort<TKey, TValue>( | ||
this IDictionary<TKey, TValue> source, | ||
IComparer<TKey> comparer) | ||
where TKey : notnull | ||
{ | ||
#if NET7_0_OR_GREATER | ||
ArgumentNullException.ThrowIfNull(nameof(source)); | ||
ArgumentNullException.ThrowIfNull(nameof(comparer)); | ||
#else | ||
if (source == null) | ||
throw new ArgumentNullException(nameof(source)); | ||
if (comparer == null) | ||
throw new ArgumentNullException(nameof(comparer)); | ||
#endif | ||
return source.OrderBy(kvp => kvp.Key, comparer) | ||
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.