ALAppExtensions icon indicating copy to clipboard operation
ALAppExtensions copied to clipboard

API: Support for Edm.Collection in ODataEdmType XML

Open wisa1 opened this issue 6 years ago • 3 comments

Situation: In order to use complex types in custom API's we can use the Table "OData Edm Type" to describe the desired type definition using xml. The BC-Standard API's use this to represent PaymentMethods and UOM-Conversions among a few other things. All the integrated Edm Types BC ships with have one thing in common: they don't nest further than 2 levels.

Therefore the following JSON payload can be published/received properly:

{
    "id": "ORDER1234",
    "customerName": "Jon Doe",
    "lines": [
        {
            "id": 10000,
            "description": "TestItem"
        }
    ]   
}

To represent the "lines" - Part in our API Page Object we would need create a OdataEdmType (name: "LINES") with the following structure:

<ComplexType Name="lines">
    <Property Name="id" Type="Edm.Int32"></Property>
    <Property Name="description" Type="Edm.String"></Property>
</ComplexType>

Since we want an Array of the new Type we would use ODataEDMType = 'Collection(LINES)'; to assign it to a Page Control.

However, trying to define more complex scenarios leads to a Problem.

Problem Assuming the desired JSON representation gets a bit more complicated, for example we might want a single "line" to hold another array of applicable discounts:

{
    "id": "ORDER1234",
    "customerName": "Jon Doe",
    "lines": [
        {
            "id": 10000,
            "description": "TestItem",
            "discounts": [
                {
                    "id": "d10"
                    "discountPercent": 10
                },
                {
                    "id": "d3"
                    "discountPercent": 3
                },
            ]
        }
    ]   
}

In my understanding according to the Edm Type Definition the way to do this would be to create a new EdmType "DISCOUNTS"

<ComplexType Name="discounts">
    <Property Name="id" Type="Edm.Int32"></Property>
    <Property Name="discountPercent" Type="Edm.String"></Property>
</ComplexType>

and extend the "LINES" type to:

<ComplexType Name="lines">
    <Property Name="id" Type="Edm.Int32"></Property>
    <Property Name="description" Type="Edm.String"></Property>
    <Property Name="discounts" Type="Collection(DISCOUNTS)"></Property>
</ComplexType>

However doing so will yield an Error when calling the WebService or trying to receive the OData Metadata:

{
    "error": {
    "code": "Unknown",
    "message": "The OData EDM complex type Collection(DISCOUNTS) that is referenced by lines was not found."
    }
}

Types embedded in the XML-Blob in the "OData EDM Type" Record do not seem to be checked for the "Collection" Keyword.

Question: I need to be able to receive a complex JSON payload which does not at all map to BC Data. Out of hundreds of fields and nested Objects I'm only interested in very few fields. I am fully aware that using a Flow or some proxy Service that does the mapping this would be achievable very easily, however I would rather not resort to additional external services if this is doable in pure AL.

Is there any way to achieve this in the current version? If yes, how? If not, are there any plans on implementing this scenario in future releases?

I'm really looking forward responses regarding this issue.

wisa1 avatar Mar 09 '20 12:03 wisa1

@nikolakukrika - can you help?

AlexanderYakunin avatar May 24 '20 09:05 AlexanderYakunin

It is not possible to implement the complex types outside of Microsoft, this feature is not extensible in SaaS. For OnPrem you could define your own complex types however I would not recommend it.

The current implementation of complex types has two major issues:

  1. Performance - perf impact is large, APIs using complex types are much slower than APIs without them. The reason for slowness is the fact that we are generating them via AL, a lot of time is lost in .NET interoop and AL invocation.
  2. Stability - if AL parsing / generation throws an error then the API is broken. We should really not handle the complex types via AL, but platform should generate them.

Because of these two problems we have decided to keep complex types as internal only feature and will remove most of the complex types in near future. The plan is to replace most of the complex types with API parts. API pages can nest as many levels as necessary, there is no 2 level limitation present in the product which is something I would strongly recommend. Address may be the only complex type that will stay, we need to see how to model this one.

For your APIs I would recommend following:

  1. See if you can use Query objects to define a payload
  2. If you have few records (less than 1000) then you can use temporary tables if you cannot build an API
  3. You could implement an aggregate table as we did on the Sales Invoices and other sales documents, this would give you a fast read if there is a lot of data, however there is a cost of maintain the aggregate and writes are more expensive. 4.. You could also use parts for subparts of data. The subparts of data could be bound to the temporary tables. Then the users of the API can decide to $expand or call the parts separately. This would limit the performance hit of loading a lot of data.

nikolakukrika avatar May 24 '20 13:05 nikolakukrika

@wisa1 - if you have more questions, you can join this yammer group and ask here: https://www.yammer.com/microsoft.com/#/threads/inGroup?type=in_group&feedId=14737513 The name is APIs for Dynamics 365 Business Central"

nikolakukrika avatar May 24 '20 19:05 nikolakukrika