Skip to content

Commit 34b81ce

Browse files
Merge pull request #1888 from microsoft/mk/remove-validation-rule
Remove DataTypeMismatch validation rule from the default ruleset
2 parents 6987f7b + a478bd2 commit 34b81ce

13 files changed

+151
-341
lines changed

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

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

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

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text.Json.Nodes;
7+
using Microsoft.OpenApi.Models;
8+
9+
namespace Microsoft.OpenApi.Validations.Rules
10+
{
11+
/// <summary>
12+
/// Defines a non-default set of rules for validating examples in header, media type and parameter objects against the schema
13+
/// </summary>
14+
public static class OpenApiNonDefaultRules
15+
{
16+
/// <summary>
17+
/// Validate the data matches with the given data type.
18+
/// </summary>
19+
public static ValidationRule<OpenApiHeader> HeaderMismatchedDataType =>
20+
new(nameof(HeaderMismatchedDataType),
21+
(context, header) =>
22+
{
23+
ValidateMismatchedDataType(context, nameof(HeaderMismatchedDataType), header.Example, header.Examples, header.Schema);
24+
});
25+
26+
/// <summary>
27+
/// Validate the data matches with the given data type.
28+
/// </summary>
29+
public static ValidationRule<OpenApiMediaType> MediaTypeMismatchedDataType =>
30+
new(nameof(MediaTypeMismatchedDataType),
31+
(context, mediaType) =>
32+
{
33+
ValidateMismatchedDataType(context, nameof(MediaTypeMismatchedDataType), mediaType.Example, mediaType.Examples, mediaType.Schema);
34+
});
35+
36+
/// <summary>
37+
/// Validate the data matches with the given data type.
38+
/// </summary>
39+
public static ValidationRule<OpenApiParameter> ParameterMismatchedDataType =>
40+
new(nameof(ParameterMismatchedDataType),
41+
(context, parameter) =>
42+
{
43+
ValidateMismatchedDataType(context, nameof(ParameterMismatchedDataType), parameter.Example, parameter.Examples, parameter.Schema);
44+
});
45+
46+
/// <summary>
47+
/// Validate the data matches with the given data type.
48+
/// </summary>
49+
public static ValidationRule<OpenApiSchema> SchemaMismatchedDataType =>
50+
new(nameof(SchemaMismatchedDataType),
51+
(context, schema) =>
52+
{
53+
// default
54+
context.Enter("default");
55+
56+
if (schema.Default != null)
57+
{
58+
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Default, schema);
59+
}
60+
61+
context.Exit();
62+
63+
// example
64+
context.Enter("example");
65+
66+
if (schema.Example != null)
67+
{
68+
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Example, schema);
69+
}
70+
71+
context.Exit();
72+
73+
// enum
74+
context.Enter("enum");
75+
76+
if (schema.Enum != null)
77+
{
78+
for (var i = 0; i < schema.Enum.Count; i++)
79+
{
80+
context.Enter(i.ToString());
81+
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Enum[i], schema);
82+
context.Exit();
83+
}
84+
}
85+
86+
context.Exit();
87+
});
88+
89+
private static void ValidateMismatchedDataType(IValidationContext context,
90+
string ruleName,
91+
JsonNode example,
92+
IDictionary<string, OpenApiExample> examples,
93+
OpenApiSchema schema)
94+
{
95+
// example
96+
context.Enter("example");
97+
98+
if (example != null)
99+
{
100+
RuleHelpers.ValidateDataTypeMismatch(context, ruleName, example, schema);
101+
}
102+
103+
context.Exit();
104+
105+
// enum
106+
context.Enter("examples");
107+
108+
if (examples != null)
109+
{
110+
foreach (var key in examples.Keys.Where(k => examples[k] != null))
111+
{
112+
context.Enter(key);
113+
context.Enter("value");
114+
RuleHelpers.ValidateDataTypeMismatch(context, ruleName, examples[key]?.Value, schema);
115+
context.Exit();
116+
context.Exit();
117+
}
118+
}
119+
120+
context.Exit();
121+
}
122+
}
123+
}

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

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,45 +58,6 @@ public static class OpenApiParameterRules
5858
context.Exit();
5959
});
6060

