Skip to content

Commit 712b6d4

Browse files
committed
- Allow OpenApiNull as the data when schema specifies nullable == true
- Note that this applies to all data types, even arrays and objects. Per JSON schema, objects, arrays, strings, etc. are separate from the null types. If the schema wants to allow the objects, arrays, etc. to be nullable, the nullable boolean must be specifically set to true. (In JSON schema, this is done by using type : ["array", "null"], which is not allowed in OpenAPI spec). Reference: At its core, JSON Schema defines the following basic types: string Numeric types object array boolean null Primitive data types in the OAS are based on the types supported by the JSON Schema Specification Wright Draft 00. Note that integer as a type is also supported and is defined as a JSON number without a fraction or exponent part. null is not supported as a type (see nullable for an alternative solution). Models are defined using the Schema Object, which is an extended subset of JSON Schema Specification Wright Draft 00.
1 parent c08eb18 commit 712b6d4

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

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

Lines changed: 19 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
{
@@ -103,6 +114,11 @@ public static void ValidateDataTypeMismatch(
103114
return;
104115
}
105116

117+
if (value is OpenApiNull)
118+
{
119+
return;
120+
}
121+
106122
// If value is not a string and also not an array, there is a data mismatch.
107123
if (!(value is OpenApiArray))
108124
{

0 commit comments

Comments
 (0)