Skip to content

Commit 388d6f7

Browse files
authored
Merge pull request #2395 from microsoft/fix/schema-no-warn
fix: warn instead of error out when $schema is present in the document
2 parents 34c9de1 + 7c7b053 commit 388d6f7

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/Microsoft.OpenApi/Reader/ParseNodes/PropertyNode.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,15 @@ public void ParseField<T>(
7474
}
7575
else
7676
{
77-
Context.Diagnostic.Errors.Add(
78-
new("", $"{Name} is not a valid property at {Context.GetLocation()}"));
77+
var error = new OpenApiError("", $"{Name} is not a valid property at {Context.GetLocation()}");
78+
if ("$schema".Equals(Name, StringComparison.OrdinalIgnoreCase))
79+
{
80+
Context.Diagnostic.Warnings.Add(error);
81+
}
82+
else
83+
{
84+
Context.Diagnostic.Errors.Add(error);
85+
}
7986
}
8087
}
8188
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,17 @@ public async Task ParseDocumentWithEmptyTagsWorks()
579579

580580
doc.Paths["/groups"].Operations[HttpMethod.Get].Tags.Should().BeNull("Empty tags are ignored, so we should not have any tags");
581581
}
582+
[Fact]
583+
public async Task DocumentWithSchemaResultsInWarning()
584+
{
585+
var path = Path.Combine(SampleFolderPath, "documentWithSchema.json");
586+
var (doc, diag) = await OpenApiDocument.LoadAsync(path, SettingsFixture.ReaderSettings);
587+
Assert.NotNull(doc);
588+
Assert.NotNull(diag);
589+
Assert.Empty(diag.Errors);
590+
Assert.Single(diag.Warnings);
591+
Assert.StartsWith("$schema is not a valid property", diag.Warnings[0].Message);
592+
}
582593

583594
[Fact]
584595
public void ParseEmptyMemoryStreamThrowsAnArgumentException()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"$schema": "https://spec.openapis.org/oas/3.1/schema/2025-02-13",
3+
"openapi": "3.1.0",
4+
"info": {
5+
"title": "Sample API",
6+
"version": "1.0.0"
7+
},
8+
"paths": {
9+
"/example": {
10+
"get": {
11+
"summary": "Example operation",
12+
"responses": {
13+
"200": {
14+
"description": "Successful response",
15+
"content": {
16+
"application/json": {
17+
"schema": {
18+
"type": "string"
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)