0

Summarize:

I use swashbuckle 5.3.x with Newtonsoft in an .net core 3.1 application. My current problem is related to serialization of an incoming json on a PUT method to an object with a list of children that use polymorphism. The discriminator for swagger in swashbuckle is a "$type" key in the given json. I expect that it is independent where the $type-key is in the json child object but if I try a PUT it only works when the $type-key is the first key-value pair in the child.

Here is an example:

public class Parent
{
    public string AParentField { get; set; }
    public IList<ChildBase> Positions { get; set; }
}

[KnownType(typeof(ChildSimple))]
[KnownType(typeof(ChildComplex))]
public abstract class ChildBase
{
    public string AChildField { get; set; }
}

public class ChildSimple : ChildBase 
{
    public string ASimpleField { get; set; }  
}

public class ChildComplex : ChildBase
{ 
    public string AComplexField1 { get; set; }
    public string AComplexField2 { get; set; }
}

If I update with the following body:

{
    "aparentfield": "parentvalue",
    "positions":
     [
      {
        "$type": "ChildSimple",
        "asimplefield": "simplevalue"
      },
      {
        "$type": "ChildComplex",
        "acomplexfield1": "complexfield1",
        "acomplexfield2": "complexfield2"
      }
     ]
}

Everything works fine! But if I switch for example the $type in the complex with the "acomplexfield1" line I get the following error:

{
  "errors": {
    "childUpdate": [
      "The childUpdate field is required."
    ],
    "positions[1].acomplexfield1.0": [
      "Could not create an instance of type Base. Type is an interface or abstract class and cannot be instantiated. Path 'positions[0].acomplexfield1', line x, position y."
    ]
  },
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-4d2014c2e258af40a6dc4176c9109d6c-ed14ccc0be550f48-00"
}

I expect that it doesn't matter where the $type is, just like it should be for a real json! I tried to use a Document- and SchemaFilter like in this example: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/842

But it didnt work out. I wasn't able to force newtonsoft to look for the $type before choosing the right object.

Hope someone can help or has a hint on how I can make the position of the discriminator "$type" not necessary so it's a real json.

  • Looks like a bug in swagger-ui, can you provide a minimal swagger.json that causes the error – Helder Sepulveda Apr 13 '20 at 13:28
  • Set [`MetadataPropertyHandling`](https://www.newtonsoft.com/json/help/html/SerializationSettings.htm#MetadataPropertyHandling) to `ReadAhead` to fix this problem. See the linked duplicate question. – Brian Rogers Apr 13 '20 at 20:33

0 Answers0