Skip to content

Commit d1b8b4d

Browse files
committed
Fixed thrown exceptions
1 parent b98b6d0 commit d1b8b4d

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

src/Microsoft.OpenApi.Readers/V2/OpenApiV2VersionService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.OpenApi.Exceptions;
88
using Microsoft.OpenApi.Interfaces;
99
using Microsoft.OpenApi.Models;
10+
using Microsoft.OpenApi.Readers.Exceptions;
1011
using Microsoft.OpenApi.Readers.Interface;
1112
using Microsoft.OpenApi.Readers.ParseNodes;
1213
using Microsoft.OpenApi.Readers.Properties;
@@ -90,7 +91,7 @@ private static ReferenceType ParseReferenceType(string referenceTypeName)
9091
return ReferenceType.SecurityScheme;
9192

9293
default:
93-
throw new ArgumentException();
94+
throw new OpenApiReaderException($"Unknown reference type '{referenceTypeName}'");
9495
}
9596
}
9697

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
305305
return null;
306306
}
307307

308+
if (this.Components == null) {
309+
throw new OpenApiException(string.Format(Properties.SRResource.InvalidReferenceId, reference.Id));
310+
}
311+
308312
try
309313
{
310314
switch (reference.Type)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,5 +2496,60 @@ public void SerializeSimpleDocumentWithTopLevelSelfReferencingWithOtherPropertie
24962496
expected = expected.MakeLineBreaksEnvironmentNeutral();
24972497
actual.Should().Be(expected);
24982498
}
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+
}
24992554
}
25002555
}

0 commit comments

Comments
 (0)