Skip to content

Commit 55befcb

Browse files
committed
hotfix(datasource): apply defaults, coercion and patch correctly
1 parent 814c49f commit 55befcb

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

polytope_server/common/datasource/datasource.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,23 @@ def match(ds_config, coerced_ur: Dict[str, Any], user: User) -> str:
6767
if roles and not user.has_access(roles):
6868
return f"Skipping datasource {DataSource.repr(ds_config)}: user not authorized."
6969

70-
coerced_ur = copy.deepcopy(coerced_ur) # don't want to modify the original request
70+
coerced_ur_copy = copy.deepcopy(coerced_ur) # don't want to modify the original request
7171
# apply defaults
7272
defaults = ds_config.get("defaults", {})
7373
if "date" not in defaults:
7474
defaults["date"] = "-1" # today, default for mars
7575
for k, v in defaults.items():
76-
if k not in coerced_ur:
77-
coerced_ur[k] = coerce_value(k, v)
76+
if k not in coerced_ur_copy:
77+
coerced_ur_copy[k] = coerce_value(k, v)
7878

7979
# check match rules
8080
if ds_config.get("type") == "polytope":
81-
if "feature" not in coerced_ur:
81+
if "feature" not in coerced_ur_copy:
8282
return (
8383
f"Skipping datasource {DataSource.repr(ds_config)}: "
8484
"request does not contain expected key 'feature'"
8585
)
86-
elif "feature" in coerced_ur:
86+
elif "feature" in coerced_ur_copy:
8787
return (
8888
f"Skipping datasource {DataSource.repr(ds_config)}: "
8989
"request contains key 'feature', but this is not expected by the datasource."
@@ -94,7 +94,7 @@ def match(ds_config, coerced_ur: Dict[str, Any], user: User) -> str:
9494

9595
# An empty match rule means that the key must not be present
9696
if allowed_values is None or len(allowed_values) == 0:
97-
if rule_key in coerced_ur:
97+
if rule_key in coerced_ur_copy:
9898
return (
9999
f"Skipping datasource {DataSource.repr(ds_config)}: "
100100
f"request containing key '{rule_key}' is not allowed."
@@ -103,7 +103,7 @@ def match(ds_config, coerced_ur: Dict[str, Any], user: User) -> str:
103103
continue # no more checks to do
104104

105105
# Check that the required key exists
106-
if rule_key not in coerced_ur:
106+
if rule_key not in coerced_ur_copy:
107107
return (
108108
f"Skipping datasource {DataSource.repr(ds_config)}: "
109109
f"request does not contain expected key '{rule_key}'"
@@ -112,7 +112,7 @@ def match(ds_config, coerced_ur: Dict[str, Any], user: User) -> str:
112112
# Process date rules
113113
if rule_key == "date":
114114
try:
115-
date_check(coerced_ur["date"], allowed_values)
115+
date_check(coerced_ur_copy["date"], allowed_values)
116116
except DateError as e:
117117
return f"Skipping datasource {DataSource.repr(ds_config)}: {e}."
118118
except Exception as e:
@@ -121,18 +121,24 @@ def match(ds_config, coerced_ur: Dict[str, Any], user: User) -> str:
121121

122122
# check that all values in request are allowed
123123
request_values = (
124-
[coerced_ur[rule_key]] if not isinstance(coerced_ur[rule_key], (list, tuple)) else coerced_ur[rule_key]
124+
[coerced_ur_copy[rule_key]]
125+
if not isinstance(coerced_ur_copy[rule_key], (list, tuple))
126+
else coerced_ur_copy[rule_key]
125127
)
126128
if not set(request_values).issubset(set(coerce_value(rule_key, allowed_values))):
127129
return (
128130
f"Skipping datasource {DataSource.repr(ds_config)}: "
129-
f"got {rule_key} : {coerced_ur[rule_key]}, but expected one of {allowed_values}"
131+
f"got {rule_key} : {coerced_ur_copy[rule_key]}, but expected one of {allowed_values}"
130132
)
131133
# If we reach here, the request matches the datasource
132-
# Downstream expects MARS-like format of request
133-
for key in coerced_ur:
134-
if isinstance(coerced_ur[key], list):
135-
coerced_ur[key] = "/".join(coerced_ur[key])
134+
for patch_k, patch_v in ds_config.get("patch", {}).items():
135+
coerced_ur_copy[patch_k] = patch_v
136+
# Downstream expects MARS-like format of request, apply to original request object
137+
for key in coerced_ur_copy:
138+
if isinstance(coerced_ur_copy[key], list):
139+
coerced_ur[key] = "/".join(coerced_ur_copy[key])
140+
else:
141+
coerced_ur[key] = str(coerced_ur_copy[key])
136142
return "success"
137143

138144
@staticmethod

0 commit comments

Comments
 (0)