diff --git a/projects/lib/utils/utils/resource-field-by-path.spec.ts b/projects/lib/utils/utils/resource-field-by-path.spec.ts index ee4d656..3c213b7 100644 --- a/projects/lib/utils/utils/resource-field-by-path.spec.ts +++ b/projects/lib/utils/utils/resource-field-by-path.spec.ts @@ -47,6 +47,17 @@ describe('getResourceValueByJsonPath', () => { expect(result).toBe('test-result'); }); + it('should query resource using jsonPathExpression', () => { + (jsonpath.query as jest.Mock).mockReturnValue(['test-result']); + + const result = getResourceValueByJsonPath(mockResource, { + jsonPathExpression: '$.spec.value', + }); + + expect(jsonpath.query).toHaveBeenCalledWith(mockResource, '$.spec.value'); + expect(result).toBe('test-result'); + }); + it('should query resource using property', () => { (jsonpath.query as jest.Mock).mockReturnValue(['property-result']); diff --git a/projects/lib/utils/utils/resource-field-by-path.ts b/projects/lib/utils/utils/resource-field-by-path.ts index b8ccb86..db45072 100644 --- a/projects/lib/utils/utils/resource-field-by-path.ts +++ b/projects/lib/utils/utils/resource-field-by-path.ts @@ -25,7 +25,8 @@ export const getResourceValueByJsonPath = ( return undefined; } - const queryResult = jsonpath.query(resource, `$.${property}`); + const prefix = property.startsWith('$.') ? '' : '$.'; + const queryResult = jsonpath.query(resource, `${prefix}${property}`); const value = queryResult.length ? queryResult[0] : undefined; if (value && field.propertyField) {