1414
1515namespace Microsoft . OpenApi . Tests . Extensions
1616{
17- public class DictionaryExtensionsTests
17+ public class CollectionExtensionsTests
1818 {
1919 public static readonly OpenApiDocument Document = new OpenApiDocument
2020 {
@@ -137,72 +137,73 @@ public class DictionaryExtensionsTests
137137 }
138138 } ;
139139
140- [ Fact ]
141- public void SortEmptyDictionaryReturnsEmptyDictionary ( )
140+ [ Theory ]
141+ [ MemberData ( nameof ( OpenApiSpecVersions ) ) ]
142+ public async Task SortOpenApiDocumentUsingCustomComparerSucceeds ( OpenApiSpecVersion version )
142143 {
143144 // Arrange
144- Document . Components . Headers = new Dictionary < string , IOpenApiHeader > ( ) ;
145+ var outputStringWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
146+ var settings = new OpenApiWriterSettings
147+ {
148+ Comparer = StringComparer . OrdinalIgnoreCase
149+ } ;
150+ var writer = new OpenApiYamlWriter ( outputStringWriter , settings ) ;
145151
146152 // Act
147- var sortedDictionary = Document . Components . Headers . Sort ( ) ;
153+ Document . SerializeAs ( version , writer ) ;
154+ await writer . FlushAsync ( ) ;
148155
149156 // Assert
150- Assert . Empty ( sortedDictionary ) ;
157+ await Verifier . Verify ( outputStringWriter ) . UseParameters ( version ) ;
151158 }
152159
153160 [ Fact ]
154- public async Task SortOpenApiDocumentLexicographicallySucceeds ( )
161+ public async Task SortHashSetsWorks ( )
155162 {
156163 // Arrange
157164 var expected = @"required:
158165 - id
159166 - name
160- properties:
161- id:
162- type: integer
163- format: int64
164- name:
165- type: string
166- tag:
167- type: string" ;
167+ - tag" ;
168+ var schema = new OpenApiSchema
169+ {
170+ Required = new HashSet < string > { "tag" , "id" , "name" } ,
171+ } ;
168172
169173 var outputStringWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
170174 var settings = new OpenApiWriterSettings
171175 {
172- EnableSorting = true
176+ Comparer = StringComparer . OrdinalIgnoreCase
173177 } ;
174178 var writer = new OpenApiYamlWriter ( outputStringWriter , settings ) ;
175179
176180 // Act
177- var schema = Document . Components . Schemas [ "pet" ] ;
178-
179181 schema . SerializeAsV3 ( writer ) ;
180182 await writer . FlushAsync ( ) ;
181- var actual = outputStringWriter . ToString ( ) ;
183+ var sortedString = outputStringWriter . ToString ( ) ;
182184
183185 // Assert
184- Assert . Equal ( expected . MakeLineBreaksEnvironmentNeutral ( ) , actual . MakeLineBreaksEnvironmentNeutral ( ) ) ;
186+ Assert . Equal ( expected . MakeLineBreaksEnvironmentNeutral ( ) , sortedString . MakeLineBreaksEnvironmentNeutral ( ) ) ;
185187 }
186188
187- [ Theory ]
188- [ MemberData ( nameof ( OpenApiSpecVersions ) ) ]
189- public async Task SortOpenApiDocumentUsingCustomComparerSucceeds ( OpenApiSpecVersion version )
189+ [ Fact ]
190+ public void SortTagsByNameUsingComparerWorks ( )
190191 {
191192 // Arrange
192- var outputStringWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
193- var settings = new OpenApiWriterSettings
193+ var tags = new HashSet < OpenApiTag >
194194 {
195- KeyComparer = StringComparer . OrdinalIgnoreCase
195+ new ( ) { Name = "three" } ,
196+ new ( ) { Name = "two" } ,
197+ new ( ) { Name = "one" }
196198 } ;
197- var writer = new OpenApiYamlWriter ( outputStringWriter , settings ) ;
198199
199200 // Act
200- Document . SerializeAs ( version , writer ) ;
201- await writer . FlushAsync ( ) ;
201+ var sortedTags = tags . Sort ( new OpenApiTagNameComparer ( ) ) ;
202202
203203 // Assert
204- await Verifier . Verify ( outputStringWriter ) . UseParameters ( version ) ;
205- }
204+ Assert . Equal ( 3 , sortedTags . Count ) ;
205+ Assert . True ( sortedTags [ 0 ] . Name == "one" ) ;
206+ }
206207
207208 public static TheoryData < OpenApiSpecVersion > OpenApiSpecVersions ( )
208209 {
@@ -214,4 +215,12 @@ public static TheoryData<OpenApiSpecVersion> OpenApiSpecVersions()
214215 return values ;
215216 }
216217 }
218+
219+ public class OpenApiTagNameComparer : IComparer < OpenApiTag >
220+ {
221+ public int Compare ( OpenApiTag tag1 , OpenApiTag tag2 )
222+ {
223+ return string . Compare ( tag1 . Name , tag2 . Name , StringComparison . OrdinalIgnoreCase ) ;
224+ }
225+ }
217226}
0 commit comments