Skip to content

Commit c37de21

Browse files
committed
Ensure proper prefix handling in JSONPath queries and add test case.
1 parent 5220b36 commit c37de21

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

projects/lib/utils/utils/resource-field-by-path.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ describe('getResourceValueByJsonPath', () => {
4747
expect(result).toBe('test-result');
4848
});
4949

50+
it('should query resource using jsonPathExpression', () => {
51+
(jsonpath.query as jest.Mock).mockReturnValue(['test-result']);
52+
53+
const result = getResourceValueByJsonPath(mockResource, {
54+
jsonPathExpression: '$.spec.value',
55+
});
56+
57+
expect(jsonpath.query).toHaveBeenCalledWith(mockResource, '$.spec.value');
58+
expect(result).toBe('test-result');
59+
});
60+
5061
it('should query resource using property', () => {
5162
(jsonpath.query as jest.Mock).mockReturnValue(['property-result']);
5263

projects/lib/utils/utils/resource-field-by-path.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const getResourceValueByJsonPath = (
2525
return undefined;
2626
}
2727

28-
const queryResult = jsonpath.query(resource, `$.${property}`);
28+
const prefix = property.startsWith('$.') ? '' : '$.';
29+
const queryResult = jsonpath.query(resource, `${prefix}${property}`);
2930
const value = queryResult.length ? queryResult[0] : undefined;
3031

3132
if (value && field.propertyField) {

0 commit comments

Comments
 (0)