I have an APIController that one of its methods is expecting a [FromBody] List<AbstractEntity>
. AbstractEntity is an abstract class and it is implemented by let's call them ConcreteEntity1 and ConcreteEntity2. How is this setup in C# .NET so that deserialization works? In my json everything is fine but an empty list of objects is passed in the APIController method.
Here is how the method looks:
[HttpPut]
[Route("x/y/z")]
public void SomeMethod([FromBody] List<AbstractEntity> entities) { ... }
In Java Spring this is how I have set it in the past: I have a variable nodeClass in each of the concrete implementations that is being checked against so the correct class is chosen during deserialization. In abstract entity I put those annotations at the top of the class:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "nodeClass"
)
@JsonSubTypes({
@JsonSubTypes.Type(value = ConcreteEntity1.class, name="Concrete1"),
@JsonSubTypes.Type(value = ConcreteEntity2.class, name="Concrete2"),
})
public abstract class AbstractEntity { ... }
I am hoping there is a concise solution that doesn't involve custom deserializers..
Any help is appreciated! Thanks