File tree Expand file tree Collapse file tree 4 files changed +138
-1
lines changed
Microsoft.OpenApi.Readers/V2
Microsoft.OpenApi.Readers.Tests/V2Tests
Microsoft.OpenApi.Tests/Models Expand file tree Collapse file tree 4 files changed +138
-1
lines changed Original file line number Diff line number Diff line change 7
7
using Microsoft . OpenApi . Exceptions ;
8
8
using Microsoft . OpenApi . Interfaces ;
9
9
using Microsoft . OpenApi . Models ;
10
+ using Microsoft . OpenApi . Readers . Exceptions ;
10
11
using Microsoft . OpenApi . Readers . Interface ;
11
12
using Microsoft . OpenApi . Readers . ParseNodes ;
12
13
using Microsoft . OpenApi . Readers . Properties ;
@@ -90,7 +91,7 @@ private static ReferenceType ParseReferenceType(string referenceTypeName)
90
91
return ReferenceType . SecurityScheme ;
91
92
92
93
default :
93
- throw new ArgumentException ( ) ;
94
+ throw new OpenApiReaderException ( $ "Unknown reference type ' { referenceTypeName } '" ) ;
94
95
}
95
96
}
96
97
Original file line number Diff line number Diff line change @@ -305,6 +305,10 @@ public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
305
305
return null ;
306
306
}
307
307
308
+ if ( this . Components == null ) {
309
+ throw new OpenApiException ( string . Format ( Properties . SRResource . InvalidReferenceId , reference . Id ) ) ;
310
+ }
311
+
308
312
try
309
313
{
310
314
switch ( reference . Type )
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Text ;
5
+ using System . Threading . Tasks ;
6
+ using FluentAssertions ;
7
+ using Microsoft . OpenApi . Exceptions ;
8
+ using Microsoft . OpenApi . Models ;
9
+ using Microsoft . OpenApi . Readers . Exceptions ;
10
+ using Xunit ;
11
+
12
+ namespace Microsoft . OpenApi . Readers . Tests . V2Tests
13
+ {
14
+ public class OpenApiDocumentTests
15
+ {
16
+ [ Fact ]
17
+ public void ShouldThrowWhenReferenceTypeIsInvalid ( )
18
+ {
19
+ var input = @"
20
+ swagger: 2.0
21
+ info:
22
+ title: test
23
+ version: 1.0.0
24
+ paths:
25
+ '/':
26
+ get:
27
+ responses:
28
+ '200':
29
+ description: ok
30
+ schema:
31
+ $ref: '#/definition/does/notexist'
32
+ " ;
33
+
34
+ var reader = new OpenApiStringReader ( new OpenApiReaderSettings ( ) {
35
+ } ) ;
36
+
37
+ OpenApiDocument doc = null ;
38
+
39
+ Assert . Throws < OpenApiReaderException > ( ( ) =>
40
+ doc = reader . Read ( input , out var diagnostic ) ) ;
41
+
42
+ Assert . Null ( doc ) ;
43
+ }
44
+
45
+
46
+ [ Fact ]
47
+ public void ShouldThrowWhenReferenceDoesNotExist ( )
48
+ {
49
+ var input = @"
50
+ swagger: 2.0
51
+ info:
52
+ title: test
53
+ version: 1.0.0
54
+ paths:
55
+ '/':
56
+ get:
57
+ produces: ['application/json']
58
+ responses:
59
+ '200':
60
+ description: ok
61
+ schema:
62
+ $ref: '#/definitions/doesnotexist'
63
+ " ;
64
+
65
+ var reader = new OpenApiStringReader ( new OpenApiReaderSettings ( )
66
+ {
67
+ } ) ;
68
+
69
+ OpenApiDocument doc = null ;
70
+
71
+ doc = reader . Read ( input , out var diagnostic ) ;
72
+
73
+ diagnostic . Errors . ShouldBeEquivalentTo ( new List < OpenApiError > {
74
+ new OpenApiError ( new OpenApiException ( "Invalid Reference identifier 'doesnotexist'." ) ) } ) ;
75
+ }
76
+ }
77
+ }
Original file line number Diff line number Diff line change @@ -2496,5 +2496,60 @@ public void SerializeSimpleDocumentWithTopLevelSelfReferencingWithOtherPropertie
2496
2496
expected = expected . MakeLineBreaksEnvironmentNeutral ( ) ;
2497
2497
actual . Should ( ) . Be ( expected ) ;
2498
2498
}
2499
+
2500
+ [ Fact ]
2501
+ public void SerializeDocumentWithReferenceButNoComponents ( )
2502
+ {
2503
+ // Arrange
2504
+ var document = new OpenApiDocument ( )
2505
+ {
2506
+ Info = new OpenApiInfo
2507
+ {
2508
+ Title = "Test" ,
2509
+ Version = "1.0.0"
2510
+ } ,
2511
+ Paths = new OpenApiPaths
2512
+ {
2513
+ [ "/" ] = new OpenApiPathItem
2514
+ {
2515
+ Operations = new Dictionary < OperationType , OpenApiOperation >
2516
+ {
2517
+ [ OperationType . Get ] = new OpenApiOperation
2518
+ {
2519
+ Responses = new OpenApiResponses
2520
+ {
2521
+ [ "200" ] = new OpenApiResponse
2522
+ {
2523
+ Content = new Dictionary < string , OpenApiMediaType > ( )
2524
+ {
2525
+ [ "application/json" ] = new OpenApiMediaType
2526
+ {
2527
+ Schema = new OpenApiSchema
2528
+ {
2529
+ Reference = new OpenApiReference
2530
+ {
2531
+ Id = "test" ,
2532
+ Type = ReferenceType . Schema
2533
+ }
2534
+ }
2535
+ }
2536
+ }
2537
+ }
2538
+ }
2539
+ }
2540
+ }
2541
+ }
2542
+ }
2543
+ } ;
2544
+
2545
+
2546
+ var reference = document . Paths [ "/" ] . Operations [ OperationType . Get ] . Responses [ "200" ] . Content [ "application/json" ] . Schema . Reference ;
2547
+
2548
+ // Act
2549
+ var actual = document . Serialize ( OpenApiSpecVersion . OpenApi2_0 , OpenApiFormat . Json ) ;
2550
+
2551
+ // Assert
2552
+ Assert . NotEmpty ( actual ) ;
2553
+ }
2499
2554
}
2500
2555
}
You can’t perform that action at this time.
0 commit comments