Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity Builder fromJson not working for non-top-level navigation properties #5180

Closed
sippsolutions opened this issue Nov 14, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@sippsolutions
Copy link

sippsolutions commented Nov 14, 2024

Describe the bug
When trying to use fromJson with navigation properties it works perfectly if they are only assigned on the top level item (example: root>toItem). Nested navigationProperties (example: root>toItem>toScheduleLine) do not work, the entity builder does not have the functions assigned at that moment.

To Reproduce

  1. Use this example JSON:
{
  salesOrderType: 'OR',
  salesOrderTypeInternalCode: 'TA',
  salesOrganization: '1010',
  distributionChannel: '10',
  organizationDivision: '00',
  soldToParty: '10100003',
  purchaseOrderByCustomer: 'PO-GROUP-88',
  salesOrderDate: Moment<2024-11-24T00:00:00+01:00>,
  pricingDate: Moment<2024-11-24T00:00:00+01:00>,
  requestedDeliveryDate: Moment<2024-11-24T00:00:00+01:00>,
  shippingCondition: '01',
  completeDeliveryIsDefined: true,
  toItem: [
    {
      salesOrder: '10',
      salesOrderItem: '10',
      material: 'TG11',
      requestedQuantity: '100',
      requestedQuantityUnit: 'PC',
      toScheduleLine: [
        {
          salesOrder: '10',
          salesOrderItem: '10',
          scheduleLine: '1',
          requestedDeliveryDate: Moment<2024-11-24T00:00:00+01:00>,
          confirmedDeliveryDate: null,
          orderQuantityUnit: 'PC',
          scheduleLineOrderQuantity: '100'
        }
      ]
    }
  ]
}
  1. Try to build the entity with fromJson: salesOrderApi.entityBuilder().fromJson(data);
  2. A warning is displayed: WARN (entity-builder): Field name "toScheduleLine" is already existing in "SalesOrderItem" and thus cannot be defined as custom field.

toItem works perfectly (top-level navigation property on SalesOrder API), but toScheduleLine on the SalesOrderItem Entity does not work as toScheduleLine is not assigned as a function.

Expected behavior
Nested navigation properties get assigned correctly.

Used Versions:

  • node version v20.18.0
  • npm version via npm -v 10.8.2
  • SAP Cloud SDK version you used as dependency "@sap-cloud-sdk/generator": "^3.22.2",

Code example

const util = require('util');
const {apiSalesOrder} = require('../../../services/API_SALES_ORDER/service.js');

async function sendData(params, data, preProcessed) {
    const destination = {
        name: 'xxx',
        url: 'xxx',
        username: 'xxx',
        password: 'xxx',
    }
    process.env.destinations = JSON.stringify([destination]);

    const {salesOrderApi} = apiSalesOrder();
    const salesOrderEntity = salesOrderApi.entityBuilder().fromJson(data);

    salesOrderApi
        .requestBuilder()
        .create(salesOrderEntity)
        .execute({destinationName: 'xxx'})
        .then((data) => {
            console.log(data);
        });

    return {
        success: true
    }
}

module.exports = {
    sendData
}

(use data json object from above)

@sippsolutions sippsolutions added the bug Something isn't working label Nov 14, 2024
@deekshas8
Copy link
Contributor

Hi @sippsolutions ,

We are aware of this issue. We do a lazy init of 2nd level navigation properties. This is the reason why they don't get initialized.

As a workaround, can you destructure the nested apis as well (even if you don't use them). This ensures that the nested navigation properties are also initialized. An example would be :

const { salesOrderApi, salesOrderItemApi, salesOrderScheduleLineApi } = apiSalesOrder();

@sippsolutions
Copy link
Author

@deekshas8 Thank you, the workaround works well!

@deekshas8
Copy link
Contributor

Hi @sippsolutions , that's great. I will close this ticket as we have #3538 to track the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants