Skip to content

Commit

Permalink
fix: _format not working
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
Chinlinlee committed Oct 9, 2023
1 parent fa806b6 commit 03bb722
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions api/FHIRApiService/services/base.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ 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")) {
res.set("Content-Type", "application/fhir+xml");
} else if (format && format.includes("json")) {
res.set("Content-Type", "application/fhir+json");
}
res.locals._format = format;
delete req["query"]["_format"];
}

Expand Down

0 comments on commit 03bb722

Please sign in to comment.