SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
807 stars 171 forks source link

RESTHeart issue with filters #16

Closed dShringi closed 9 years ago

dShringi commented 9 years ago

I've been using RESTHeart on top of mongodb to have crud support with Mongodb's REST interface. Its working fine when I'm not using any filter, however when I tried to apply filter with the HTTP GET request as provided below:

Request: http://127.0.0.1:8080/inBeta/donor?filter="{'name':'john'}"

I'm getting the below error:

14:15:49.373 [XNIO-1 task-1] ERROR c.s.restheart.handlers.ErrorHandler - error handling the request java.lang.ClassCastException: java.lang.String cannot be cast to org.bson.BSONObject at com.softinstigate.restheart.db.CollectionDAO.lambda$getCollectionData$45(CollectionDAO.java:178) ~[restheart.jar:0.9.7] at com.softinstigate.restheart.db.CollectionDAO$$Lambda$20/1288164368.accept(Unknown Source) ~[na:na] at java.util.ArrayDeque$DeqSpliterator.forEachRemaining(Unknown Source)~[na:1.8.0_31] at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source) ~[na:1.8.0_31] at com.softinstigate.restheart.db.CollectionDAO.getCollectionData(CollectionDAO.java:177) ~[restheart.jar:0.9.7] When I didn't apply any filter its returning JSON object, however with filters its returning BSON object which RESTHeart is unable to convert as JSON response. Will appreciate any help or direction to look into the issue.

ujibang commented 9 years ago

http://127.0.0.1:8080/inBeta/donor?filter={'name':'john'} should work, can you confirm?

note that the value of the query parameter filter is specified without the "" (quotation marks).

in any case, this should not lead to an error code 500 (internal server error), I'm going to fix it.

ujibang commented 9 years ago

fixed in develop branch.

in case the filter parameter is not a valid json object, we get a the following response:

{ "http status code": 400, "http status description": "Bad Request", "message": "illegal filter paramenter, it is not a json object: \"{'name':'john'}\" => String" }

saurav955 commented 7 years ago

hi ujibang,

I am getting a similar issue with date. It seems I am missing something basic here. Can you please spare some time and kindly suggest me for the following.

http://192.168.124.12:8080/browser/#/db/coll?filter={"dateOfBirth" : { "$date" : "ISODate("2017-10-02T18:30:00Z")"}}&hal=f

{ "_exceptions": [ { "exception": "com.mongodb.util.JSONParseException", "exception message": "invalid json" } ], "http status code": 400, "http status description": "Bad Request", "message": "illegal filter paramenter: {'dateOfBirth' : { '$date' : 'ISODate('2017-10-02T18:30:00Z')'}}" }

ujibang commented 7 years ago

ISODate() is a mogno shell stuff.

you need { "$date" : { "$numberLong" : "1510318354000" } } 

that big number is the epoch time in milliseconds

for testing you can convert dates to epoch time using this tool https://www.epochconverter.com/

restheart handles mongodb types in the so called json "stric mode". more information at https://docs.mongodb.com/manual/reference/mongodb-extended-json/

for instance for dates: https://docs.mongodb.com/manual/reference/mongodb-extended-json/#date

saurav955 commented 7 years ago

Thanks for such a prompt reply. Really appreciate that.

As per link suggested by you: "The MongoDB JSON parser currently does not support loading ISO-8601 strings representing dates prior to the Unix epoch. When formatting pre-epoch dates and dates past what your system’s time_t type can hold, the following format is used:"

I tried using "{ 'dateOfBirth' : { '$date' : 'new Date("2017-10-02T18:30:00Z")'} }" but still the same result. But $eq does works or atleast doesn't give an error.

To me the problem seems to be with $date.

Thanks for epoch time suggestion. :)

ujibang commented 7 years ago

Aldo new Date() is a mongo Shell feature. You cannot really put a Javascript construct in a json payload and expect it to be evaluated.