Skip to content

Commit 58cb4ac

Browse files
committed
fix: rename class; add logic for sorting IEnumerable collections
1 parent 392e5a7 commit 58cb4ac

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

src/Microsoft.OpenApi/Extensions/DictionaryExtensions.cs renamed to src/Microsoft.OpenApi/Extensions/CollectionExtensions.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,44 @@ namespace Microsoft.OpenApi.Extensions
77
/// <summary>
88
/// Dictionary extension methods
99
/// </summary>
10-
public static class DictionaryExtensions
10+
public static class CollectionExtensions
1111
{
1212
/// <summary>
13-
/// Returns a new dictionary with entries sorted by key using the default comparer.
13+
/// Returns a new dictionary with entries sorted by key using a custom comparer.
1414
/// </summary>
1515
public static IDictionary<TKey, TValue> Sort<TKey, TValue>(
16-
this IDictionary<TKey, TValue> source)
16+
this IDictionary<TKey, TValue> source,
17+
IComparer<TKey> comparer)
1718
where TKey : notnull
1819
{
1920
#if NET7_0_OR_GREATER
2021
ArgumentNullException.ThrowIfNull(nameof(source));
22+
ArgumentNullException.ThrowIfNull(nameof(comparer));
2123
#else
2224
if (source == null)
2325
throw new ArgumentNullException(nameof(source));
26+
if (comparer == null)
27+
throw new ArgumentNullException(nameof(comparer));
2428
#endif
25-
26-
return source.OrderBy(kvp => kvp.Key)
29+
return source.OrderBy(kvp => kvp.Key, comparer)
2730
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
2831
}
2932

3033
/// <summary>
31-
/// Returns a new dictionary with entries sorted by key using a custom comparer.
34+
/// Sorts any IEnumerable<T> using the specified comparer and returns a List</T>.
3235
/// </summary>
33-
public static IDictionary<TKey, TValue> Sort<TKey, TValue>(
34-
this IDictionary<TKey, TValue> source,
35-
IComparer<TKey> comparer)
36-
where TKey : notnull
36+
public static List<T> Sort<T>(this IEnumerable<T> source, IComparer<T> comparer)
3737
{
3838
#if NET7_0_OR_GREATER
39-
ArgumentNullException.ThrowIfNull(nameof(source));
40-
ArgumentNullException.ThrowIfNull(nameof(comparer));
39+
ArgumentNullException.ThrowIfNull(source);
40+
ArgumentNullException.ThrowIfNull(comparer);
4141
#else
4242
if (source == null)
4343
throw new ArgumentNullException(nameof(source));
4444
if (comparer == null)
4545
throw new ArgumentNullException(nameof(comparer));
4646
#endif
47-
return source.OrderBy(kvp => kvp.Key, comparer)
48-
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
47+
return source.OrderBy(item => item, comparer).ToList();
4948
}
5049
}
5150
}

src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,13 @@ private static void WriteCollectionInternal<T>(
421421
writer.WriteStartArray();
422422
if (elements != null)
423423
{
424+
var settings = writer.GetSettings();
425+
426+
if (settings?.Comparer is IComparer<T> typedComparer)
427+
{
428+
elements = elements.Sort(typedComparer);
429+
}
430+
424431
foreach (var item in elements)
425432
{
426433
if (item != null)
@@ -460,14 +467,9 @@ private static void WriteMapInternal<T>(
460467
if (elements != null)
461468
{
462469
var settings = writer.GetSettings();
463-
464-
if (settings?.EnableSorting == true)
465-
{
466-
elements = elements.Sort(); // sort with default comparer
467-
}
468-
else if (settings?.KeyComparer != null)
470+
if (settings?.Comparer != null)
469471
{
470-
elements = elements.Sort(settings.KeyComparer); // sort using custom comparer
472+
elements = elements.Sort(settings.Comparer); // sort using custom comparer
471473
}
472474

473475
foreach (var item in elements)

src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,9 @@ internal bool ShouldInlineReference(OpenApiReference reference)
2626
|| (reference.IsExternal && InlineExternalReferences);
2727
}
2828

29-
/// <summary>
30-
/// Enables sorting of collections using the default comparer
31-
/// </summary>
32-
public bool EnableSorting { get; set; }
33-
3429
/// <summary>
3530
/// Custom comparer for sorting collections.
3631
/// </summary>
37-
public IComparer<string>? KeyComparer { get; set; }
32+
public IComparer<string>? Comparer { get; set; }
3833
}
3934
}

0 commit comments

Comments
 (0)