Problem with Serialization of request body
See original GitHub issueHello team,
We have been using Xero for long time and we were consuming xero-apis in a traditional way using node-fetch
.
We are trying to move our API calls to Xero-Node wrappers. But we are facing serialization issues in all services, invoices, credit notes etc.
Example - credit note:
Code we are using:
const createXeroCreditNote = async (invoiceID) => {
const tokenSet = await getTokenSet();
await xero.setTokenSet(tokenSet.token_set);
const unitdp = 4;
let invoice;
try {
const response = await xero.accountingApi.getInvoice(tokenSet.tenant_id, invoiceID, unitdp);
invoice = response.body.invoices[0];
} catch (err) {
const error = JSON.stringify(err.response.body, null, 2);
throw new ErrorHandler(500, `Getting invoice to void error: ${err} => ${error}`);
}
logger.info(`Creating credit note for invoice of ${invoice.reference}`);
const contact = {
contactID: invoice.contact.contactID,
};
const lineItems = invoice.lineItems.map(lineItem => ({
description: lineItem.description,
quantity: lineItem.quantity,
unitAmount: lineItem.unitAmount,
accountCode: lineItem.accountCode,
itemCode: lineItem.itemCode,
}));
const creditNotes = {
creditNotes: [{
type: CreditNote.TypeEnum.ACCRECCREDIT,
contact: contact,
date: moment(),
lineItems,
}]
};
try {
const response = await xero.accountingApi.createCreditNotes(tokenSet.tenant_id, creditNotes, true, unitdp);
logger.info('Credit note successfully created - %o', response.body || response.response.statusCode);
return response.body.creditNotes[0];
} catch (err) {
const error = JSON.stringify(err, null, 2);
console.log(error);
throw new ErrorHandler(500, `createXeroCreditNote error: ${err} => ${error}`);
}
};
Error I get
{
"response": {
"statusCode": 400,
"body": {
"ErrorNumber": 10,
"Type": "ValidationException",
"Message": "A validation exception occurred",
"Elements": [
{
"CreditNoteID": "00000000-0000-0000-0000-000000000000",
"ID": "00000000-0000-0000-0000-000000000000",
"HasErrors": true,
"Type": "ACCRECCREDIT",
"Contact": {
"ContactID": "54b2e385-9987-490f-b901-a47b6fd0f501",
"Addresses": [],
"Phones": [],
"ContactGroups": [],
"ContactPersons": [],
"HasValidationErrors": false,
"ValidationErrors": []
},
"DateString": "2021-09-22T03:02:24",
"Date": "/Date(1632279744252)/",
"Status": "DRAFT",
"LineAmountTypes": "Exclusive",
"LineItems": [
{
"ItemCode": "C01-C020003-030-1",
"Description": "Circle CBD 20 Oil - 30ml",
"UnitAmount": 119,
"TaxType": "BASEXCLUDED",
"TaxAmount": 0,
"LineAmount": 238,
"AccountCode": "200",
"Tracking": [],
"Quantity": 2,
"AccountID": "acafc55b-57e9-4d19-b9a6-66d30d4b50b7",
"ValidationErrors": []
},
{
"ItemCode": "DIS-FEE",
"Description": "Pharmacy Dispensing Fee",
"UnitAmount": 35,
"TaxType": "BASEXCLUDED",
"TaxAmount": 0,
"LineAmount": 35,
"AccountCode": "200",
"Tracking": [],
"Quantity": 1,
"AccountID": "acafc55b-57e9-4d19-b9a6-66d30d4b50b7",
"ValidationErrors": []
},
{
"Tracking": [],
"ValidationErrors": []
},
{
"Tracking": [],
"ValidationErrors": []
}
],
"SubTotal": 273,
"TotalTax": 0,
"Total": 273,
"CurrencyCode": "AUD",
"ValidationErrors": [
{
"Message": "The description field is mandatory for each line item."
},
{
"Message": "The description field is mandatory for each line item."
}
]
},
{
"CreditNoteID": "00000000-0000-0000-0000-000000000000",
"ID": "00000000-0000-0000-0000-000000000000",
"HasErrors": true,
"DateString": "2021-09-22T00:00:00",
"Date": "/Date(1632268800000+0000)/",
"Status": "DRAFT",
"LineAmountTypes": "Exclusive",
"LineItems": [],
"SubTotal": 0,
"TotalTax": 0,
"Total": 0,
"CurrencyCode": "AUD",
"ValidationErrors": [
{
"Message": "One or more line items must be specified"
},
{
"Message": "Type is required."
},
{
"Message": "A Contact must be specified for this type of transaction"
}
]
}
]
},
"headers": {
"content-type": "application/json; charset=utf-8",
"content-length": "2898",
"server": "nginx",
"xero-correlation-id": "6b7a5ebe-2690-4a1b-89fc-a1827c3ebfa4",
"x-appminlimit-remaining": "9996",
"x-minlimit-remaining": "57",
"x-daylimit-remaining": "4796",
"expires": "Wed, 22 Sep 2021 03:02:24 GMT",
"cache-control": "max-age=0, no-cache, no-store",
"pragma": "no-cache",
"date": "Wed, 22 Sep 2021 03:02:24 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&unitdp=4",
"query": "summarizeErrors=true&unitdp=4",
"pathname": "/api.xro/2.0/CreditNotes",
"path": "/api.xro/2.0/CreditNotes?summarizeErrors=true&unitdp=4",
"href": "https://api.xero.com/api.xro/2.0/CreditNotes?summarizeErrors=true&unitdp=4"
},
"method": "PUT",
"headers": {
"user-agent": "xero-node-4.15.0",
"xero-tenant-id": "9033f7ba-e92f-4510-bbda-01d4dfa299e9",
"Authorization": "Bearer ",
"accept": "application/json",
"content-type": "application/json",
"content-length": 397
}
}
},
"body": {}
}
Please check the response I have posted in error.
There are SERIALIZATION issues 2 here.
-
You can there are 2 credit notes in the response, but in the code you can see I have added only one CN and there is no way it can be 2 CN from code. I don’t how it became 2 in the response.
-
You can see the validation error from first credit note which is the one I’m passing to xero. it says
"Message": "The description field is mandatory for each line item."
. Even in the response data that xero returns has the description value. How it is possible to produce this error idk.
Please help me with these issues I’m stuck here.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (1 by maintainers)
@sksankarraj Can you contact Xero API Support at api@xero.com with the details of your integration:
We can then check the application’s logs to investigate.
As I think this is not an SDK issue, I’ll close this issue for now.
I am unable to replicate the issue using Xero-Node v4.15.0.
With each release that adds new methods we add those methods to our kitchen sink sample app and test those calls to verify functionality. https://github.com/XeroAPI/xero-node-oauth2-app
In the past 30 days Xero-Node users have made: POST: 4,963,388 API calls PUT: 2,159,183 API calls
I’ve tested using two of our sample apps:
Node and Express without TypeScript https://github.com/XeroAPI/xero-node-serializer-test
Node and Express with TypeScript https://github.com/XeroAPI/xero-node-oauth2-ts-starter
I understand your frustration and I’m keen to help get you unstuck. If you could, please try to replicate your issue using one of the two above sample repos and submit a PR to the repo