Skip to content

Commit bb294d7

Browse files
committed
- Add rules for header (mirror parameter)
1 parent 53c160c commit bb294d7

File tree

5 files changed

+197
-2
lines changed

5 files changed

+197
-2
lines changed

src/Microsoft.OpenApi/Validations/OpenApiValidator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public void AddError(OpenApiValidatorError error)
7777
/// <param name="item">The object to be validated</param>
7878
public override void Visit(OpenApiComponents item) => Validate(item);
7979

80+
/// <summary>
81+
/// Execute validation rules against an <see cref="OpenApiHeader"/>
82+
/// </summary>
83+
/// <param name="item">The object to be validated</param>
84+
public override void Visit(OpenApiHeader item) => Validate(item);
85+
8086
/// <summary>
8187
/// Execute validation rules against an <see cref="OpenApiResponse"/>
8288
/// </summary>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using Microsoft.OpenApi.Models;
6+
using Microsoft.OpenApi.Properties;
7+
8+
namespace Microsoft.OpenApi.Validations.Rules
9+
{
10+
/// <summary>
11+
/// The validation rules for <see cref="OpenApiHeader"/>.
12+
/// </summary>
13+
[OpenApiRule]
14+
public static class OpenApiHeaderRules
15+
{
16+
/// <summary>
17+
/// Validate the data matches with the given data type.
18+
/// </summary>
19+
public static ValidationRule<OpenApiHeader> HeaderMismatchedDataType =>
20+
new ValidationRule<OpenApiHeader>(
21+
(context, header) =>
22+
{
23+
// example
24+
context.Enter("example");
25+
26+
if (header.Example != null)
27+
{
28+
RuleHelpers.ValidateDataTypeMismatch(context, nameof(HeaderMismatchedDataType), header.Example, header.Schema);
29+
}
30+
31+
context.Exit();
32+
33+
34+
// examples
35+
context.Enter("examples");
36+
37+
if (header.Examples != null)
38+
{
39+
foreach (var key in header.Examples.Keys)
40+
{
41+
if (header.Examples[key] != null)
42+
{
43+
context.Enter(key);
44+
context.Enter("value");
45+
RuleHelpers.ValidateDataTypeMismatch(context, nameof(HeaderMismatchedDataType), header.Examples[key]?.Value, header.Schema);
46+
context.Exit();
47+
context.Exit();
48+
}
49+
}
50+
}
51+
52+
context.Exit();
53+
});
54+
55+
// add more rule.
56+
}
57+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static class OpenApiParameterRules
7676
context.Exit();
7777

7878

79-
// enum
79+
// examples
8080
context.Enter("examples");
8181

8282
if (parameter.Examples != null)
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using FluentAssertions;
8+
using Microsoft.OpenApi.Any;
9+
using Microsoft.OpenApi.Extensions;
10+
using Microsoft.OpenApi.Models;
11+
using Microsoft.OpenApi.Properties;
12+
using Microsoft.OpenApi.Services;
13+
using Microsoft.OpenApi.Validations.Rules;
14+
using Xunit;
15+
16+
namespace Microsoft.OpenApi.Validations.Tests
17+
{
18+
public class OpenApiHeaderValidationTests
19+
{
20+
[Fact]
21+
public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema()
22+
{
23+
// Arrange
24+
IEnumerable<OpenApiError> errors;
25+
var header = new OpenApiHeader()
26+
{
27+
Required = true,
28+
Example = new OpenApiInteger(55),
29+
Schema = new OpenApiSchema()
30+
{
31+
Type = "string",
32+
}
33+
};
34+
35+
// Act
36+
var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet());
37+
var walker = new OpenApiWalker(validator);
38+
walker.Walk(header);
39+
40+
errors = validator.Errors;
41+
bool result = !errors.Any();
42+
43+
// Assert
44+
result.Should().BeFalse();
45+
errors.Select(e => e.Message).Should().BeEquivalentTo(new[]
46+
{
47+
RuleHelpers.DataTypeMismatchedErrorMessage
48+
});
49+
errors.Select(e => e.Pointer).Should().BeEquivalentTo(new[]
50+
{
51+
"#/example",
52+
});
53+
}
54+
55+
[Fact]
56+
public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema()
57+
{
58+
// Arrange
59+
IEnumerable<OpenApiError> errors;
60+
61+
var header = new OpenApiHeader()
62+
{
63+
Required = true,
64+
Schema = new OpenApiSchema()
65+
{
66+
Type = "object",
67+
AdditionalProperties = new OpenApiSchema()
68+
{
69+
Type = "integer",
70+
}
71+
},
72+
Examples =
73+
{
74+
["example0"] = new OpenApiExample()
75+
{
76+
Value = new OpenApiString("1"),
77+
},
78+
["example1"] = new OpenApiExample()
79+
{
80+
Value = new OpenApiObject()
81+
{
82+
["x"] = new OpenApiInteger(2),
83+
["y"] = new OpenApiString("20"),
84+
["z"] = new OpenApiString("200")
85+
}
86+
},
87+
["example2"] = new OpenApiExample()
88+
{
89+
Value =
90+
new OpenApiArray()
91+
{
92+
new OpenApiInteger(3)
93+
}
94+
},
95+
["example3"] = new OpenApiExample()
96+
{
97+
Value = new OpenApiObject()
98+
{
99+
["x"] = new OpenApiInteger(4),
100+
["y"] = new OpenApiInteger(40),
101+
}
102+
},
103+
}
104+
};
105+
106+
// Act
107+
var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet());
108+
var walker = new OpenApiWalker(validator);
109+
walker.Walk(header);
110+
111+
errors = validator.Errors;
112+
bool result = !errors.Any();
113+
114+
// Assert
115+
result.Should().BeFalse();
116+
errors.Select(e => e.Message).Should().BeEquivalentTo(new[]
117+
{
118+
RuleHelpers.DataTypeMismatchedErrorMessage,
119+
RuleHelpers.DataTypeMismatchedErrorMessage,
120+
RuleHelpers.DataTypeMismatchedErrorMessage,
121+
});
122+
errors.Select(e => e.Pointer).Should().BeEquivalentTo(new[]
123+
{
124+
// #enum/0 is not an error since the spec allows
125+
// representing an object using a string.
126+
"#/examples/example1/value/y",
127+
"#/examples/example1/value/z",
128+
"#/examples/example2/value"
129+
});
130+
}
131+
}
132+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void DefaultRuleSetPropertyReturnsTheCorrectRules()
4343
Assert.NotEmpty(rules);
4444

4545
// Update the number if you add new default rule(s).
46-
Assert.Equal(19, rules.Count);
46+
Assert.Equal(20, rules.Count);
4747
}
4848
}
4949
}

0 commit comments

Comments
 (0)