Skip to content

Commit e9d2569

Browse files
committed
Validation fixes based on PR review
1 parent 7e1f151 commit e9d2569

File tree

7 files changed

+83
-40
lines changed

7 files changed

+83
-40
lines changed

src/Microsoft.OpenApi/Interfaces/IOpenApiExtension.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using Microsoft.OpenApi.Writers;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using Microsoft.OpenApi.Writers;
75

86
namespace Microsoft.OpenApi.Interfaces
97
{

src/Microsoft.OpenApi/Services/OpenApiVisitorBase.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Microsoft.OpenApi.Services
1111
{
1212
/// <summary>
13-
/// Open API visitor base providing base validation logic for each <see cref="IOpenApiElement"/>
13+
/// Open API visitor base provides common logic for concrete vistors
1414
/// </summary>
1515
public abstract class OpenApiVisitorBase
1616
{
@@ -285,5 +285,11 @@ public virtual void Visit(IOpenApiExtension openApiExtension)
285285
{
286286
}
287287

288+
/// <summary>
289+
/// Visits list of <see cref="OpenApiExample"/>
290+
/// </summary>
291+
public virtual void Visit(IList<OpenApiExample> example)
292+
{
293+
}
288294
}
289295
}

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,15 @@ internal void Walk(IList<OpenApiTag> tags)
5151
_visitor.Enter(OpenApiConstants.Tags);
5252
_visitor.Visit(tags);
5353

54-
foreach (var tag in tags)
54+
// Visit tags
55+
if (tags != null)
5556
{
56-
Walk(tag);
57+
for (int i = 0; i < tags.Count; i++)
58+
{
59+
_visitor.Enter(i.ToString());
60+
Walk(tags[i]);
61+
_visitor.Exit();
62+
}
5763
}
5864
_visitor.Exit();
5965

@@ -91,7 +97,7 @@ internal void Walk(OpenApiPaths paths)
9197
_visitor.Visit(paths);
9298
foreach (var pathItem in paths)
9399
{
94-
_visitor.Enter(pathItem.Key.Replace("/","~1"));
100+
_visitor.Enter(pathItem.Key.Replace("/","~1")); // JSON Pointer uses ~1 as an escape character for /
95101
Walk(pathItem.Value);
96102
_visitor.Exit();
97103
}
@@ -110,9 +116,11 @@ internal void Walk(IList<OpenApiServer> servers)
110116
// Visit Servers
111117
if (servers != null)
112118
{
113-
foreach (var server in servers)
119+
for (int i = 0; i < servers.Count; i++)
114120
{
115-
Walk(server);
121+
_visitor.Enter(i.ToString());
122+
Walk(servers[i]);
123+
_visitor.Exit();
116124
}
117125
}
118126
_visitor.Exit();
@@ -195,11 +203,9 @@ internal void Walk(OpenApiTag tag)
195203
/// <param name="server"></param>
196204
internal void Walk(OpenApiServer server)
197205
{
198-
_visitor.Enter(OpenApiConstants.Server);
199206
_visitor.Visit(server);
200207
Walk(server.Variables);
201208
_visitor.Visit(server as IOpenApiExtensible);
202-
_visitor.Exit();
203209
}
204210

205211
/// <summary>
@@ -272,16 +278,19 @@ internal void Walk(OpenApiOperation operation)
272278
/// <param name="parameters"></param>
273279
internal void Walk(IList<OpenApiParameter> parameters)
274280
{
281+
_visitor.Enter(OpenApiConstants.Parameters);
282+
_visitor.Visit(parameters);
283+
275284
if (parameters != null)
276285
{
277-
_visitor.Enter(OpenApiConstants.Parameters);
278-
_visitor.Visit(parameters);
279-
foreach (var parameter in parameters)
286+
for (int i = 0; i < parameters.Count; i++)
280287
{
281-
Walk(parameter);
288+
_visitor.Enter(i.ToString());
289+
Walk(parameters[i]);
290+
_visitor.Exit();
282291
}
283-
_visitor.Exit();
284292
}
293+
_visitor.Exit();
285294
}
286295

287296
/// <summary>
@@ -466,10 +475,20 @@ internal void Walk(OpenApiExample example)
466475
/// <param name="examples"></param>
467476
internal void Walk(IList<OpenApiExample> examples)
468477
{
469-
foreach (var item in examples)
478+
_visitor.Enter(OpenApiConstants.Examples);
479+
_visitor.Visit(examples);
480+
481+
// Visit Examples
482+
if (examples != null)
470483
{
471-
_visitor.Visit(item);
484+
for (int i = 0; i < examples.Count; i++)
485+
{
486+
_visitor.Enter(i.ToString());
487+
Walk(examples[i]);
488+
_visitor.Exit();
489+
}
472490
}
491+
_visitor.Exit();
473492
}
474493

475494
/// <summary>

src/Microsoft.OpenApi/Services/VisitorContext.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Microsoft.OpenApi/Validations/ValidationContext.cs renamed to src/Microsoft.OpenApi/Validations/IValidationContext.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace Microsoft.OpenApi.Validations
1414
/// </summary>
1515
public interface IValidationContext
1616
{
17-
1817
/// <summary>
1918
/// Register an error with the validation context.
2019
/// </summary>

src/Microsoft.OpenApi/Validations/OpenApiValidator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ public void AddError(ValidationError error)
150150
/// <param name="item">The object to be validated</param>
151151
public override void Visit(IOpenApiExtension item) => Validate(item, item.GetType());
152152

153-
153+
/// <summary>
154+
/// Execute validation rules against a list of <see cref="OpenApiExample"/>
155+
/// </summary>
156+
/// <param name="items">The object to be validated</param>
157+
public override void Visit(IList<OpenApiExample> items) => Validate(items, items.GetType());
158+
154159
private void Validate<T>(T item)
155160
{
156161
var type = typeof(T);

test/Microsoft.OpenApi.Tests/Services/OpenApiValidatorTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,38 @@ public void ResponseMustHaveADescription()
5252
String.Format(SRResource.Validation_FieldIsRequired, "description", "response"))
5353
});
5454
}
55+
56+
[Fact]
57+
public void ServersShouldBeReferencedByIndex()
58+
{
59+
var openApiDocument = new OpenApiDocument();
60+
openApiDocument.Info = new OpenApiInfo()
61+
{
62+
Title = "foo",
63+
Version = "1.2.2"
64+
};
65+
openApiDocument.Servers = new List<OpenApiServer> {
66+
new OpenApiServer
67+
{
68+
Url = "http://example.org"
69+
},
70+
new OpenApiServer
71+
{
72+
73+
}
74+
};
75+
76+
var validator = new OpenApiValidator();
77+
var walker = new OpenApiWalker(validator);
78+
walker.Walk(openApiDocument);
79+
80+
validator.Errors.ShouldBeEquivalentTo(
81+
new List<ValidationError>
82+
{
83+
new ValidationError(ErrorReason.Required, "#/servers/1/url",
84+
String.Format(SRResource.Validation_FieldIsRequired, "url", "server"))
85+
});
86+
}
87+
5588
}
5689
}

0 commit comments

Comments
 (0)