Writing top-level property with ODataJsonElementValue
fails with exception stream value not supported
#3162
Labels
ODataJsonElementValue
fails with exception stream value not supported
#3162
Short summary (3-5 sentences) describing the issue.
ODataMessageWriter
supports writing a top-level property (one that is not embedded in a resource). One use case for this is to write the result of an action or function call that returns a primitive value.As demonstrated in the code snippet below, when you write a top-level property with a primitive value using
ODataMessageWriter
, you usually get an object with a field calledvalue
containing the serialized value (the property's original value is ignored) and anodata.context
field.Result:
However, if the value is inside a
JsonElement
, and you useODataJsonElementValue
to store in an OData property a so:You will get the following exception
Assemblies affected
Microsoft.OData.Core
8.x
Reproduce steps
call
ODataMessageWriter.WritePropertyAsync
with anODataProperty
whose Value is anODataJsonElementValue
.Expected result
If the JsonElement is a primitive value, then it should be handled the same way as writing the corresponding
ODataPrimitiveValue
. If it's an array or object, then it's a bit tricky, maybe it could be treated as a resource or untyped?Actual result
Throws an exception (see summary above).
Additional detail
This exception occurs because OData Writer is trying to generate a context URL for the value but it doesn't know the type. Specifically, it gets thrown in
ODataContextUrlInfo.GetTypeNameForValue
, this method handles the different child types ofODataValue
, but doesn't handleODataJsonElementValue
. We should add a case to handleODataJsonElementValue
.ODataJsonElementValue
is a niche feature added in 2023 to provide an optimization for scenario where the payload input is already JSON document parsed by System Text Json (JsonElement
). If the server already knows that the contents of the parsedJsonElement
is valid OData, then it could save a lot of memory and CPU cycles to pass it down as-is to the underlyingODataUtf8JsonWriter
without breaking it down. The use case for this would be some gateway that proxies responses from an OData service.The text was updated successfully, but these errors were encountered: