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

.NET 8.0 JsonArray.Add throws System.InvalidOperationException: "The element cannot be an object or array." #94842

Closed
campersau opened this issue Nov 16, 2023 · 2 comments · Fixed by #94854
Assignees
Milestone

Comments

@campersau
Copy link
Contributor

Description

Adding a JsonElement via JsonArray.Add throws an exception with .NET 8. In .NET 7 it worked without throwing.

It is interesting that JsonArray.Add worked in .NET 7 but JsonValue.Create fails in both .NET 7/8 when passing in a JsonElement with ValueKind.Object/Array.

Related: #70427
Regression in 1db4357 ?

Reproduction Steps

using System.Text.Json;
using System.Text.Json.Nodes;

var element = JsonDocument.Parse("""{"test":"hello world"}""");

var array = new JsonArray();
array.Add(element.RootElement); // .NET 8.0 throws here

Console.WriteLine(array.ToJsonString());

Expected behavior

Works without exception.

Actual behavior

Throws a System.InvalidOperationException: "The element cannot be an object or array."

   bei System.Text.Json.ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray()
   bei System.Text.Json.Nodes.JsonValue.VerifyJsonElementIsNotArrayOrObject(JsonElement& element)
   bei System.Text.Json.Nodes.JsonValue.Create[T](T value, Nullable`1 options)
   bei System.Text.Json.Nodes.JsonArray.Add[T](T value)

Regression?

Yes, it works with .NET 7.0

Known Workarounds

For this particular case the JsonDocument could be added instead of the RootElement.
A general workaround would be #70427 (comment)

Configuration

.NET 8
Windows
x64

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 16, 2023
@ghost
Copy link

ghost commented Nov 16, 2023

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Adding a JsonElement via JsonArray.Add throws an exception with .NET 8. In .NET 7 it worked without throwing.

It is interesting that JsonArray.Add worked in .NET 7 but JsonValue.Create fails in both .NET 7/8 when passing in a JsonElement with ValueKind.Object/Array.

Related: #70427
Regression in 1db4357 ?

Reproduction Steps

using System.Text.Json;
using System.Text.Json.Nodes;

var element = JsonDocument.Parse("""{"test":"hello world"}""");

var array = new JsonArray();
array.Add(element.RootElement); // .NET 8.0 throws here

Console.WriteLine(array.ToJsonString());

Expected behavior

Works without exception.

Actual behavior

Throws a System.InvalidOperationException: "The element cannot be an object or array."

   bei System.Text.Json.ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray()
   bei System.Text.Json.Nodes.JsonValue.VerifyJsonElementIsNotArrayOrObject(JsonElement& element)
   bei System.Text.Json.Nodes.JsonValue.Create[T](T value, Nullable`1 options)
   bei System.Text.Json.Nodes.JsonArray.Add[T](T value)

Regression?

Yes, it works with .NET 7.0

Known Workarounds

For this particular case the JsonDocument could be added instead of the RootElement.
A general workaround would be #70427 (comment)

Configuration

.NET 8
Windows
x64

Other information

No response

Author: campersau
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Nov 16, 2023
@eiriktsarpalis eiriktsarpalis added this to the 8.0.x milestone Nov 16, 2023
@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Nov 16, 2023

It seems that the particular example was working until .NET 7 because the method was creating a JsonValue instance directly:

JsonNode jNode = value as JsonNode ?? new JsonValueNotTrimmable<T>(value);

Bypassing the usual validation in place when creating JsonValue instances from JsonElement:

The validation is in place because JsonValue instances do not support nesting, so if you try to pass in anything more complex that won't be inspectible by the DOM APIs (although it would still generated valid JSON).

Nevertheless, we should try to fix this because it's a regression. I'll follow up with a PR that both fixes the problem and ensures JsonElements are mapped to the correct JsonNode representation.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Nov 16, 2023
@eiriktsarpalis eiriktsarpalis self-assigned this Nov 16, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Nov 16, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants