File tree Expand file tree Collapse file tree 4 files changed +130
-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 +130
-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
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT license.
3
+
4
+ using System . Collections . Generic ;
5
+ using FluentAssertions ;
6
+ using Microsoft . OpenApi . Exceptions ;
7
+ using Microsoft . OpenApi . Models ;
8
+ using Microsoft . OpenApi . Readers . Exceptions ;
9
+ using Xunit ;
10
+
11
+ namespace Microsoft . OpenApi . Readers . Tests . V2Tests
12
+ {
13
+ public class OpenApiDocumentTests
14
+ {
15
+ [ Fact ]
16
+ public void ShouldThrowWhenReferenceTypeIsInvalid ( )
17
+ {
18
+ var input = @"
19
+ swagger: 2.0
20
+ info:
21
+ title: test
22
+ version: 1.0.0
23
+ paths:
24
+ '/':
25
+ get:
26
+ responses:
27
+ '200':
28
+ description: ok
29
+ schema:
30
+ $ref: '#/defi888nition/does/notexist'
31
+ " ;
32
+
33
+ var reader = new OpenApiStringReader ( ) ;
34
+
35
+ Assert . Throws < OpenApiReaderException > ( ( ) => {
36
+ var doc = reader . Read ( input , out var diagnostic ) ;
37
+ } ) ;
38
+ }
39
+
40
+
41
+ [ Fact ]
42
+ public void ShouldThrowWhenReferenceDoesNotExist ( )
43
+ {
44
+ var input = @"
45
+ swagger: 2.0
46
+ info:
47
+ title: test
48
+ version: 1.0.0
49
+ paths:
50
+ '/':
51
+ get:
52
+ produces: ['application/json']
53
+ responses:
54
+ '200':
55
+ description: ok
56
+ schema:
57
+ $ref: '#/definitions/doesnotexist'
58
+ " ;
59
+
60
+ var reader = new OpenApiStringReader ( ) ;
61
+
62
+ var doc = reader . Read ( input , out var diagnostic ) ;
63
+
64
+ diagnostic . Errors . ShouldBeEquivalentTo ( new List < OpenApiError > {
65
+ new OpenApiError ( new OpenApiException ( "Invalid Reference identifier 'doesnotexist'." ) ) } ) ;
66
+ doc . Should ( ) . NotBeNull ( ) ;
67
+ }
68
+ }
69
+ }
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