Skip to content

Commit aa5aed4

Browse files
committed
Add more tests for #26
1 parent 0031a36 commit aa5aed4

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ POST /r4/parse-template
611611
#### Strict mode
612612

613613
There's a flag, called `strict` that is set to `false` by default. If it set to `true`, all accesses to the variables without the percent sign will be rejected and exception will be thrown.
614+
NOTE: there's a known issue with accessing the resource by resource type (e.g. QuestionnaireResponse.item), see details [here](https://github.com/beda-software/FHIRPathMappingLanguage/issues/27).
614615

615616
The previous example using strict mode:
616617

python/tests/core/test_extract.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@ def test_transformation_fails_on_accessing_props_of_resource_in_strict_mode() ->
2727
resolve_template(resource, {"key": "{{ list.key }}"}, {}, strict=True)
2828

2929

30-
def test_transformation_fails_on_accessing_props_of_resource_with_capital_letter_in_strict_mode() -> None: # noqa: E501
31-
resource: Resource = {"resourceType": "Resource", "key": [1,2,3]}
30+
def test_transformation_fails_on_accessing_props_of_resource_with_capital_letter_in_strict_mode() -> ( # noqa: E501
31+
None
32+
):
33+
resource: Resource = {"resourceType": "Resource", "key": [1, 2, 3]}
3234
with pytest.raises(FPMLValidationError):
3335
resolve_template(resource, {"key": "{{ Resource.key }}"}, {}, strict=True)
3436

3537

38+
def test_transformation_fails_on_accessing_props_of_undefined_resource_with_capital_letter_in_strict_mode() -> ( # noqa: E501
39+
None
40+
):
41+
resource: Resource = {"resourceType": "Resource", "key": [1, 2, 3]}
42+
with pytest.raises(FPMLValidationError):
43+
resolve_template(resource, {"key": "{{ UndefinedResource.key }}"}, {}, strict=True)
44+
45+
3646
def test_transformation_works_on_accessing_props_of_explicit_context_in_strict_mode() -> None:
3747
resource: Resource = {"list": [{"key": 1}, {"key": 2}, {"key": 3}]}
3848
result = resolve_template(
@@ -213,16 +223,19 @@ def test_assign_block_with_undefined_intermediate_values() -> None:
213223
"resourceType": "Resource",
214224
"sourceValue": 100,
215225
}
216-
assert resolve_template(
217-
resource,
218-
{
219-
"{% assign %}": [
220-
{"varA": "{{ {} }}"},
221-
{"varB": "{{ %varA }}"},
222-
],
223-
"valueA": "{{ %varB }}",
224-
},
225-
) == {}
226+
assert (
227+
resolve_template(
228+
resource,
229+
{
230+
"{% assign %}": [
231+
{"varA": "{{ {} }}"},
232+
{"varB": "{{ %varA }}"},
233+
],
234+
"valueA": "{{ %varB }}",
235+
},
236+
)
237+
== {}
238+
)
226239

227240

228241
def test_assign_block_multiple_vars_as_array_of_objects() -> None:

ts/server/src/core/extract.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('Transformation', () => {
99
).toThrow(FPMLValidationError);
1010
});
1111

12-
test.skip('fails on accessing props of resource (with capital letter) in strict mode', () => {
12+
test('fails on accessing props of resource (with capital letter) in strict mode', () => {
1313
expect(() =>
1414
resolveTemplate(
1515
{ resourceType: 'Resource', key: [1, 2, 3] },
@@ -22,6 +22,19 @@ describe('Transformation', () => {
2222
).toThrow(FPMLValidationError);
2323
});
2424

25+
test.skip('fails on accessing props of undefined resource (with capital letter) in strict mode (issue #27)', () => {
26+
expect(() =>
27+
resolveTemplate(
28+
{ resourceType: 'Resource', key: [1, 2, 3] },
29+
{ key: '{{ UndefinedResource.key }}' },
30+
{},
31+
null,
32+
null,
33+
true,
34+
),
35+
).toThrow(FPMLValidationError);
36+
});
37+
2538
test('works on accessing props of explicit context in strict mode', () => {
2639
expect(
2740
resolveTemplate(

ts/server/src/core/extract.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,24 @@ export class FPMLValidationError extends Error {
2323
}
2424
}
2525

26-
const guardedResource = new Proxy(
27-
{},
28-
{
26+
const guardedResourceFactory = (resource: any) =>
27+
new Proxy(resource, {
2928
get: (obj, prop) => {
30-
if (prop === '__path__' || prop === 'resourceType') {
29+
if (prop === '__path__') {
3130
return undefined;
3231
}
3332

33+
if (prop === 'resourceType') {
34+
return obj.resourceType;
35+
}
36+
3437
throw new Error(
3538
`Forbidden access to resource property ${String(
3639
prop,
3740
)} in strict mode. Use context instead`,
3841
);
3942
},
40-
},
41-
);
43+
});
4244

4345
export function resolveTemplate(
4446
resource: Resource,
@@ -50,7 +52,7 @@ export function resolveTemplate(
5052
): any {
5153
return resolveTemplateRecur(
5254
[],
53-
strict ? guardedResource : resource,
55+
strict ? guardedResourceFactory(resource) : resource,
5456
template,
5557
{ context: resource, ...(context ?? {}) },
5658
model,

0 commit comments

Comments
 (0)