11using System . Collections . Generic ;
22using System . Globalization ;
33using System . IO ;
4+ using System . Threading . Tasks ;
45using FluentAssertions ;
56using Microsoft . OpenApi . Extensions ;
6- using Microsoft . OpenApi . Interfaces ;
77using Microsoft . OpenApi . Models ;
88using Microsoft . OpenApi . Models . References ;
99using Microsoft . OpenApi . Reader ;
1010using Microsoft . OpenApi . Tests ;
1111using Microsoft . OpenApi . Writers ;
12+ using Microsoft . OpenApi . Services ;
1213using Xunit ;
14+ using System . Linq ;
1315
1416namespace Microsoft . OpenApi . Readers . Tests . V31Tests
1517{
@@ -392,7 +394,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
392394 new OpenApiDiagnostic ( ) { SpecificationVersion = OpenApiSpecVersion . OpenApi3_1 } ) ;
393395
394396 var outputWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
395- var writer = new OpenApiJsonWriter ( outputWriter , new ( ) { InlineLocalReferences = true } ) ;
397+ var writer = new OpenApiJsonWriter ( outputWriter , new ( ) { InlineLocalReferences = true } ) ;
396398 actual . OpenApiDocument . SerializeAsV31 ( writer ) ;
397399 var serialized = outputWriter . ToString ( ) ;
398400 }
@@ -445,7 +447,7 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks()
445447 }
446448 }
447449 } ;
448-
450+
449451 // Serialization
450452 var mediaType = result . OpenApiDocument . Paths [ "/example" ] . Operations [ OperationType . Get ] . Responses [ "200" ] . Content [ "application/json" ] ;
451453
@@ -461,7 +463,7 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks()
461463 type: string
462464 prop3:
463465 type: string" ;
464-
466+
465467 var actualMediaType = mediaType . SerializeAsYaml ( OpenApiSpecVersion . OpenApi3_1 ) ;
466468
467469 // Assert
@@ -484,5 +486,48 @@ public void ParseDocumentWithReferenceByIdGetsResolved()
484486 Assert . Equal ( "object" , requestBodySchema . Type ) ;
485487 Assert . Equal ( "string" , parameterSchema . Type ) ;
486488 }
489+
490+ [ Fact ]
491+ public async Task ExternalDocumentDereferenceToOpenApiDocumentUsingJsonPointerWorks ( )
492+ {
493+ // Arrange
494+ var path = Path . Combine ( Directory . GetCurrentDirectory ( ) , SampleFolderPath ) ;
495+
496+ var settings = new OpenApiReaderSettings
497+ {
498+ LoadExternalRefs = true ,
499+ BaseUrl = new ( path ) ,
500+ } ;
501+
502+ // Act
503+ var result = await OpenApiDocument . LoadAsync ( Path . Combine ( SampleFolderPath , "docWithExternalRef.yaml" ) , settings ) ;
504+ var responseSchema = result . OpenApiDocument . Paths [ "/resource" ] . Operations [ OperationType . Get ] . Responses [ "200" ] . Content [ "application/json" ] . Schema ;
505+
506+ // Assert
507+ result . OpenApiDocument . Workspace . Contains ( "./externalResource.yaml" ) ;
508+ responseSchema . Properties . Count . Should ( ) . Be ( 2 ) ; // reference has been resolved
509+ }
510+
511+ [ Fact ]
512+ public async Task ParseExternalDocumentDereferenceToOpenApiDocumentByIdWorks ( )
513+ {
514+ // Arrange
515+ var path = Path . Combine ( Directory . GetCurrentDirectory ( ) , SampleFolderPath ) ;
516+
517+ var settings = new OpenApiReaderSettings
518+ {
519+ LoadExternalRefs = true ,
520+ BaseUrl = new ( path ) ,
521+ } ;
522+
523+ // Act
524+ var result = await OpenApiDocument . LoadAsync ( Path . Combine ( SampleFolderPath , "docWithExternalRef.yaml" ) , settings ) ;
525+ var externalDoc = await OpenApiDocument . LoadAsync ( Path . Combine ( SampleFolderPath , "externalResource.yaml" ) , settings ) ;
526+
527+ var requestBodySchema = result . OpenApiDocument . Paths [ "/resource" ] . Operations [ OperationType . Get ] . Parameters . First ( ) . Schema ;
528+
529+ // Assert
530+ requestBodySchema . Properties . Count . Should ( ) . Be ( 2 ) ; // reference has been resolved
531+ }
487532 }
488533}
0 commit comments