Skip to content

Commit

Permalink
Quick and dirty fix for nested provider fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Jun 1, 2020
1 parent 758baac commit e25514a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion optimade/server/mappers/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ def alias_for(cls, field: str) -> str:
:return: Aliased field as found in PROVIDER_ALIASES + ALIASES
:rtype: str
"""
return dict(cls.all_aliases()).get(field, field)
split = field.split(".")
prop = split[0]
subprop = split[1:] if len(split) > 1 else []
alias = dict(cls.all_aliases()).get(prop, None)
if alias is not None:
return alias + ("." + ".".join(subprop) if subprop else "")
else:
return field

@classmethod
def get_required_fields(cls) -> set:
Expand Down
12 changes: 12 additions & 0 deletions tests/filtertransformers/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ class MyStructureMapper(BaseResourceMapper):
("A", "D"),
("B", "E"),
("C", "F"),
("_exmpl_nested_field", "nested_field"),
)

mapper = MyStructureMapper()
Expand All @@ -564,6 +565,17 @@ class MyStructureMapper(BaseResourceMapper):
test_filter = ["A", "elements", "C"]
self.assertEqual(t.postprocess(test_filter), ["A", "elements", "C"])

test_filter = {"_exmpl_nested_field.sub_property": {"$gt": 1234.5}}
self.assertEqual(
t.postprocess(test_filter), {"nested_field.sub_property": {"$gt": 1234.5}}
)

test_filter = {"_exmpl_nested_field.sub_property.x": {"$exists": False}}
self.assertEqual(
t.postprocess(test_filter),
{"nested_field.sub_property.x": {"$exists": False}},
)

def test_list_properties(self):
""" Test the HAS ALL, ANY and optional ONLY queries.
Expand Down

0 comments on commit e25514a

Please sign in to comment.