xero-node: Cannot create Invoices and contacts

SDK:

  • Version - 4.11.0

Describe the bug I’m trying to create Invoices and Contacts using Xero-Node library. But facing the same behaviour in both. Here I explained what I did for Contacts

To Reproduce

  const contacts = {
    contacts: [
      {
        'name': data.first_name + ' ' + data.last_name,
        'firstName': data.first_name,
        'lastName': data.last_name,
        'emailAddress': data.email,
        'addresses': [
          {
            'addressType': 'STREET',
            'addressLine1': data.address.replace('$$$$', ', '),
            'city': data.city,
            'postalCode': data.zip_code
          }
        ],
      }
    ],
  };
  console.log(contacts);
  try {
    const response = await xero.accountingApi.createContacts(tokenSet.tenant_id, contacts, summarizeErrors);
    logger.info('Invoice created', response.body || response.response.statusCode);
    return response;
  } catch (err) {
    const error = JSON.stringify(err, null, 2);
    logger.error(`Status Code: ${err} => ${error}`);
  }
}; 

Output:

// from console.log
{
  contacts: [
    {
      name: 'patient name3',
      firstName: 'patient',
      lastName: 'name3',
      emailAddress: 'name@patient.com',
      addresses: [Array]
    }
  ]
}

// error
"response": {
    "statusCode": 400,
    "body": {
      "ErrorNumber": 10,
      "Type": "ValidationException",
      "Message": "A validation exception occurred",
      "Elements": [
        {
          "ContactID": "00000000-0000-0000-0000-000000000000",
          "Name": "",
          "Addresses": [],
          "Phones": [],
          "ContactGroups": [],
          "ContactPersons": [],
          "HasValidationErrors": true,
          "ValidationErrors": [
            {
              "Message": "Contact name cannot be empty"
            }
          ]
        }
      ]
    },
    "headers": {
      "content-type": "application/json; charset=utf-8",
      "content-length": "475",
      "server": "nginx",
      "x-appminlimit-remaining": "9995",
      "x-minlimit-remaining": "58",
      "x-daylimit-remaining": "4859",
      "expires": "Sun, 21 Mar 2021 05:03:00 GMT",
      "cache-control": "max-age=0, no-cache, no-store",
      "pragma": "no-cache",
      "date": "Sun, 21 Mar 2021 05:03:00 GMT",
      "connection": "close",
      "x-client-tls-ver": "tls1.3"
    },
    "request": {
      "uri": {
        "protocol": "https:",
        "slashes": true,
        "auth": null,
        "host": "api.xero.com",
        "port": null,
        "hostname": "api.xero.com",
        "hash": null,
        "search": "?summarizeErrors=true",
        "query": "summarizeErrors=true",
        "pathname": "/api.xro/2.0/Contacts",
        "path": "/api.xro/2.0/Contacts?summarizeErrors=true",
        "href": "https://api.xero.com/api.xro/2.0/Contacts?summarizeErrors=true"
      },
      "method": "PUT",
      "headers": {
        "user-agent": "xero-node-4.11.0",
        "xero-tenant-id": <tenant-id>,
        "Authorization": "Bearer  token",
        "accept": "application/json",
        "content-type": "application/json",
        "content-length": 231
      }
    }
  },
  "body": {}
}

So I console.logged here - https://github.com/XeroAPI/xero-node/blob/master/src/gen/api/accountingApi.ts#L1314

console.log(models_1.ObjectSerializer.serialize(contacts, "Contacts"))

I got this as input data.

  Contacts: [
    {
      ContactID: undefined,
      ContactNumber: undefined,
      AccountNumber: undefined,
      ContactStatus: undefined,
      Name: 'patient name3',
      FirstName: 'patient',
      LastName: 'name3',
      EmailAddress: 'name@patient.com',
      SkypeUserName: undefined,
      ContactPersons: undefined,
      BankAccountDetails: undefined,
      TaxNumber: undefined,
      AccountsReceivableTaxType: undefined,
      AccountsPayableTaxType: undefined,
      Addresses: [Array],
      Phones: undefined,
      IsSupplier: undefined,
      IsCustomer: undefined,
      DefaultCurrency: undefined,
      XeroNetworkKey: undefined,
      SalesDefaultAccountCode: undefined,
      PurchasesDefaultAccountCode: undefined,
      SalesTrackingCategories: undefined,
      PurchasesTrackingCategories: undefined,
      TrackingCategoryName: undefined,
      TrackingCategoryOption: undefined,
      PaymentTerms: undefined,
      UpdatedDateUTC: undefined,
      ContactGroups: undefined,
      Website: undefined,
      BrandingTheme: undefined,
      BatchPayments: undefined,
      Discount: undefined,
      Balances: undefined,
      Attachments: undefined,
      HasAttachments: undefined,
      ValidationErrors: undefined,
      HasValidationErrors: undefined,
      StatusAttributeString: undefined,
      undefined: undefined
    },
    {
      ContactID: undefined,
      ContactNumber: undefined,
      AccountNumber: undefined,
      ContactStatus: undefined,
      Name: '',
      FirstName: undefined,
      LastName: undefined,
      EmailAddress: undefined,
      SkypeUserName: undefined,
      ContactPersons: undefined,
      BankAccountDetails: undefined,
      TaxNumber: undefined,
      AccountsReceivableTaxType: undefined,
      AccountsPayableTaxType: undefined,
      Addresses: undefined,
      Phones: undefined,
      IsSupplier: undefined,
      IsCustomer: undefined,
      DefaultCurrency: undefined,
      XeroNetworkKey: undefined,
      SalesDefaultAccountCode: undefined,
      PurchasesDefaultAccountCode: undefined,
      SalesTrackingCategories: undefined,
      PurchasesTrackingCategories: undefined,
      TrackingCategoryName: undefined,
      TrackingCategoryOption: undefined,
      PaymentTerms: undefined,
      UpdatedDateUTC: undefined,
      ContactGroups: undefined,
      Website: undefined,
      BrandingTheme: undefined,
      BatchPayments: undefined,
      Discount: undefined,
      Balances: undefined,
      Attachments: undefined,
      HasAttachments: undefined,
      ValidationErrors: undefined,
      HasValidationErrors: undefined,
      StatusAttributeString: undefined,
      undefined: undefined
    }
  ],
  undefined: undefined
}

So it seems like the serialization adds one empty object in the array. Am I missing anything here? Or is it a Bug?

Thanks in advance.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (4 by maintainers)

Most upvoted comments

@MattBidewell Hey Matt, here is the basic code to pull it off, in case you (or someone else happening upon this thread) need it:

const contact = { name: "Bruce Banner", emailAddress: "hulk@avengers.com", phones: [ { phoneNumber:'555-555-5555', phoneType: 'MOBILE' } ] }; const contacts = { Contacts: [contact] }; const current_token = xero.readTokenSet(); let contact_resp = await axios({ method: 'post', url: 'https://api.xero.com/api.xro/2.0/Contacts', headers: { "xero-tenant-id": credentials.xeroConfig.tenant_id, Authorization: Bearer ${current_token.access_token} }, data: contacts }).then(resp => { console.log(resp.data); }).catch(err => { console.log(JSON.stringify(err)); });

This issue is caused when additional properties have been added to Array.prototype. More info here: #590

Hi @sksankarraj. I just tried to replicate your issue and I only console log one object in the serializer as expected. It looks like you are sending a contact array with two objects - the second object with only a name property set as an empty string? What is logged if you console.log(contacts.length) just before calling the API? Closing this for now as it does not appear to be an SDK issue.