-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathOpenApiHeaderValidationTests.cs
More file actions
98 lines (88 loc) · 3.01 KB
/
OpenApiHeaderValidationTests.cs
File metadata and controls
98 lines (88 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Xunit;
namespace Microsoft.OpenApi.Validations.Tests
{
public class OpenApiHeaderValidationTests
{
[Fact]
public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema()
{
// Arrange
var header = new OpenApiHeader
{
Required = true,
Example = 55,
Schema = new OpenApiSchema
{
Type = JsonSchemaType.String
}
};
// Act
var defaultRuleSet = ValidationRuleSet.GetDefaultRuleSet();
defaultRuleSet.Add(typeof(IOpenApiHeader), OpenApiNonDefaultRules.HeaderMismatchedDataType);
var validator = new OpenApiValidator(defaultRuleSet);
var walker = new OpenApiWalker(validator);
walker.Walk((IOpenApiHeader)header);
// Assert
Assert.NotEmpty(validator.Warnings);
}
[Fact]
public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema()
{
// Arrange
IEnumerable<OpenApiError> warnings;
var header = new OpenApiHeader
{
Required = true,
Schema = new OpenApiSchema
{
Type = JsonSchemaType.Object,
AdditionalProperties = new OpenApiSchema
{
Type = JsonSchemaType.Integer
}
},
Examples = new Dictionary<string, IOpenApiExample>
{
["example0"] = new OpenApiExample()
{
Value = "1",
},
["example1"] = new OpenApiExample()
{
Value = new JsonObject()
{
["x"] = 2,
["y"] = "20",
["z"] = "200"
}
},
["example2"] = new OpenApiExample()
{
Value = new JsonArray(){3}
},
["example3"] = new OpenApiExample()
{
Value = new JsonObject()
{
["x"] = 4,
["y"] = 40
}
},
}
};
// Act
var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet());
var walker = new OpenApiWalker(validator);
walker.Walk((IOpenApiHeader)header);
warnings = validator.Warnings;
var result = !warnings.Any();
// Assert
Assert.True(result);
}
}
}