Skip to content

Commit b12a2f3

Browse files
authored
Merge pull request #399 from Microsoft/perthcharern/AllowOpenApiNullForNullableSchema
Allow null as a valid data when schema.nullable is set to true
2 parents 8299ed0 + 224bc86 commit b12a2f3

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public static bool IsEmailAddress(this string input)
4040
}
4141

4242
public static void ValidateDataTypeMismatch(
43-
IValidationContext context,
44-
string ruleName,
45-
IOpenApiAny value,
43+
IValidationContext context,
44+
string ruleName,
45+
IOpenApiAny value,
4646
OpenApiSchema schema)
4747
{
4848
if (schema == null)
@@ -52,6 +52,17 @@ public static void ValidateDataTypeMismatch(
5252

5353
var type = schema.Type;
5454
var format = schema.Format;
55+
var nullable = schema.Nullable;
56+
57+
// Before checking the type, check first if the schema allows null.
58+
// If so and the data given is also null, this is allowed for any type.
59+
if (nullable)
60+
{
61+
if (value is OpenApiNull)
62+
{
63+
return;
64+
}
65+
}
5566

5667
if (type == "object")
5768
{

0 commit comments

Comments
 (0)