From 03bb7228b274e54aa6013dbb15941dd2cadacf7c Mon Sep 17 00:00:00 2001 From: chinlinlee Date: Mon, 9 Oct 2023 19:26:46 +0800 Subject: [PATCH] fix: _format not working # Problems - When using _format=xml and uploading data as json, the returned data is not converted to xml # Solutions - Add a condition `res.locals._format` to check if the user has provided the _format parameter and perform the conversion --- api/FHIRApiService/services/base.service.js | 10 ++++++---- routes.js | 6 ++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/api/FHIRApiService/services/base.service.js b/api/FHIRApiService/services/base.service.js index f95ae23f..b155beb1 100644 --- a/api/FHIRApiService/services/base.service.js +++ b/api/FHIRApiService/services/base.service.js @@ -55,7 +55,7 @@ class BaseFhirApiService { code: 409, msg: handleError.duplicate(err.message) }; - } else if (err.stack.includes("ValidationError")) { + } else if (err?.stack?.includes("ValidationError")) { let operationOutcomeError = new OperationOutcome([]); for (let errorKey in err.errors) { @@ -70,7 +70,7 @@ class BaseFhirApiService { msg: operationOutcomeError }; - } else if (err.stack.includes("stored by resource")) { + } else if (err?.stack?.includes("stored by resource")) { operationOutcomeMessage = { code: 400, msg: handleError.processing(err.message) @@ -91,14 +91,16 @@ class BaseFhirApiService { item = handleError.processing(item); } - if (this.response.getHeader("content-type").includes("xml") || - this.request.get("accept").includes("xml") + if ((this.response.getHeader("content-type").includes("xml") || + this.request.get("accept").includes("xml")) || + this.response.locals?._format?.toLowerCase() === "xml" ) { let fhir = new FHIR(); let xmlItem = fhir.objToXml(item); if (this._pretty) xmlItem = xmlFormatter(xmlItem); return this.response.status(code).send(xmlItem); } + return this.response.status(code).send(item); } diff --git a/routes.js b/routes.js index f071c683..b1c32219 100644 --- a/routes.js +++ b/routes.js @@ -12,6 +12,11 @@ if (fs.existsSync(path.join(__dirname, "./plugins/config.js"))) { pluginsConfig = require("./plugins/config.template").pluginsConfig; } +/** + * + * @param {import("express").Request} req + * @param {import("express").Response} res + */ function setFormatWhenQuery(req, res) { let format = _.get(req, "query._format"); if (format && format.includes("xml")) { @@ -19,6 +24,7 @@ function setFormatWhenQuery(req, res) { } else if (format && format.includes("json")) { res.set("Content-Type", "application/fhir+json"); } + res.locals._format = format; delete req["query"]["_format"]; }