Skip to content

Commit 49f6d70

Browse files
authored
Merge pull request #275 from Microsoft/dm/vnext/fix270
Fixed thrown exceptions
2 parents a78bb41 + e14fb34 commit 49f6d70

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

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)