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

System.Text.Json Serializer ignores JsonIgnore when generating JsonTypeInfo #76794

Closed
hopperpl opened this issue Oct 9, 2022 · 2 comments
Closed

Comments

@hopperpl
Copy link

hopperpl commented Oct 9, 2022

Description

The JSON type info generator triggers an exception when trying to serialize a object with a field that is marked as JsonIgnore but has no callable type constructor available.

System.ArgumentException: 'The type 'System.ReadOnlySpan`1[System.Byte]&' may not be used as a type argument.'

The reason is that the only constructor of class Secret (see code below) has a ref ROS field. And to date, Reflection.Emit cannot invoke such methods. That is all reasonable, but the serializer was explicitly instructed to ignore that field. The documentation is rather strict on stating that such fields are always ignored if attributed that way.

The deserializer should also never set this field, therefore never require to actually create any object and invoke any constructor.

My understanding is that a field/property marked as JsonIgnore is never touched or set by the serializer/deserializer. And when an object containing such a field is created, the field is never modified either.

Reproduction Steps

using System.Text.Json;
using System.Text.Json.Serialization;

var Obj     = new Container();
var ObjText = JsonSerializer.Serialize(Obj, new JsonSerializerOptions { IncludeFields = true });

class Container
{
  public ulong  A;
  public ulong  B;

  [JsonIgnore(Condition = JsonIgnoreCondition.Always)]
  public Secret? C;
}

class Secret
{
  public ulong Code;

  public Secret(in ReadOnlySpan<byte> payload)
  {
  }
}

Expected behavior

no exception

Actual behavior

System.ArgumentException: 'The type 'System.ReadOnlySpan`1[System.Byte]&' may not be used as a type argument.'

Bonus: it would be nice if json could aggregate that exception and rethrow it upstream with the problematic field name added.

>	[Exception] System.Private.CoreLib.dll!System.RuntimeType.ThrowIfTypeNeverValidGenericArgument(System.RuntimeType type) Line 871	C#
 	[Exception] System.Private.CoreLib.dll!System.RuntimeType.SanityCheckGenericArguments(System.RuntimeType[] genericArguments, System.RuntimeType[] genericParameters) Line 879	C#
 	[Exception] System.Private.CoreLib.dll!System.RuntimeType.MakeGenericType(System.Type[] instantiation) Line 3513	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Converters.ObjectConverterFactory.CreateConverter(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) Line 84	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.Serialization.JsonConverterFactory.GetConverterInternal(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) Line 50	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetConverterForType(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options, bool resolveJsonConverterAttribute) Line 156	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.CreateJsonTypeInfo(System.Type type, System.Text.Json.JsonSerializerOptions options) Line 89	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetTypeInfo(System.Type type, System.Text.Json.JsonSerializerOptions options) Line 71	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.JsonSerializerOptions.GetTypeInfoNoCaching(System.Type type) Line 651	C#
        ...

Regression?

not to my knowledge

Known Workarounds

create a dummy, second parameterless public constructor, that triggers an exception when ever being called

Configuration

7.0.0-rc.1.22427.1

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Oct 9, 2022
@ghost
Copy link

ghost commented Oct 9, 2022

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

The JSON type info generator triggers an exception when trying to serialize a object with a field that is marked as JsonIgnore but has no callable type constructor available.

System.ArgumentException: 'The type 'System.ReadOnlySpan`1[System.Byte]&' may not be used as a type argument.'

The reason is that the only constructor of class Secret (see code below) has a ref ROS field. And to date, Reflection.Emit cannot invoke such methods. That is all reasonable, but the serializer was explicitly instructed to ignore that field. The documentation is rather strict on stating that such fields are always ignored if attributed that way.

The deserializer should also never set this field, therefore never require to actually create any object and invoke any constructor.

My understanding is that a field/property marked as JsonIgnore is never touched or set by the serializer/deserializer. And when an object containing such a field is created, the field is never modified either.

Reproduction Steps

using System.Text.Json;
using System.Text.Json.Serialization;

var Obj     = new Container();
var ObjText = JsonSerializer.Serialize(Obj, new JsonSerializerOptions { IncludeFields = true });

class Container
{
  public ulong  A;
  public ulong  B;

  [JsonIgnore(Condition = JsonIgnoreCondition.Always)]
  public Secret? C;
}

class Secret
{
  public ulong Code;

  public Secret(in ReadOnlySpan<byte> payload)
  {
  }
}

Expected behavior

no exception

Actual behavior

System.ArgumentException: 'The type 'System.ReadOnlySpan`1[System.Byte]&' may not be used as a type argument.'

Bonus: it would be nice if json could aggregate that exception and rethrow it upstream with the problematic field name added.

>	[Exception] System.Private.CoreLib.dll!System.RuntimeType.ThrowIfTypeNeverValidGenericArgument(System.RuntimeType type) Line 871	C#
 	[Exception] System.Private.CoreLib.dll!System.RuntimeType.SanityCheckGenericArguments(System.RuntimeType[] genericArguments, System.RuntimeType[] genericParameters) Line 879	C#
 	[Exception] System.Private.CoreLib.dll!System.RuntimeType.MakeGenericType(System.Type[] instantiation) Line 3513	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Converters.ObjectConverterFactory.CreateConverter(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) Line 84	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.Serialization.JsonConverterFactory.GetConverterInternal(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) Line 50	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetConverterForType(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options, bool resolveJsonConverterAttribute) Line 156	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.CreateJsonTypeInfo(System.Type type, System.Text.Json.JsonSerializerOptions options) Line 89	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetTypeInfo(System.Type type, System.Text.Json.JsonSerializerOptions options) Line 71	C#
 	[Exception] System.Text.Json.dll!System.Text.Json.JsonSerializerOptions.GetTypeInfoNoCaching(System.Type type) Line 651	C#
        ...

Regression?

not to my knowledge

Known Workarounds

create a dummy, second parameterless public constructor, that triggers an exception when ever being called

Configuration

7.0.0-rc.1.22427.1

Other information

No response

Author: hopperpl
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

Appears to be a duplicate of #76807.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Oct 10, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Nov 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants