aspnetcore: Asp.Net Core does not properly handle the error with polymorphic deserialization from STJ when type discriminator is not specified

Description

When type discriminator is not specified in the request, the error is not handled and 500 is returned instead of 400.

Example request 1:

{
	"type": "unknown",
	"someOtherProperty": "value"
}

Example request 2:

{
	"someOtherProperty": "value"
}

Controller action:

[HttpPost("test")]
public void Test(Base request)
{
        
}

Models:

[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(Derived), "derived")]
public abstract record Base
{
}

public record Derived : Base
{
}

Reproduction Steps

  1. Create a controller with the action and models from Description
  2. Do a call with the example request 1, response is 400
{
	"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
	"title": "One or more validation errors occurred.",
	"status": 400,
	"errors": {
		"$": [
			"Read unrecognized type discriminator id 'unknown'. Path: $ | LineNumber: 2 | BytePositionInLine: 21."
		],
		"request": [
			"The request field is required."
		]
	},
	"traceId": "00-a4e0c60070a4c53222d0622acad8d198-e18068d6fe5d5773-00"
}
  1. Do a call with the example request 2, response is 500
System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported.

Expected behavior

Ideally I expect the 400 error that will say clearly that the “type” TypeDiscriminator has to be provided (since the base class is abstract it is clear it cannot be constructed).

At the very least some form of 400 with json that tells that json cannot be deserialized instead of 500 (there was nothing “wrong” with the server, but client provided a bad request which is clearly a 400).

Actual behavior

A 500 is returned and the exception is not handled by asp.net core (unlike example 1 request)

Regression?

No response

Known Workarounds

One can probably make class not abstract and do a validation oneself that the “base” class is not supported.

Configuration

.Net 8 MacOS ARM

Other information

It is somewhat related to the case when type discriminator is not the first field. If it is not the first - it will be the same error I think, which should also be handled ideally, but this can be fixed in .Net 9 hopefully.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 2
  • Comments: 28 (17 by maintainers)

Most upvoted comments

Yes, deserialization is of course not the same as validation, but deserialization is a part of handling the http input, and since that input is wrong (does not conform to the expected model) so that we cannot deserialize, I expect to have a 400 the same way as if the whole json is not properly formatted, etc. Failing with 500 seems a bit unnecessary since we know for fact this was a bad request: json that cannot be deserialized.

Would it be possible e.g. instead of a flag to automatically detect that the base class cannot be constructed (because it is abstract) and then make the discriminator required?

We can have a flag to always force set it as required as well, but there probably not be a need for that since it is obvious in case of an abstract base class it is what the user intends.

I still suspect that fixing System.Text.Json to throw a JsonException instead of a NotSupportedException specifically for this polymorphic deserialization-only case will break absolutely no one and fix a lot of apps.

In simplified terms, what the polymorphic deserializer does is read the type discriminator, look up a type/converter corresponding to it and then hand over control to that converter. There are dozens of reasons why calling that other converter could result in an NSE, mostly related to configuration errors. Superficially, it may appear as if a particular JSON payload prompted the error and must therefore be surfaced as a 4xx, but it truly is a matter of misconfiguration. I don’t think that the polymorphic converter should be made to catch and wrap arbitrary NSE’s, especially if we agree that it shouldn’t happen when the same failure mode occurs in monomorphic contracts.

I maintain that the issue fundamentally is how we handle payloads that don’t specify a discriminator. Today this is hardcoded to using the contract for the base type, but we should consider introducing a mode that rejects such payloads altogether, presumably via a flag on JsonPolymorphicAttribute.

Just ran into this problem - been over a year and it’s still untriaged 😦

Failing with 500 is wrong as it ends up triggering alarms (any 500 errors need dev intervention so alarms are designed to go off on it) due to malformed requests.

EDIT: I think the issue here is the STJ team is throwing very generic exceptions (i.e. NotSupportedException) instead of newtonsoft’s JsonSerializationException which the modelbinder converts to 400. Generally, throwing generic exceptions JsonSerializationException is an anti-pattern.

this is not directly connected to the type discriminator being out of order.

The particular PR also improved the error message in the particular use case that you are citing, cf. https://github.com/dotnet/runtime/pull/97474/commits/4b6b478efa3e297d5c91ce882dd20091ef7904a1

At the very least some form of 400 with json that tells that json cannot be deserialized instead of 500 (there was nothing “wrong” with the server, but client provided a bad request which is clearly a 400).

Regarding this concern, it is difficult to map deserialization failure modes to validation errors (there are too many of those, serialization fails on the first occurrence, serialization can fail at arbitrary locations in the object graph). Arguably, every deserialization error might be regarded as a validation error so if you wish, you might want to write an exception filter that intercepts JsonExceptions.