biothings / biothings.api

BioThings API framework - Making high-performance API for biological annotation data
https://biothings.io
Apache License 2.0
45 stars 25 forks source link

Problem with jmespath: unexpected behavior with single items (not arrays) #323

Open colleenXu opened 6 months ago

colleenXu commented 6 months ago

I'm trying to query MyChem's drugcentral info and only keep drugcentral.bioactivity objects with the matching action_type value (POSITIVE MODULATOR). So I'm setting the parameter jmespath to drugcentral.bioactivity|[?action_type=='POSITIVE MODULATOR']

But if I try using that jmespath on this document C0017845_doesnt_work.json, I end up with drugcentral.bioactivity: null which is unexpected. (To retrieve this document yourself, use this GET query))

POST-query with chemical C0017845 that returns null

``` curl --location --globoff 'https://mychem.info/v1/query?size=1000&fields=drugcentral.bioactivity.uniprot.uniprot_id,drugcentral.bioactivity.action_type,drugcentral.bioactivity.act_source,drugcentral.bioactivity.organism&jmespath=drugcentral.bioactivity|[?action_type=='POSITIVE%20MODULATOR']' \ --header 'Content-Type: application/json' \ --data '{ "q": [ [ "C0017845", "POSITIVE MODULATOR"] ], "scopes": ["drugcentral.xrefs.umlscui", "drugcentral.bioactivity.action_type"] }' ```


VS when I try using jmespath on a different document C0018549_works.json, it works as-intended. (To retrieve this document yourself, use this GET query))

A POST query with chemical C0018549 that works as-intended

``` curl --location --globoff 'https://mychem.info/v1/query?size=1000&fields=drugcentral.bioactivity.uniprot.uniprot_id,drugcentral.bioactivity.action_type,drugcentral.bioactivity.act_source,drugcentral.bioactivity.organism&jmespath=drugcentral.bioactivity|[?action_type=='POSITIVE MODULATOR']' \ --header 'Content-Type: application/json' \ --data '{ "q": [ [ "C0018549", "POSITIVE MODULATOR"] ], "scopes": ["drugcentral.xrefs.umlscui", "drugcentral.bioactivity.action_type"] }' ```

I do notice a different between these two documents that may account for this:

If this is the key, I'd like jmespath to be able to gracefully handle both situations...

colleenXu commented 6 months ago

Another example is described in https://github.com/biothings/biothings_explorer/issues/316#issuecomment-2030832144

colleenXu commented 5 months ago

I've updated the opening post to match the discussions in Slack/add the json documents.

The proposed fix is to adjust the BioThings API's always_list parameter behavior (so it works before jmespath). Then adjust x-bte annotations to add it to queries using jmespath as a just-in-case. The syntax is always_list=drugcentral.bioactivity (aka path to the field that should be an array for jmespath to work on it).

colleenXu commented 2 months ago

Assigning to myself to review and see if this is fixed.