11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license.
33
4+ using System . Globalization ;
45using Microsoft . Extensions . Logging ;
6+ using Microsoft . OpenApi . Extensions ;
57using Microsoft . OpenApi . Models ;
68using Microsoft . OpenApi . Models . Interfaces ;
79using Microsoft . OpenApi . Models . References ;
810using Microsoft . OpenApi . Reader ;
911using Microsoft . OpenApi . Services ;
1012using Microsoft . OpenApi . Tests . UtilityFiles ;
13+ using Microsoft . OpenApi . Writers ;
1114using Moq ;
1215using Xunit ;
1316
@@ -235,7 +238,14 @@ public async Task CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly(
235238 using var stream = File . OpenRead ( filePath ) ;
236239 var settings = new OpenApiReaderSettings ( ) ;
237240 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+
239249 var predicate = OpenApiFilterService . CreatePredicate ( operationIds : operationIds ) ;
240250 var subsetOpenApiDocument = OpenApiFilterService . CreateFilteredDocument ( doc , predicate ) ;
241251
@@ -255,6 +265,26 @@ public async Task CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly(
255265 Assert . Single ( targetHeaders ) ;
256266 Assert . NotNull ( targetExamples ) ;
257267 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 ) ;
258288 }
259289
260290 [ Theory ]
0 commit comments