61-
/// <summary>
62-
/// Validate the data matches with the given data type.
63-
/// </summary>
64-
public static ValidationRule<OpenApiParameter> ParameterMismatchedDataType =>
65-
new(nameof(ParameterMismatchedDataType),
66-
(context, parameter) =>
67-
{
68-
// example
69-
context.Enter("example");
70-
71-
if (parameter.Example != null)
72-
{
73-
RuleHelpers.ValidateDataTypeMismatch(context, nameof(ParameterMismatchedDataType), parameter.Example, parameter.Schema);
74-
}
75-
76-
context.Exit();
77-
78-
// examples
79-
context.Enter("examples");
80-
81-
if (parameter.Examples != null)
82-
{
83-
foreach (var key in parameter.Examples.Keys)
84-
{
85-
if (parameter.Examples[key] != null)
86-
{
87-
context.Enter(key);
88-
context.Enter("value");
89-
RuleHelpers.ValidateDataTypeMismatch(context,
90-
nameof(ParameterMismatchedDataType), parameter.Examples[key]?.Value, parameter.Schema);
91-
context.Exit();
92-
context.Exit();
93-
}
94-
}
95-
}
96-
97-
context.Exit();
98-
});
99-
10061
/// <summary>
10162
/// Validate that a path parameter should always appear in the path
10263
/// </summary>

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

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,6 @@ namespace Microsoft.OpenApi.Validations.Rules
1313
[OpenApiRule]
1414
public static class OpenApiSchemaRules
1515
{
16-
/// <summary>
17-
/// Validate the data matches with the given data type.
18-
/// </summary>
19-
public static ValidationRule<OpenApiSchema> SchemaMismatchedDataType =>
20-
new(nameof(SchemaMismatchedDataType),
21-
(context, schema) =>
22-
{
23-
// default
24-
context.Enter("default");
25-
26-
if (schema.Default != null)
27-
{
28-
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Default, schema);
29-
}
30-
31-
context.Exit();
32-
33-
// example
34-
context.Enter("example");
35-
36-
if (schema.Example != null)
37-
{
38-
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Example, schema);
39-
}
40-
41-
context.Exit();
42-
43-
// enum
44-
context.Enter("enum");
45-
46-
if (schema.Enum != null)
47-
{
48-
for (var i = 0; i < schema.Enum.Count; i++)
49-
{
50-
context.Enter(i.ToString());
51-
RuleHelpers.ValidateDataTypeMismatch(context, nameof(SchemaMismatchedDataType), schema.Enum[i], schema);
52-
context.Exit();
53-
}
54-
}
55-
56-
context.Exit();
57-
});
58-
5916
/// <summary>
6017
/// Validates Schema Discriminator
6118
/// </summary>

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System;
54
using System.Text.Json;
65
using System.Text.Json.Nodes;
7-
using Microsoft.OpenApi.Any;
86
using Microsoft.OpenApi.Models;
97

108
namespace Microsoft.OpenApi.Validations.Rules

src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-

2-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
32
// Licensed under the MIT license.
43

54
using System;
@@ -9,7 +8,6 @@
98
using Microsoft.OpenApi.Exceptions;
109
using Microsoft.OpenApi.Properties;
1110
using Microsoft.OpenApi.Validations.Rules;
12-
using System.Data;
1311

1412
namespace Microsoft.OpenApi.Validations
1513
{
@@ -329,17 +327,15 @@ internal static PropertyInfo[] GetValidationRuleTypes()
329327
..typeof(OpenApiExternalDocsRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
330328
..typeof(OpenApiInfoRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
331329
..typeof(OpenApiLicenseRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
332-
..typeof(OpenApiMediaTypeRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
333330
..typeof(OpenApiOAuthFlowRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
334331
..typeof(OpenApiServerRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
335332
..typeof(OpenApiResponseRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
336333
..typeof(OpenApiResponsesRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
337334
..typeof(OpenApiSchemaRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
338-
..typeof(OpenApiHeaderRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
339335
..typeof(OpenApiTagRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
340336
..typeof(OpenApiPathsRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
341-
..typeof(OpenApiParameterRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
342-
];
337+
..typeof(OpenApiParameterRules).GetProperties(BindingFlags.Static | BindingFlags.Public)
338+
];
343339
}
344340
}
345341
}

0 commit comments

Comments
 (0)