3

I'm developing an api in net core.

I've done a post function in which I send an object containing multiple parameters and a list within another list.

When I'm debugging the code the function is called correctly but I find that the second list always arrives null.

The rest of the data arrives at you correctly. I have done different tests with other objects and everything works correctly.

It is this case in which the list within another the second one arrives null.

My code:

example request input

{
  "Name": "TestName",
  "Related1": 
   [{
      "id1": "TestNameRelated1",
      "Related2": 
      [{
         "id2": "TestNameRelated2"
      }]
   }]
}    
[HttpPost]
public resultExample Test([FromBody]TestClass test)
{
   //do something
}    
[DataContract]
public class TestClass 
{    
   [DataMember]
   public string Name { get; set; }
   [DataMember]
   public List<TestClassArray> Related1 { get; set; }
}

[DataContract]
public class TestClassArray
{    
   [DataMember]
   public string id1 { get; set; }
   [DataMember]
   public List<TestClassArray2> Related2 { get; set; }
}

[DataContract]
public class TestClassArray2
{    
   [DataMember]
   public string id2 { get; set; }
}    

This api was previously made in .NET framework 4.8 and this case worked correctly.

Now I'm passing the api to .Net5.

Could it be that in .Net5 it is not allowed to pass lists within other lists?

Do you have to enable some kind of configuration to be able to do this now?

2
  • In the top portion code you give an example of the output. Is this the output of your current code or how you want it to be? Commented Jun 29, 2021 at 13:50
  • 3
    Is this part of JSON populated in your DTO? "id1": "TestNameRelated1", Please note that [DataContract] and [DataMember] are ignored by default in later versions of asp.net Core. BR Commented Jun 29, 2021 at 14:06

2 Answers 2

4

You need use class/DTO with constructor like shown below and you should be good to go. I have uploaded this sample API app's code working with .net5.0 on my GitHub here.

public class TestClass
    {
        public TestClass()
        {
            Related1 = new List<TestClassArray>();
        }

        public string Name { get; set; }

        public List<TestClassArray> Related1 { get; set; }
    }

public class TestClassArray
    {
        public TestClassArray()
        {
            Related2    = new List<TestClassArray2>();
        }
    
        public string id1 { get; set; }
        
        public List<TestClassArray2> Related2 { get; set; }
    }

public class TestClassArray2
    {
        
        public string id2 { get; set; }
    }

 public class ResultExample
    {
        public string StatusCode { get; set; }
        public string Message { get; set; }
    }

Controller Post Method

        [HttpPost]
        [ProducesResponseType(typeof(ResultExample), 200)]
        public ResultExample Post([FromBody] TestClass test)
        {
            ResultExample testResult = new ResultExample();    

            TestClass test2 = new TestClass();
            TestClassArray testClassArray = new TestClassArray();
            TestClassArray2 testClassArray2 = new TestClassArray2();
            
            test2.Name = test.Name;            
            
            foreach (var item in test.Related1)
            {
                foreach (var item2 in item.Related2)
                {
                    testClassArray2.id2 = item2.id2;
                }
                
                testClassArray.Related2.Add(testClassArray2); 
            }

            test2.Related1.Add(testClassArray);

            Console.WriteLine(test2);


            testResult.Message = "New Result added successfullly....";
            testResult.StatusCode = "201";

            return testResult;
        }

Swagger Input Sample Payload

enter image description here

Post Controller Result

enter image description here

Response of Sample input payload,(You can change it to default 201 response code as well)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

I had a similar issue.

API method shows List was null

In my case a date field was not well formatted

So I use SimpleDateFormat on Android Studio with a correct datetime format

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",Locale.US);
item.setDate(dateFormat.format(calendar.getTime()));

and works fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.