From eca03a4d043c11d4ed8dc0b021ade4f17b859333 Mon Sep 17 00:00:00 2001 From: Maximilian Smidt <134879273+maxi-smidt@users.noreply.github.com> Date: Fri, 8 Nov 2024 16:56:14 +0100 Subject: [PATCH] resource util bug (#98) * resource util bug fix * 2.2.8 * resource util bug fix --- CHANGELOG.md | 5 ++++- package.json | 2 +- src/library/ResourceUtils/ResourceUtils.spec.ts | 7 +++++++ src/library/ResourceUtils/ResourceUtils.ts | 13 ++++++++----- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8814d3..62f0f37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.2.8 + +* Fixed issue with `ResourceUtils.getValuesAtResourcePath` not checking if the root of the path matches the resource type. + ## 2.2.7 * Added `BundleUtils.getResourceByFullUrl` to get a resource by its full url from a bundle. @@ -8,7 +12,6 @@ * Fixed issue with `ResourceUtils.getValuesAtResourcePath` not being able to get an array with values if path exists for a top level element and is an object. - ## 2.2.5 * Fixed issue with `ResourceUtils.getValuesAtResourcePath` not being able to get an array with values if path exists for a top level element and is an array. diff --git a/package.json b/package.json index cbc75f5..4bf0c2f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@smile-cdr/fhirts", - "version": "2.2.7", + "version": "2.2.8", "description": "Fhir ts/js library for frontend apps", "license": "Apache-2.0", "main": "dist/index.js", diff --git a/src/library/ResourceUtils/ResourceUtils.spec.ts b/src/library/ResourceUtils/ResourceUtils.spec.ts index f91aee8..f6f1c30 100644 --- a/src/library/ResourceUtils/ResourceUtils.spec.ts +++ b/src/library/ResourceUtils/ResourceUtils.spec.ts @@ -247,6 +247,13 @@ describe("ResourceUtils", () => { describe("#getValuesAtResourcePath()", () => { + it("should return empty array if the root of the path path does not match the resource type", () => { + // execute + const pathValues = resourceUtils.getValuesAtResourcePath(patientPayload, "Practitioner.name.given"); + // validate + expect(pathValues.length).toEqual(0); + }); + it("should return array with values if path exists for a top level element", () => { // execute const pathValues = resourceUtils.getValuesAtResourcePath(patientPayload, "Patient.gender"); diff --git a/src/library/ResourceUtils/ResourceUtils.ts b/src/library/ResourceUtils/ResourceUtils.ts index c5b8f4a..1e15560 100644 --- a/src/library/ResourceUtils/ResourceUtils.ts +++ b/src/library/ResourceUtils/ResourceUtils.ts @@ -70,7 +70,12 @@ export class ResourceUtils { * @returns array of elements found at the provided path */ getValuesAtResourcePath(resource: any, elementPath: string): any[] { - const pathSections = elementPath.split("."); + const pathSections = elementPath.split('.'); + if (!resource || (resource.resourceType !== pathSections[0])) return []; + return this.getValuesAtResourcePathInner(resource, pathSections); + } + + private getValuesAtResourcePathInner(resource: any, pathSections: string[]): any[] { let resourcePathValue; for (let index = 1; index < pathSections.length; index++) { const subPaths = pathSections[index]; @@ -86,14 +91,12 @@ export class ResourceUtils { resultSet.push(subPathValue); } else { - resultSet.push(...this.getValuesAtResourcePath(subPathValue, - pathSections.slice(index).join("."))); + resultSet.push(...this.getValuesAtResourcePathInner(subPathValue, pathSections.slice(index))); } } return resultSet; } else if (typeof (resourcePathValue) === 'object') { - return this.getValuesAtResourcePath(resourcePathValue, - pathSections.slice(index).join(".")); + return this.getValuesAtResourcePathInner(resourcePathValue, pathSections.slice(index)); } } else { break;