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

Polymorphic Serialization - Doesn't allow typeDiscriminator to be anything but first #73693

Closed
RyanGaudion opened this issue Aug 10, 2022 · 4 comments

Comments

@RyanGaudion
Copy link

Description

I have this endpoint:

[HttpPost]
public async Task<IActionResult> Add([FromBody] BaseClass test)
{
    //...
}

and my BaseClass is annotated as follows:

[JsonPolymorphic(TypeDiscriminatorPropertyName = "resourceType")]
[JsonDerivedType(typeof(DerivedClass1), typeDiscriminator: nameof(DerivedClass1))]
[JsonDerivedType(typeof(DerivedClass2), typeDiscriminator: nameof(DerivedClass2))]

When sending a json body where resourceType is anything but the first property, I get the following error:

{
	"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
	"title": "One or more validation errors occurred.",
	"status": 400,
	"traceId": "...",
	"errors": {
		"$.resourceType": [
			"The metadata property is either not supported by the type or is not the first property in the deserialized JSON object. Path: $.resourceType | LineNumber: 2 | BytePositionInLine: 16."
		]
	}
}

Reproduction Steps

Sending this as a POST request throws the error:

{
	"prop1": 1,
	"resourceType": "DerivedClass1",
	"prop2": "test",
}

however this doesn't:

Sending this as a POST request throws the error:

{
"resourceType": "DerivedClass1",
"prop1": 1,
"prop2": "test",
}

Expected behavior

I understand the performance advantages to having the discriminator as the first property however is there a toggle/option that can be set to allow the JsonSerializer to look through the whole jsonObject first for the discriminator?

Actual behavior

....

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

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

ghost commented Aug 10, 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 this endpoint:

[HttpPost]
public async Task<IActionResult> Add([FromBody] BaseClass test)
{
    //...
}

and my BaseClass is annotated as follows:

[JsonPolymorphic(TypeDiscriminatorPropertyName = "resourceType")]
[JsonDerivedType(typeof(DerivedClass1), typeDiscriminator: nameof(DerivedClass1))]
[JsonDerivedType(typeof(DerivedClass2), typeDiscriminator: nameof(DerivedClass2))]

When sending a json body where resourceType is anything but the first property, I get the following error:

{
	"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
	"title": "One or more validation errors occurred.",
	"status": 400,
	"traceId": "...",
	"errors": {
		"$.resourceType": [
			"The metadata property is either not supported by the type or is not the first property in the deserialized JSON object. Path: $.resourceType | LineNumber: 2 | BytePositionInLine: 16."
		]
	}
}

Reproduction Steps

Sending this as a POST request throws the error:

{
	"prop1": 1,
	"resourceType": "DerivedClass1",
	"prop2": "test",
}

however this doesn't:

Sending this as a POST request throws the error:

{
"resourceType": "DerivedClass1",
"prop1": 1,
"prop2": "test",
}

Expected behavior

I understand the performance advantages to having the discriminator as the first property however is there a toggle/option that can be set to allow the JsonSerializer to look through the whole jsonObject first for the discriminator?

Actual behavior

....

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: RyanGaudion
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@RyanGaudion
Copy link
Author

FYI - Using .Net 7 Preview 7

@Tornhoof
Copy link
Contributor

See #72604

@layomia
Copy link
Contributor

layomia commented Aug 10, 2022

Duplicate of #72604.

@layomia layomia closed this as completed Aug 10, 2022
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Aug 10, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 10, 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

3 participants