Skip to content

Commit

Permalink
fix response serialization for leading underscore keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeddy committed Jul 24, 2019
1 parent c94a0bf commit 1236c97
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
10 changes: 5 additions & 5 deletions synprov/graph/controllers/activities_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from synprov.config import neo4j_connection as graph
from synprov.graph import ActivityBuilder
from synprov.util import neo4j_to_d3
from synprov.util import neo4j_to_d3, convert_keys


def create_activity(body=None): # noqa: E501
Expand All @@ -26,7 +26,7 @@ def create_activity(body=None): # noqa: E501
)
act_node = builder.save()
print(act_node)
return humps.camelize({
return convert_keys({
'id': str(act_node.identity),
'labels': list(act_node.labels),
'properties': dict(act_node)
Expand Down Expand Up @@ -66,7 +66,7 @@ def get_activities_graph(sort_by=None, order=None, limit=None): # noqa: E501
results = graph.run(
query_base,
)
return humps.camelize(neo4j_to_d3(results.data()))
return convert_keys(neo4j_to_d3(results.data()))


def get_agent_subgraph(id, sort_by=None, order=None, limit=None): # noqa: E501
Expand Down Expand Up @@ -105,7 +105,7 @@ def get_agent_subgraph(id, sort_by=None, order=None, limit=None): # noqa: E501
query_base,
id=id
)
return humps.camelize(neo4j_to_d3(results.data()))
return convert_keys(neo4j_to_d3(results.data()))


def get_reference_subgraph(id,
Expand Down Expand Up @@ -155,4 +155,4 @@ def get_reference_subgraph(id,
query_base,
id=id
)
return humps.camelize(neo4j_to_d3(results.data()))
return convert_keys(neo4j_to_d3(results.data()))
7 changes: 4 additions & 3 deletions synprov/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def _deserialize_dict(data, boxed_type):
return {k: _deserialize(v, boxed_type)
for k, v in six.iteritems(data)}

def _convert_keys(obj):

def convert_keys(obj):
"""
Convert keys in a dictionary (or nested dictionary) from snake_case
to camelCase; ignore '_id' keys.
Expand All @@ -161,11 +162,11 @@ def _convert_keys(obj):
:rtype: dict, list
"""
if isinstance(obj, list):
return [_convert_keys(i) for i in obj]
return [convert_keys(i) for i in obj]
elif isinstance(obj, dict):
return {(humps.camelize(k.lstrip('_'))
if not re.search('^_id', k)
else k): _convert_keys(obj[k])
else k): convert_keys(obj[k])
for k in obj}
else:
return obj
Expand Down

0 comments on commit 1236c97

Please sign in to comment.