Chiricescu Mihai
Chiricescu Mihai

Reputation: 239

Function result Complex type serialization OData 6.0.0

I updated OData version to 6.0.0.

Before update i had a function declaration like:

var getResult = odataBuilder.EntityType<Customer>().Collection.Function("GetResult");
    getResult.Parameter<int>("Number");
    getResult.Returns<ResultDto>();

ResultDto has structure:

    public class ResultDto
{
    public string CustomerName { get; set; }
    public IList<ResultComponentsDto> ResultComponentsDtos { get; set; }
}

ResultComponentsDto has structure:

    public class ResultComponentsDto
{
    public string Description { get; set; }
    public IList<Product> Products { get; set; }
}

Where Product is an entity.

The function will return the correct data as:

[HttpGet]
    public async Task<IHttpActionResult> GetResult(int number)
    {
        var result = new ResultDto
        {
            CustomerName = "Test",
            ResultComponentsDtos = new List<ResultComponentsDto>
            {
                new ResultComponentsDto
                {
                    Description = "Test 1",
                    Products = new List<Product>
                    {
                        new Product
                        {
                            Id = Guid.NewGuid(),
                            Name = "1"
                        },
                        new Product
                        {
                            Id = Guid.NewGuid(),
                            Name = "2"
                        },
                    }
                },
                new ResultComponentsDto
                {
                    Description = "Test 2",
                    Products = new List<Product>
                    {
                        new Product
                        {
                            Id = Guid.NewGuid(),
                            Name = "3"
                        },
                        new Product
                        {
                            Id = Guid.NewGuid(),
                            Name = "4"
                        },
                    }
                }
            }
        };
        return Ok(result);
    }

My problem is after the update i will not get the full result, i only get the following result serialized:

    {  
   "@odata.context":"http://localhost:3721/$metadata#WebApi.Dto.ResultDto",
   "CustomerName":"Test",
   "ResultComponentsDtos":[  
      {  
         "Description":"Test 1"
      },
      {  
         "Description":"Test 2"
      }
   ]
}

The collection of products inside ResultComponentsDto is not being serialized, before update this worked. As the serializer for complex type has changed: see here What would be the way to achieve the same results as in the previous version?

What i tried:

  1. Enable query parameter: [EnableQuery(MaxExpansionDepth = 4)]
  2. Configuring the complext type in the model builder with autoexpand set to true:

            var result = odataBuilder.ComplexType<ResultDto>();
        result.Property(r => r.CustomerName);
        var resultComponents = result.CollectionProperty(r => r.ResultComponentsDtos);
        resultComponents.AutoExpand = true;
    
        var resultComponentsComplexType = odataBuilder.ComplexType<ResultComponentsDto>();
        resultComponentsComplexType.Property(rc => rc.Description);
        var products = resultComponentsComplexType.HasMany(rc => rc.Products);
        products.AutoExpand = true;
        products.AutomaticallyExpand(false);
    
  3. Use autoexpand attribute: [AutoExpand] public IList Products { get; set; }

  4. Manual expand in request (unsing $expand), this is not working currently for complext types.

Thanks in advance, Mihai

Upvotes: 0

Views: 602

Answers (1)

Ahmadreza Farrokhnejad
Ahmadreza Farrokhnejad

Reputation: 2400

I had same problem. i suggest you that change output result to string. such as bellow. getResult.Returns< string > ();

Then in getResult method convert result object to json string. return Ok(JsonConvert.SerializeObject(result))

In client you must DeSerialize output string to object.

Upvotes: 0

Related Questions