Home > Enterprise >  How can I return a List<BaseClass> with Children properties?
How can I return a List<BaseClass> with Children properties?

Time:11-10

How can I return a list of Base class objects with its sub types properties from a .NET 5 controller.

public class StepBaseDto 
{
    public string Title { get; set; }
    public int Order { get; set; }
    public StepType StepType { get; set; }
    public bool IsDone { get; set; }
    public string Description { get; set; }
    public string PropertyLabel { get; protected set; }
    public Guid ProtocolId { get; set; }
    public List<SubStepDto> SubSteps { get; set; }
}



public class StartDateStepDto : StepBaseDto
{
    public DateTime? StartingDate { get; set; }
}



public class ProtocolDto : ProtocolBaseDto
{
    public List<StepBaseDto > StepSequence { get; set; }
}

Endpoint:

public override Task<ProtocolDto> GetAsync(Guid id)

When returning a ProtocolDto with its StepBaseDto list, I cannot see the properties of the derived classes on the JSON result.

But is working fine if I change

public class ProtocolDto : ProtocolBaseDto
{
    public List<object> StepSequence { get; set; }
}

Is there a way I can accomplish the same result without changing StepBaseDto to object?

edit: I am using automapper

I found out a related question to this problem: Derived type's properties missing in JSON response from ASP.NET Core API

but the solution is not working for me, I am using ABP framework 5.3.4

When using List

When using List

Fixed It by using dynamic instead of the base class

CodePudding user response:

You can't see the subclass properties because you said it's a list of base classes. Any code that references that list will then also believe them to be the base classes. You can either:

  1. Make the list a list of subclasses, or
  2. Type cast every time you want to access those subclass properties, or
  3. Move the subclass properties to an interface and use a list of interface, or
  4. Move the subclass properties into the base class.

I think the trouble you're having is that you don't actually want a list of base classes if you are trying to access properties of the subclass. Since your subclass is only adding a nullable property, I would argue you could put that in the base class and not break anything - objects that don't have the property leave it null, ones that do can set the field.

It's not clear to me how you're trying to access this, how it's supposed to be used, etc., so it's hard to give you better guidance.

CodePudding user response:

Solution; Microsoft.Text.Json <V6 doesn't support polymorphism, we must use Newtonsoft Json parser. or you have to switch to V7 which is not possible for me.

full explanation here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-7-0

they force us to use object or just switch back to Newtonsoft

Edit: a Good work around is to use dynamic instead of the base class

  • Related