Skip to content

Commit 7b42b1b

Browse files
committed
Use rules to validate required properties instead of reader
1 parent 9f7c7f6 commit 7b42b1b

File tree

9 files changed

+40
-14
lines changed

9 files changed

+40
-14
lines changed

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Linq;
66
using Microsoft.OpenApi.Models;
77
using Microsoft.OpenApi.Readers.Interface;
8+
using Microsoft.OpenApi.Services;
9+
using Microsoft.OpenApi.Validations;
810
using SharpYaml;
911
using SharpYaml.Serialization;
1012

@@ -28,6 +30,7 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
2830
YamlDocument yamlDocument;
2931
diagnostic = new OpenApiDiagnostic();
3032

33+
// Parse the YAML/JSON
3134
try
3235
{
3336
yamlDocument = LoadYamlDocument(input);
@@ -38,8 +41,20 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
3841
return new OpenApiDocument();
3942
}
4043

44+
// Parse the OpenAPI Document
4145
context = new ParsingContext();
42-
return context.Parse(yamlDocument, diagnostic);
46+
var document = context.Parse(yamlDocument, diagnostic);
47+
48+
// Validate the document
49+
var openApiValidator = new OpenApiValidator();
50+
var walker = new OpenApiWalker(openApiValidator);
51+
walker.Walk(document);
52+
foreach (var item in openApiValidator.Errors)
53+
{
54+
diagnostic.Errors.Add(new OpenApiError(item.ErrorPath, item.ErrorMessage));
55+
}
56+
57+
return document;
4358
}
4459

4560
/// <summary>

src/Microsoft.OpenApi.Readers/V3/OpenApiDocumentDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static OpenApiDocument LoadOpenApi(RootNode rootNode)
4646

4747
ParseMap(openApiNode, openApidoc, _openApiFixedFields, _openApiPatternFields, required);
4848

49-
ReportMissing(openApiNode, required);
49+
//ReportMissing(openApiNode, required);
5050

5151
return openApidoc;
5252
}

src/Microsoft.OpenApi.Readers/V3/OpenApiV3Deserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private static void ParseMap<T>(
3333
requiredFields?.Remove(propertyNode.Name);
3434
}
3535

36-
ReportMissing(mapNode, requiredFields);
36+
//ReportMissing(mapNode, requiredFields);
3737
}
3838

3939
private static RuntimeExpression LoadRuntimeExpression(ParseNode node)

src/Microsoft.OpenApi/Extensions/OpenApiElementExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class OpenApiElementExtensions
2020
/// </summary>
2121
/// <typeparam name="T"></typeparam>
2222
/// <param name="element"></param>
23-
/// <returns></returns>
23+
/// <returns>An IEnumerable of errors. This function will never return null.</returns>
2424
public static IEnumerable<ValidationError> Validate(this IOpenApiElement element) {
2525
var validator = new OpenApiValidator();
2626
var walker = new OpenApiWalker(validator);

src/Microsoft.OpenApi/Validations/OpenApiValidator.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public OpenApiValidator(ValidationRuleSet ruleSet = null)
2929
_context = new ValidationContext(_ruleSet);
3030
}
3131

32+
/// <summary>
33+
/// Create a vistor that will validate an OpenAPIDocument using an existing ValidationContext
34+
/// </summary>
35+
/// <param name="context">Existing validation context</param>
36+
public OpenApiValidator(ValidationContext context)
37+
{
38+
_ruleSet = context.RuleSet;
39+
_context = new ValidationContext(_ruleSet);
40+
}
41+
3242
/// <summary>
3343
/// Execute validation rules against an <see cref="OpenApiDocument"/>
3444
/// </summary>

src/Microsoft.OpenApi/Validations/Rules/OpenApiInfoRules.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ internal static class OpenApiInfoRules
2020
new ValidationRule<OpenApiInfo>(
2121
(context, item) =>
2222
{
23+
context.Push("info");
24+
2325
// title
2426
context.Push("title");
2527
if (String.IsNullOrEmpty(item.Title))
2628
{
2729
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
28-
String.Format(SRResource.Validation_FieldIsRequired, "url", "info"));
30+
String.Format(SRResource.Validation_FieldIsRequired, "title", "info"));
2931
context.AddError(error);
3032
}
3133
context.Pop();
@@ -39,6 +41,8 @@ internal static class OpenApiInfoRules
3941
context.AddError(error);
4042
}
4143
context.Pop();
44+
45+
context.Pop();
4246
});
4347

4448
// add more rule.

src/Microsoft.OpenApi/Validations/ValidationContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using Microsoft.OpenApi.Validations.Rules;
78

89
namespace Microsoft.OpenApi.Validations
@@ -70,7 +71,7 @@ internal string PathString
7071
{
7172
get
7273
{
73-
return "#/" + String.Join("/", _path);
74+
return "#/" + String.Join("/", _path.Reverse());
7475
}
7576
}
7677
#endregion

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void ParseBrokenMinimalDocumentShouldYieldExpectedDiagnostic()
105105
{
106106
Errors =
107107
{
108-
new OpenApiError("#/info", "title is a required property")
108+
new OpenApiError("#/info/title", "The field 'title' in 'info' object is REQUIRED.")
109109
}
110110
});
111111
}

test/Microsoft.OpenApi.Tests/Validations/OpenApiInfoValidationTests.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using Microsoft.OpenApi.Extensions;
78
using Microsoft.OpenApi.Models;
89
using Microsoft.OpenApi.Properties;
910
using Microsoft.OpenApi.Services;
@@ -17,19 +18,14 @@ public class OpenApiInfoValidationTests
1718
public void ValidateFieldIsRequiredInInfo()
1819
{
1920
// Arrange
20-
string urlError = String.Format(SRResource.Validation_FieldIsRequired, "url", "info");
21+
string urlError = String.Format(SRResource.Validation_FieldIsRequired, "title", "info");
2122
string versionError = String.Format(SRResource.Validation_FieldIsRequired, "version", "info");
22-
IEnumerable<ValidationError> errors;
2323
OpenApiInfo info = new OpenApiInfo();
2424

2525
// Act
26-
var validator = new OpenApiValidator();
27-
var walker = new OpenApiWalker(validator);
28-
walker.Walk(info);
29-
26+
var errors = info.Validate();
3027

3128
// Assert
32-
errors = validator.Errors;
3329
bool result = !errors.Any();
3430

3531
// Assert

0 commit comments

Comments
 (0)