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 7 Preview 7 - Polymorphic Deserialization ignores PropertyNameCaseInsensitive setting #75269

Open
Tracked by #63918
SefaOray opened this issue Sep 8, 2022 · 5 comments

Comments

@SefaOray
Copy link

SefaOray commented Sep 8, 2022

Description

I have the following class structure

[JsonPolymorphic(TypeDiscriminatorPropertyName = nameof(ActivityType))]
[JsonDerivedType(typeof(LearnSkillActivity), (int) CareerActivities.LearnSkill)]
public abstract class BaseProfessionalActivity
{
     public abstract CareerActivities ActivityType { get; }
}

public class LearnSkillActivity : BaseProfessionalActivity
{
    public string SkillId { get; set; }

    public override CareerActivities ActivityType => CareerActivities.LearnSkill;
}

Reproduction Steps

Deserializing following json doesn't work

{
  "activityType": 0,
  "skillId": "63189799bc0a84a02ffc4bc9"
}

while the following works

{
  "ActivityType": 0,
  "skillId": "63189799bc0a84a02ffc4bc9"
}

Mind that only difference is the capical A in ActivityType

Expected behavior

Deserializes correctly without error

Actual behavior

Throws error

System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported

Regression?

No response

Known Workarounds

No response

Configuration

dotnet --version
7.0.100-preview.7.22377.5

Other information

No response

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

ghost commented Sep 8, 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

I have the following class structure

[JsonPolymorphic(TypeDiscriminatorPropertyName = nameof(ActivityType))]
[JsonDerivedType(typeof(LearnSkillActivity), (int) CareerActivities.LearnSkill)]
public abstract class BaseProfessionalActivity
{
     public abstract CareerActivities ActivityType { get; }
}

public class LearnSkillActivity : BaseProfessionalActivity
{
    public string SkillId { get; set; }

    public override CareerActivities ActivityType => CareerActivities.LearnSkill;
}

Reproduction Steps

Deserializing following json doesn't work

{
  "activityType": 0,
  "skillId": "63189799bc0a84a02ffc4bc9"
}

while the following works

{
  "ActivityType": 0,
  "skillId": "63189799bc0a84a02ffc4bc9"
}

Mind that only difference is the capical A in ActivityType

Expected behavior

Deserializes correctly without error

Actual behavior

Throws error

System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported

Regression?

No response

Known Workarounds

No response

Configuration

dotnet --version
7.0.100-preview.7.22377.5

Other information

No response

Author: SefaOray
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Sep 9, 2022

I tend to agree that this is an omission that should be fixed. However, it doesn't meet the bar for a .NET 7 fix. As a workaround I would suggest hardcoding the expected casing as a string literal instead of using nameof.

@eiriktsarpalis eiriktsarpalis added enhancement Product code improvement that does NOT require public API changes/additions and removed untriaged New issue has not been triaged by the area owner labels Sep 9, 2022
@eiriktsarpalis eiriktsarpalis added this to the 8.0.0 milestone Sep 9, 2022
@eiriktsarpalis eiriktsarpalis added bug and removed enhancement Product code improvement that does NOT require public API changes/additions labels Sep 27, 2022
@eiriktsarpalis eiriktsarpalis modified the milestones: 8.0.0, Future Jan 23, 2023
@eiriktsarpalis
Copy link
Member

Moving to Future as we won't be able to work on this for 8.0

@iPilot
Copy link

iPilot commented Mar 15, 2023

Serializer should also consider case options for discriminator property on serialization too. For now, discriminator property name is written as specified in attribute or contract model configuration.

I'm also wondering, as we have generic attributes now, if it is possible to implement
JsonDerivedType<T> where T : Enum
as part of polymorphic serialization.
It is common practice to use enums for such purposes, isn't it?

@Mortana89
Copy link

This is blocking our upgrade from .net 5 using Dahomey JSON polymorphism to .net 8 STJ.
Our old frontend is using lowercase type discriminators but the new front-end/any other API integration works with camelcasing. How can we work around this? Was trying to implement a custom resolver and duplicating the derived types with lowercased discriminators, but the SDK does not allow registering the same type twice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants