3

I'm parsing metadata from an OData service using Microsoft.OData (ODataLib) version 7.

After parsing the ODataModel, I want to create an example message with sample values for all declared properties.

So far so good. Works for primitive values in the properties, or for enums and even collection values.

An example for a primitive value:

var property = new ODataProperty() {
    Name = "Key",
    Value = new ODataPrimitiveValue("Value")
};

I want to create a complex value like so:

var property = new ODataProperty() {
    Name = "Key",
    Value = new ODataComplexValue() {
      Properties = new List<ODataProperty>() {
        new ODataPrimitiveValue("Value")
      }  
    }
};

However, ODataComplexValue does not exist in version 7 (latest NuGet Release).

I had a look at the github: the class is in the master branch, but not in the ODatav4-7.x branch.

https://github.com/OData/odata.net/tree/master/src/Microsoft.OData.Core

How can I create complex values?

Christoph
  • 253
  • 1
  • 3
  • 9

2 Answers2

2

I've been able to generate complex value with OData 7 although it feels more like a workaround.

I used ODataUntypedValue and serialized the complex object myself using Newtonsoft.Json.

First of all, it says in the release notes that ODataComplexValue was replaced by ODataResource.

http://odata.github.io/odata.net/v7/#23-17-Merge-Entity-And-Complex-Breaking

However, this cannot be used for an ODataProperty's value because it is not an ODataValue.

As the library wouldn't serialize the complex value, here's the implementation with the explicit serialization using Newtonsoft.Json:

// propertiesObject is a POCO I've dynamically created using ExpandoObject
var complexObject = JsonConvert.SerializeObject(propertiesObject);

var property = new ODataProperty() {
  Name = "Key"
  Value = new ODataUntypedValue() {
    RawValue = complexObject 
  }
};

Any other solution or insight as to why the library feels so inconsistent in this regard would be much appreciated.

Christoph
  • 253
  • 1
  • 3
  • 9
2

The newer version of ODL (7.5.3+) introduces ODataResourceValue which is equivalent to ODataComplexValue.

  • 1
    But the `ODataResourceValue` can't be used as a value in `ODataProperty` for some reason. :( See https://github.com/OData/odata.net/blob/master/test/FunctionalTests/Microsoft.OData.Core.Tests/ODataResourceTests.cs#L272 – František Žiačik Feb 19 '20 at 13:07