0

The response of Web Api is missing few fields, however, the fields are visible in Debug mode. Do I need to decorate the model with anything specific?

Model:

    public class SampleModel
    {
        public string? SampleModelString { get; set; }
        public SampleModel1? SampleModel1Object { get; set; }
    }
    public class SampleModel1 : List<SampleModel2>
    {
        public SampleModel3? SampleModel3Object { get; set; }
        public string? SampleModel1String { get; set; }
    }
    public class SampleModel2
    {
        public string? SampleModel2String { get; set; }
    }
    public class SampleModel3 : List<SampleModel4>
    {        
    }
    public class SampleModel4
    {
        public string? SampleModel4String { get; set; }
    }

Api Code:

        [HttpGet]
        public IActionResult Get()
        {
            SampleModel sampleModel = new SampleModel();

            sampleModel.SampleModelString = "SampleModel.String";
            sampleModel.SampleModel1Object = new SampleModel1();
            sampleModel.SampleModel1Object.Add(new SampleModel2 { SampleModel2String = "SampleModel2.String1" });
            sampleModel.SampleModel1Object.Add(new SampleModel2 { SampleModel2String = "SampleModel2.String2" });

            sampleModel.SampleModel1Object.SampleModel1String = "SampleModel1.String";
            sampleModel.SampleModel1Object.SampleModel3Object = new SampleModel3();
            sampleModel.SampleModel1Object.SampleModel3Object.Add(new SampleModel4 { SampleModel4String = "SampleModel4.String1" });
            sampleModel.SampleModel1Object.SampleModel3Object.Add(new SampleModel4 { SampleModel4String = "SampleModel4.String2" });            

            return Ok(sampleModel);
        }

Debug mode:

enter image description here

Output:

enter image description here

AaBa
  • 451
  • 1
  • 6
  • 21

1 Answers1

0

Your SampleModel1 is a List with extra properties. This can't be reflected to json as is. You can refactor your code to contain List<SampleModel2> instead of inheriting from it.

 public class SampleModel1
    {
        public List<SampleModel2> SampleModel2ListObject {get; set;}
        public SampleModel3? SampleModel3Object { get; set; }
        public string? SampleModel1String { get; set; }
    }

Update Based on your comment: You can wrap it into another class and re-assign properties. You may need to refactor it to make it safe/read-only.

public class SampleModel1Wrapper
{
    private SampleModel1 _base;

    public List<SampleModel2> SampleModel2ListObject
    {
        get
        {
            return (List<SampleModel2>)_base;
        }
        set
        {
            _base = value;
        }
    }

    public SampleModel3? SampleModel3Object 
    {
        get
        {
            return _base.SampleModel3Object;
        }
        set
        {
            _base.SampleModel3Object = value;
        }
    }
    public string? SampleModel1String
    {
        get
        {
            return _base.SampleModel1String;
        }
        set
        {
            _base.SampleModel1String = value;
        }
    }
Orifjon
  • 915
  • 8
  • 25
  • 1
    [This SO](https://stackoverflow.com/q/21692193/2892829) has good answers why you should not inherit from `List`. – Orifjon Aug 25 '23 at 07:05
  • I understand, that adding the `List` in the class `SampleModel1` will fix the issue, but I cannot change the model as it is a legacy one. @orifjon – AaBa Aug 25 '23 at 07:12
  • 1
    You can wrap it into another class and re-assign properties. See my update. – Orifjon Aug 25 '23 at 07:30
  • Or you could release a new major version with breaking changes... (probably not, just wanted to mention there's another option) – Fildor Aug 25 '23 at 07:39
  • @Fildor - that is not an option, coz there are many downstream systems which uses this model. This model was crafted from XML dataset, and somehow XML serialization process could do the job, but it fails with JSON. – AaBa Aug 29 '23 at 03:49
  • @Orifjon - thanks again for your help. I tried to wrap that up in another class, but the behaviour is same. I'm trying with .Net7 as per this document https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-7-0 – AaBa Aug 29 '23 at 03:50