Skip to content

Commit acd1a45

Browse files
committed
Fixed extension tests
1 parent 824e3e2 commit acd1a45

File tree

4 files changed

+64
-19
lines changed

4 files changed

+64
-19
lines changed

src/Microsoft.OpenApi/Services/OpenApiVisitorBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public abstract class OpenApiVisitorBase
1919
/// <summary>
2020
/// Allow Rule to indicate validation error occured a deeper context level.
2121
/// </summary>
22-
/// <param name="segment"></param>
22+
/// <param name="segment">Identifier for context</param>
2323
public void Enter(string segment)
2424
{
2525
this._path.Push(segment);

src/Microsoft.OpenApi/Validations/IValidationContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface IValidationContext
2323
/// <summary>
2424
/// Allow Rule to indicate validation error occured a deeper context level.
2525
/// </summary>
26-
/// <param name="segment"></param>
26+
/// <param name="segment">Identifier for context</param>
2727
void Enter(string segment);
2828

2929
/// <summary>

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,15 @@ public void ParseCustomExtension()
2828
baz: hi!
2929
paths: {}
3030
";
31-
var ruleset = Validations.ValidationRuleSet.DefaultRuleSet;
32-
ruleset.Add(
33-
new ValidationRule<FooExtension>(
34-
(context, item) =>
35-
{
36-
if (item.Bar == "hey")
37-
{
38-
context.AddError(new ValidationError(ErrorReason.Format, context.PathString, "Don't say hey"));
39-
}
40-
}));
41-
42-
var settings = new OpenApiReaderSettings()
31+
var settings = new OpenApiReaderSettings()
4332
{
4433
ExtensionParsers = { { "x-foo", (a) => {
4534
var fooNode = (OpenApiObject)a;
4635
return new FooExtension() {
4736
Bar = (fooNode["bar"] as OpenApiString)?.Value,
4837
Baz = (fooNode["baz"] as OpenApiString)?.Value
4938
};
50-
} } },
51-
RuleSet = ruleset
39+
} } }
5240
};
5341

5442
var reader = new OpenApiStringReader(settings);
@@ -61,9 +49,6 @@ public void ParseCustomExtension()
6149
fooExtension.Should().NotBeNull();
6250
fooExtension.Bar.Should().Be("hey");
6351
fooExtension.Baz.Should().Be("hi!");
64-
var error = diag.Errors.First();
65-
error.Message.Should().Be("Don't say hey");
66-
error.Pointer.Should().Be("#/info/x-foo");
6752
}
6853
}
6954

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
using System.Collections.Generic;
66
using FluentAssertions;
77
using Microsoft.OpenApi.Exceptions;
8+
using Microsoft.OpenApi.Interfaces;
89
using Microsoft.OpenApi.Models;
910
using Microsoft.OpenApi.Properties;
1011
using Microsoft.OpenApi.Services;
1112
using Microsoft.OpenApi.Validations;
13+
using Microsoft.OpenApi.Writers;
1214
using Xunit;
1315

1416
namespace Microsoft.OpenApi.Tests.Services
@@ -85,5 +87,63 @@ public void ServersShouldBeReferencedByIndex()
8587
});
8688
}
8789

90+
91+
[Fact]
92+
public void ValidateCustomExtension()
93+
{
94+
95+
var ruleset = Validations.ValidationRuleSet.DefaultRuleSet;
96+
ruleset.Add(
97+
new ValidationRule<FooExtension>(
98+
(context, item) =>
99+
{
100+
if (item.Bar == "hey")
101+
{
102+
context.AddError(new ValidationError(ErrorReason.Format, context.PathString, "Don't say hey"));
103+
}
104+
}));
105+
106+
107+
var openApiDocument = new OpenApiDocument();
108+
openApiDocument.Info = new OpenApiInfo()
109+
{
110+
Title = "foo",
111+
Version = "1.2.2"
112+
};
113+
114+
var fooExtension = new FooExtension()
115+
{
116+
Bar = "hey",
117+
Baz = "baz"
118+
};
119+
120+
openApiDocument.Info.Extensions.Add("x-foo",fooExtension);
121+
122+
var validator = new OpenApiValidator(ruleset);
123+
var walker = new OpenApiWalker(validator);
124+
walker.Walk(openApiDocument);
125+
126+
validator.Errors.ShouldBeEquivalentTo(
127+
new List<ValidationError>
128+
{
129+
new ValidationError(ErrorReason.Format, "#/info/x-foo", "Don't say hey")
130+
});
131+
}
132+
133+
}
134+
135+
public class FooExtension : IOpenApiExtension, IOpenApiElement
136+
{
137+
public string Baz { get; set; }
138+
139+
public string Bar { get; set; }
140+
141+
public void Write(IOpenApiWriter writer)
142+
{
143+
writer.WriteStartObject();
144+
writer.WriteProperty("baz", Baz);
145+
writer.WriteProperty("bar", Bar);
146+
writer.WriteEndObject();
147+
}
88148
}
89149
}

0 commit comments

Comments
 (0)