1
1
using System . Collections . Generic ;
2
2
using System . Globalization ;
3
3
using System . IO ;
4
+ using System . Threading . Tasks ;
4
5
using FluentAssertions ;
5
6
using Microsoft . OpenApi . Extensions ;
6
- using Microsoft . OpenApi . Interfaces ;
7
7
using Microsoft . OpenApi . Models ;
8
8
using Microsoft . OpenApi . Models . References ;
9
9
using Microsoft . OpenApi . Reader ;
10
10
using Microsoft . OpenApi . Tests ;
11
11
using Microsoft . OpenApi . Writers ;
12
+ using Microsoft . OpenApi . Services ;
12
13
using Xunit ;
14
+ using System . Linq ;
13
15
14
16
namespace Microsoft . OpenApi . Readers . Tests . V31Tests
15
17
{
@@ -392,7 +394,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
392
394
new OpenApiDiagnostic ( ) { SpecificationVersion = OpenApiSpecVersion . OpenApi3_1 } ) ;
393
395
394
396
var outputWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
395
- var writer = new OpenApiJsonWriter ( outputWriter , new ( ) { InlineLocalReferences = true } ) ;
397
+ var writer = new OpenApiJsonWriter ( outputWriter , new ( ) { InlineLocalReferences = true } ) ;
396
398
actual . OpenApiDocument . SerializeAsV31 ( writer ) ;
397
399
var serialized = outputWriter . ToString ( ) ;
398
400
}
@@ -445,7 +447,7 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks()
445
447
}
446
448
}
447
449
} ;
448
-
450
+
449
451
// Serialization
450
452
var mediaType = result . OpenApiDocument . Paths [ "/example" ] . Operations [ OperationType . Get ] . Responses [ "200" ] . Content [ "application/json" ] ;
451
453
@@ -461,7 +463,7 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks()
461
463
type: string
462
464
prop3:
463
465
type: string" ;
464
-
466
+
465
467
var actualMediaType = mediaType . SerializeAsYaml ( OpenApiSpecVersion . OpenApi3_1 ) ;
466
468
467
469
// Assert
@@ -484,5 +486,48 @@ public void ParseDocumentWithReferenceByIdGetsResolved()
484
486
Assert . Equal ( "object" , requestBodySchema . Type ) ;
485
487
Assert . Equal ( "string" , parameterSchema . Type ) ;
486
488
}
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
+ }
487
532
}
488
533
}
0 commit comments