Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null value handling in OpenApiAnyFactory. #2102

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ private static IOpenApiAny CreateFromJsonElement(JsonElement jsonElement)
if (jsonElement.ValueKind == JsonValueKind.Object)
return CreateOpenApiObject(jsonElement);

if (jsonElement.ValueKind == JsonValueKind.Null || jsonElement.ValueKind == JsonValueKind.Undefined)
return new OpenApiNull();

throw new System.ArgumentException($"Unsupported value kind {jsonElement.ValueKind}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ public class OpenApiAnyFactoryTests
[InlineData("\"abc\"", typeof(OpenApiString), "abc")]
[InlineData("true", typeof(OpenApiBoolean), true)]
[InlineData("false", typeof(OpenApiBoolean), false)]
[InlineData("null", typeof(OpenApiNull), null)]
public void CreateFromJson_SimpleType(string json, Type expectedType, object expectedValue)
{
var openApiAnyObject = OpenApiAnyFactory.CreateFromJson(json);
Assert.NotNull(openApiAnyObject);
Assert.Equal(expectedType, openApiAnyObject.GetType());
Assert.Equal(AnyType.Primitive, openApiAnyObject.AnyType);
var valueProperty = expectedType.GetProperty("Value");
var actualValue = valueProperty.GetValue(openApiAnyObject);
Assert.Equal(expectedValue, actualValue);
if (expectedType == typeof(OpenApiNull))
{
Assert.Equal(AnyType.Null, openApiAnyObject.AnyType);
}
else
{
Assert.Equal(AnyType.Primitive, openApiAnyObject.AnyType);
var valueProperty = expectedType.GetProperty("Value");
var actualValue = valueProperty.GetValue(openApiAnyObject);
Assert.Equal(expectedValue, actualValue);
}
}

[Theory]
Expand All @@ -35,6 +43,7 @@ public void CreateFromJson_SimpleType(string json, Type expectedType, object exp
[InlineData("[true,false]", typeof(OpenApiBoolean), true, false)]
[InlineData("[{\"a\":1,\"b\":2},{\"a\":3,\"b\":4}]", typeof(OpenApiObject))]
[InlineData("[[1,2],[3,4]]", typeof(OpenApiArray))]
[InlineData("[null]", typeof(OpenApiNull))]
public void CreateFromJson_Array(string json, Type expectedType, params object[] expectedValues)
{
var openApiAnyObject = OpenApiAnyFactory.CreateFromJson(json);
Expand Down Expand Up @@ -73,7 +82,8 @@ public void CreateFromJson_Object()
{
a = 1,
b = 2
}
},
null_value = (object) null
});

var openApiAnyObject = OpenApiAnyFactory.CreateFromJson(json);
Expand Down Expand Up @@ -124,6 +134,10 @@ public void CreateFromJson_Object()
Assert.NotNull(obj["object_value"]);
Assert.Equal(typeof(OpenApiObject), obj["object_value"].GetType());
Assert.Equal(AnyType.Object, obj["object_value"].AnyType);

Assert.NotNull(obj["null_value"]);
Assert.Equal(typeof(OpenApiNull), obj["null_value"].GetType());
Assert.Equal(AnyType.Null, obj["null_value"].AnyType);
}
}
}