1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
+ using System . Globalization ;
4
5
using Microsoft . Extensions . Logging ;
6
+ using Microsoft . OpenApi . Extensions ;
5
7
using Microsoft . OpenApi . Models ;
6
8
using Microsoft . OpenApi . Models . Interfaces ;
7
9
using Microsoft . OpenApi . Models . References ;
8
10
using Microsoft . OpenApi . Reader ;
9
11
using Microsoft . OpenApi . Services ;
10
12
using Microsoft . OpenApi . Tests . UtilityFiles ;
13
+ using Microsoft . OpenApi . Writers ;
11
14
using Moq ;
12
15
using Xunit ;
13
16
@@ -235,7 +238,14 @@ public async Task CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly(
235
238
using var stream = File . OpenRead ( filePath ) ;
236
239
var settings = new OpenApiReaderSettings ( ) ;
237
240
settings . AddYamlReader ( ) ;
238
- var doc = ( await OpenApiDocument . LoadAsync ( stream , "yaml" , settings ) ) . Document ;
241
+ var doc = ( await OpenApiDocument . LoadAsync ( stream , "yaml" , settings ) ) . Document ;
242
+
243
+ // validated the tags are read as references
244
+ var openApiOperationTags = doc . Paths [ "/items" ] . Operations [ OperationType . Get ] . Tags ? . ToArray ( ) ;
245
+ Assert . NotNull ( openApiOperationTags ) ;
246
+ Assert . Single ( openApiOperationTags ) ;
247
+ Assert . True ( openApiOperationTags [ 0 ] . UnresolvedReference ) ;
248
+
239
249
var predicate = OpenApiFilterService . CreatePredicate ( operationIds : operationIds ) ;
240
250
var subsetOpenApiDocument = OpenApiFilterService . CreateFilteredDocument ( doc , predicate ) ;
241
251
@@ -255,6 +265,26 @@ public async Task CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly(
255
265
Assert . Single ( targetHeaders ) ;
256
266
Assert . NotNull ( targetExamples ) ;
257
267
Assert . Single ( targetExamples ) ;
268
+ // validated the tags of the trimmed document are read as references
269
+ var trimmedOpenApiOperationTags = subsetOpenApiDocument . Paths [ "/items" ] . Operations [ OperationType . Get ] . Tags ? . ToArray ( ) ;
270
+ Assert . NotNull ( trimmedOpenApiOperationTags ) ;
271
+ Assert . Single ( trimmedOpenApiOperationTags ) ;
272
+ Assert . True ( trimmedOpenApiOperationTags [ 0 ] . UnresolvedReference ) ;
273
+
274
+ // Finally try to write the trimmed document as v3 document
275
+ var outputStringWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
276
+ var writer = new OpenApiJsonWriter ( outputStringWriter )
277
+ {
278
+ Settings = new OpenApiWriterSettings ( )
279
+ {
280
+ InlineExternalReferences = true ,
281
+ InlineLocalReferences = true
282
+ }
283
+ } ;
284
+ subsetOpenApiDocument . SerializeAsV3 ( writer ) ;
285
+ await writer . FlushAsync ( ) ;
286
+ var result = outputStringWriter . ToString ( ) ;
287
+ Assert . NotEmpty ( result ) ;
258
288
}
259
289
260
290
[ Theory ]
0 commit comments