Skip to content

Add webhooks property to the root document #1046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f6cc9d8
Add webhooks property to OpenAPI document
MaggieKimani1 Oct 17, 2022
0678365
Deep copy the webhooks object in the copy constructor
MaggieKimani1 Oct 17, 2022
5bb8f44
Add serialization for the webhooks property
MaggieKimani1 Oct 17, 2022
7479780
Add logic to deserialize the webhooks property
MaggieKimani1 Oct 17, 2022
43e165c
Clean up project references
MaggieKimani1 Oct 17, 2022
9efc130
Add pathItem reference type and webhooks constant
MaggieKimani1 Oct 17, 2022
c79a4f9
Adds tests
MaggieKimani1 Oct 24, 2022
103f123
Adds 3.1 as a valid input OpenAPI version
MaggieKimani1 Oct 24, 2022
624bd0c
Adds a walker to visit the webhooks object and its child elements
MaggieKimani1 Oct 24, 2022
7a14575
Update the validation rule to exclude paths as a required field accor…
MaggieKimani1 Oct 24, 2022
8d004ff
Revert change
MaggieKimani1 Oct 25, 2022
149175c
Update test with correct property type
MaggieKimani1 Oct 25, 2022
17b1c2d
Add the validation for Paths as a required field in 3.0 during parsing
MaggieKimani1 Oct 26, 2022
c79bd11
Update spec version
MaggieKimani1 Oct 26, 2022
b7e3e48
Add more validation for empty paths and missing paths/webhooks for 3.1
MaggieKimani1 Oct 26, 2022
846fb0e
Use Any() instead of count
MaggieKimani1 Oct 27, 2022
2299117
Code clean up
MaggieKimani1 Oct 27, 2022
21e19a0
Add negation operator
MaggieKimani1 Oct 27, 2022
6152336
Change reference type to pathItem
MaggieKimani1 Oct 27, 2022
86594a8
Reuse LoadPaths() logic to avoid creating a root reference object in …
MaggieKimani1 Oct 31, 2022
243eb23
Update test
MaggieKimani1 Oct 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Microsoft.OpenApi.Readers/ParsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ internal OpenApiDocument Parse(YamlDocument yamlDocument)
VersionService = new OpenApiV2VersionService(Diagnostic);
doc = VersionService.LoadDocument(RootNode);
this.Diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi2_0;
ValidateRequiredFields(doc, version);
break;

case string version when version.StartsWith("3.0") || version.StartsWith("3.1"):
VersionService = new OpenApiV3VersionService(Diagnostic);
doc = VersionService.LoadDocument(RootNode);
this.Diagnostic.SpecificationVersion = OpenApiSpecVersion.OpenApi3_0;
ValidateRequiredFields(doc, version);
break;

default:
Expand Down Expand Up @@ -244,5 +246,21 @@ public void PopLoop(string loopid)
}
}

private void ValidateRequiredFields(OpenApiDocument doc, string version)
{
if ((version == "2.0" || version.StartsWith("3.0")) && (doc.Paths == null || doc.Paths.Count == 0))
{
// paths is a required field in OpenAPI 3.0 but optional in 3.1
RootNode.Context.Diagnostic.Errors.Add(new OpenApiError("", $"Paths is a REQUIRED field at {RootNode.Context.GetLocation()}"));
}
else if (version.StartsWith("3.1"))
{
if ((doc.Paths == null || doc.Paths.Count == 0) && (doc.Webhooks == null || doc.Webhooks.Count == 0))
{
RootNode.Context.Diagnostic.Errors.Add(new OpenApiError(
"", $"The document MUST contain either a Paths or Webhooks field at {RootNode.Context.GetLocation()}"));
}
}
}
}
}
12 changes: 0 additions & 12 deletions src/Microsoft.OpenApi.Readers/V3/OpenApiDocumentDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,8 @@ public static OpenApiDocument LoadOpenApi(RootNode rootNode)
var openApiNode = rootNode.GetMap();

ParseMap(openApiNode, openApidoc, _openApiFixedFields, _openApiPatternFields);
ValidatePathsField(openApidoc, rootNode);

return openApidoc;
}

private static void ValidatePathsField(OpenApiDocument doc, RootNode rootNode)
{
var versionNode = rootNode.Find(new JsonPointer("/openapi")).GetScalarValue();
if (versionNode == null) return;
else if (versionNode.Contains("3.0") && doc.Paths == null)
{
// paths is a required field in OpenAPI 3.0 but optional in 3.1
rootNode.Context.Diagnostic.Errors.Add(new OpenApiError("", $"Paths is a REQUIRED field at {rootNode.Context.GetLocation()}"));
}
}
}
}