Skip to content

Commit e6ab1cd

Browse files
Merge pull request #1955 from microsoft/mk/fix-empty-paths-failing
Fix: Empty paths object is now failing for version 3.0.0
2 parents 98b01e0 + 6a830b8 commit e6ab1cd

File tree

8 files changed

+23
-51
lines changed

8 files changed

+23
-51
lines changed

src/Microsoft.OpenApi/Reader/ParsingContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,9 @@ public void PopLoop(string loopid)
271271

272272
private void ValidateRequiredFields(OpenApiDocument doc, string version)
273273
{
274-
if ((version.is2_0() || version.is3_0()) && (doc.Paths == null || !doc.Paths.Any()))
274+
if ((version.is2_0() || version.is3_0()) && (doc.Paths == null))
275275
{
276-
// paths is a required field in OpenAPI 3.0 but optional in 3.1
276+
// paths is a required field in OpenAPI 2.0 and 3.0 but optional in 3.1
277277
RootNode.Context.Diagnostic.Errors.Add(new OpenApiError("", $"Paths is a REQUIRED field at {RootNode.Context.GetLocation()}"));
278278
}
279279
}

test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiDiagnosticTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ public async Task DiagnosticReportMergedForExternalReferenceAsync()
5656

5757
Assert.NotNull(result);
5858
Assert.NotNull(result.OpenApiDocument.Workspace);
59-
result.OpenApiDiagnostic.Errors.Should().BeEquivalentTo(new List<OpenApiError>
60-
{
61-
new OpenApiError("", "[File: ./TodoReference.yaml] Paths is a REQUIRED field at #/")
62-
});
59+
result.OpenApiDiagnostic.Errors.Should().BeEmpty();
6360
}
6461
}
6562

test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public void BrokenSimpleList()
3434
var result = OpenApiDocument.Parse(input, "yaml");
3535

3636
result.OpenApiDiagnostic.Errors.Should().BeEquivalentTo(new List<OpenApiError>() {
37-
new OpenApiError(new OpenApiReaderException("Expected a value.")),
38-
new OpenApiError("", "Paths is a REQUIRED field at #/")
37+
new OpenApiError(new OpenApiReaderException("Expected a value."))
3938
});
4039
}
4140

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,7 @@ public void ParseDocumentWithDifferentCultureShouldSucceed(string culture)
9696
.Excluding((IMemberInfo memberInfo) =>
9797
memberInfo.Path.EndsWith("Parent"))
9898
.Excluding((IMemberInfo memberInfo) =>
99-
memberInfo.Path.EndsWith("Root")));
100-
101-
result.OpenApiDiagnostic.Should().BeEquivalentTo(
102-
new OpenApiDiagnostic {
103-
SpecificationVersion = OpenApiSpecVersion.OpenApi2_0,
104-
Errors = new List<OpenApiError>()
105-
{
106-
new OpenApiError("", "Paths is a REQUIRED field at #/")
107-
}
108-
});
99+
memberInfo.Path.EndsWith("Root")));;
109100
}
110101

111102
[Fact]

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ public void InvalidHostShouldYieldError()
310310
{
311311
Errors =
312312
{
313-
new OpenApiError("#/", "Invalid host"),
314-
new OpenApiError("", "Paths is a REQUIRED field at #/")
313+
new OpenApiError("#/", "Invalid host")
315314
},
316315
SpecificationVersion = OpenApiSpecVersion.OpenApi2_0
317316
});

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,7 @@ public void ParseDocumentFromInlineStringShouldSucceed()
101101
result.OpenApiDiagnostic.Should().BeEquivalentTo(
102102
new OpenApiDiagnostic()
103103
{
104-
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
105-
Errors = new List<OpenApiError>()
106-
{
107-
new OpenApiError("", "Paths is a REQUIRED field at #/")
108-
}
104+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
109105
});
110106
}
111107

@@ -115,16 +111,7 @@ public void ParseBasicDocumentWithMultipleServersShouldSucceed()
115111
var path = System.IO.Path.Combine(SampleFolderPath, "basicDocumentWithMultipleServers.yaml");
116112
var result = OpenApiDocument.Load(path);
117113

118-
result.OpenApiDiagnostic.Should().BeEquivalentTo(
119-
new OpenApiDiagnostic()
120-
{
121-
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
122-
Errors = new List<OpenApiError>()
123-
{
124-
new OpenApiError("", "Paths is a REQUIRED field at #/")
125-
}
126-
});
127-
114+
result.OpenApiDiagnostic.Errors.Should().BeEmpty();
128115
result.OpenApiDocument.Should().BeEquivalentTo(
129116
new OpenApiDocument
130117
{
@@ -170,7 +157,6 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
170157
{
171158
Errors =
172159
{
173-
new OpenApiError("", "Paths is a REQUIRED field at #/"),
174160
new OpenApiValidatorError(nameof(OpenApiInfoRules.InfoRequiredFields),"#/info/title", "The field 'title' in 'info' object is REQUIRED.")
175161
},
176162
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
@@ -196,11 +182,7 @@ public void ParseMinimalDocumentShouldSucceed()
196182
result.OpenApiDiagnostic.Should().BeEquivalentTo(
197183
new OpenApiDiagnostic()
198184
{
199-
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
200-
Errors = new List<OpenApiError>()
201-
{
202-
new OpenApiError("", "Paths is a REQUIRED field at #/")
203-
}
185+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
204186
});
205187
}
206188

@@ -1388,11 +1370,7 @@ public void ParseBasicDocumentWithServerVariableShouldSucceed()
13881370
result.OpenApiDiagnostic.Should().BeEquivalentTo(
13891371
new OpenApiDiagnostic
13901372
{
1391-
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
1392-
Errors = new List<OpenApiError>()
1393-
{
1394-
new OpenApiError("", "Paths is a REQUIRED field at #/")
1395-
}
1373+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
13961374
});
13971375

13981376
result.OpenApiDocument.Should().BeEquivalentTo(expected, options => options.Excluding(x => x.BaseUri));
@@ -1417,5 +1395,12 @@ public void ParseBasicDocumentWithServerVariableAndNoDefaultShouldFail()
14171395

14181396
result.OpenApiDiagnostic.Errors.Should().NotBeEmpty();
14191397
}
1398+
1399+
[Fact]
1400+
public void ParseDocumentWithEmptyPathsSucceeds()
1401+
{
1402+
var result = OpenApiDocument.Load(System.IO.Path.Combine(SampleFolderPath, "docWithEmptyPaths.yaml"));
1403+
result.OpenApiDiagnostic.Errors.Should().BeEmpty();
1404+
}
14201405
}
14211406
}

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,7 @@ public void ParseBasicSchemaWithReferenceShouldSucceed()
241241
result.OpenApiDiagnostic.Should().BeEquivalentTo(
242242
new OpenApiDiagnostic()
243243
{
244-
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0,
245-
Errors = new List<OpenApiError>()
246-
{
247-
new OpenApiError("", "Paths is a REQUIRED field at #/")
248-
}
244+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
249245
});
250246

251247
var expectedComponents = new OpenApiComponents
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Sample API
4+
version: 1.0.0
5+
paths: {}

0 commit comments

Comments
 (0)