Skip to content

SystemTextJsonOutputFormatter calls the serializer with unexpected Type #44852

Closed
@akurone

Description

@akurone

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

hi there,
after a short talk with lovely people on runtime team (#dotnet/runtime/issues/77532); i think the issue below needs to be resolved here:
i want to utilize the new json polymorphism features:

[JsonPolymorphic]
[JsonDerivedType(typeof(TheDerivedType), nameof(TheDerivedType))]
public class TheBaseType { public int Prop1 { get; set; } = 1; }

public class TheDerivedType: TheBaseType { public int Prop2 { get; set; } = 2; }

//and in the controller:
[HttpGet] public TheBaseType A() => new TheDerivedType();
// call to this endpoint produces:
// {"prop2":2,"prop1":1}

what i expect to get is:

{"$type":"TheDerivedType","prop2":2,"prop1":1}

but as discussed in #41399 SystemTextJsonOutputFormatter calls the serializer in a very specific way. this call practically hides all the new metadata from the serializer and produces json without discriminator info. without discriminator, the receiving party fails to deserialize this json.

Expected Behavior

SystemTextJsonOutputFormatter and other json returning types (like JO result etc.) rendering with STJ should respect the polymorphic serialization metadata provided.

current:

{"prop2":2,"prop1":1}

expected:

{"$type":"TheDerivedType","prop2":2,"prop1":1}

Steps To Reproduce

clone the repo and make the get reqs below:

  • test/a
  • test/b

Exceptions (if any)

No response

.NET Version

7.0.100-rc.2.22477.23

Anything else?

No response

Metadata

Metadata

Assignees

Labels

old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions