From 82c396c7e42cfeed47c17fbc53934a757d360663 Mon Sep 17 00:00:00 2001 From: Pradeep Peddineni <23334879+pradeeppeddineni@users.noreply.github.com> Date: Sat, 24 Oct 2026 18:34:44 -0400 Subject: [PATCH] Read decimal values from XML input as numbers GoFSH read XML input with xmlToObj, which keeps decimal values as strings. That made instances from XML get quoted decimals, like position.longitude = "-97.7047386", which SUSHI then rejects. Use xmlToJson and parse the result instead. It writes decimals as JSON numbers, so XML input now gives the same content as the equivalent JSON file. Fixes #260 --- src/utils/Processing.ts | 6 +++++- test/utils/Processing.test.ts | 11 +++++++++++ test/utils/fixtures/xml-decimals/location.json | 9 +++++++++ test/utils/fixtures/xml-decimals/location.xml | 9 +++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/utils/fixtures/xml-decimals/location.json create mode 100644 test/utils/fixtures/xml-decimals/location.xml diff --git a/src/utils/Processing.ts b/src/utils/Processing.ts index 8ce2c5de..8a28fd9a 100644 --- a/src/utils/Processing.ts +++ b/src/utils/Processing.ts @@ -295,7 +295,11 @@ export function readJSONorXML(file: string): FileImport { return importedFile; } else if (file.endsWith('.xml')) { const buffer = fs.readFileSync(file); - const importedFile: FileImport = { content: FHIRConverter.xmlToObj(buffer.toString()) }; + // xmlToObj keeps decimal values as strings, so convert to JSON text first. + // xmlToJson writes decimals as JSON numbers, which gives the same result as reading + // the equivalent JSON file. + const json = FHIRConverter.xmlToJson(buffer.toString()); + const importedFile: FileImport = { content: json != null ? JSON.parse(json) : undefined }; if (buffer.length > LARGE_FILE_BUFFER_LENGTH) { importedFile.large = true; } diff --git a/test/utils/Processing.test.ts b/test/utils/Processing.test.ts index 3a04ecca..8aab7725 100644 --- a/test/utils/Processing.test.ts +++ b/test/utils/Processing.test.ts @@ -566,6 +566,17 @@ describe('Processing', () => { ); }); + it('should read decimal values in xml files as numbers', () => { + const XMLFilePath = path.join(__dirname, 'fixtures', 'xml-decimals', 'location.xml'); + const JSONFilePath = path.join(__dirname, 'fixtures', 'xml-decimals', 'location.json'); + + const xmlFileImport = readJSONorXML(XMLFilePath); + + expect(xmlFileImport.content.position.longitude).toBe(-97.7047386); + expect(xmlFileImport.content.position.latitude).toBe(30.4159542); + expect(xmlFileImport.content).toEqual(fs.readJSONSync(JSONFilePath)); + }); + it('should not log an error when JSON files begin with a Byte Order Mark', () => { const JSONFilePath = path.join(__dirname, 'fixtures', 'with-BOM', 'badBOM.json'); diff --git a/test/utils/fixtures/xml-decimals/location.json b/test/utils/fixtures/xml-decimals/location.json new file mode 100644 index 00000000..a2e25b37 --- /dev/null +++ b/test/utils/fixtures/xml-decimals/location.json @@ -0,0 +1,9 @@ +{ + "resourceType": "Location", + "id": "orgloc", + "name": "Org Location", + "position": { + "longitude": -97.7047386, + "latitude": 30.4159542 + } +} diff --git a/test/utils/fixtures/xml-decimals/location.xml b/test/utils/fixtures/xml-decimals/location.xml new file mode 100644 index 00000000..9ebe5a13 --- /dev/null +++ b/test/utils/fixtures/xml-decimals/location.xml @@ -0,0 +1,9 @@ + + + + + + + + +