Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonQueryParser inaccurate parse value in JSON when value[0] is '!','<','>'... #285

Closed
johnzhou2011 opened this issue Apr 21, 2016 · 7 comments · Fixed by #358
Closed

jsonQueryParser inaccurate parse value in JSON when value[0] is '!','<','>'... #285

johnzhou2011 opened this issue Apr 21, 2016 · 7 comments · Fixed by #358

Comments

@johnzhou2011
Copy link

johnzhou2011 commented Apr 21, 2016

The jsonQueryParser should parser the key not value[0]
eg:
{messageId: "70666280047D7CD@front04.ibpmail.net\>A(addedAbyApostmaster@ibpmail.com)"}
will convert to
Mongoose: emails.find({"messageId":{"$lt":"570666280047D7CD@front04.ibpmail.net>A(addedAbyApostmaster@ibpmail.com)"}})
not
Mongoose: emails.find({"messageId": "<570666280047D7CD@front04.ibpmail.net>A(addedAbyApostmaster@ibpmail.com)"})

We need recursive parse keys to operators , currently jsonQueryParser is needless, all JSON should accrate to provide operators such as "$lt" as a key in JSON string.

// H+ - JSON query param parser
  // TODO - improve to serve recursive logical operators
  function jsonQueryParser (key, value) {
    if (_.isString(value)) {
      if (value[0] === '~') { // parse RegExp
        return new RegExp(value.substring(1), 'i')
      } else if (value[0] === '>') {
        if (value[1] === '=') {
          return {$gte: value.substr(2)}
        } else {
          return {$gt: value.substr(1)}
        }
      } else if (value[0] === '<') {
        if (value[1] === '=') {
          return {$lte: value.substr(2)}
        } else {
          return {$lt: value.substr(1)}
        }
      } else if (value[0] === '!' && value[1] === '=') {
        return {$ne: value.substr(2)}
      } else if (value[0] === '=') {
        return {$eq: value.substring(1)}
      }
    } else if (_.isArray(value)) {
      if (model.schema.paths.hasOwnProperty(key)) {
        return {$in: value}
      }
    }
    return value
  }
@Zertz
Copy link
Collaborator

Zertz commented Apr 21, 2016

This behavior is documented, though I do agree that there should at least be a way to turn it off.

As a side note, I see that you're using v0.7 or v1.x, which are unlikely to be patched.

@johnzhou2011
Copy link
Author

v1.1.1

@johnzhou2011
Copy link
Author

GET /Customer?query={"name":"<Zertz>"}
something like that is so risky that db will result almost all collections.

It's not a good way use JSON key-value of value to parse operators. Operators must be a key in JSON key-value.

@Zertz
Copy link
Collaborator

Zertz commented Apr 22, 2016

Like I said, I agree. We can't just remove it though because it's a breaking change, it would have to be behind an option. queryParser or something along those lines.

You're welcome to submit a PR :)

@johnzhou2011
Copy link
Author

ok, thanks . :)
Could u set the label as enhancement?

@Zertz
Copy link
Collaborator

Zertz commented Aug 22, 2016

I'm not sure how useful this is so it could probably be removed for 5.0 and @johnzhou2011 raised a good point, running a query like "name": "<Zertz" will, in most cases, match pretty much the entire collection. Of course, we have a default limit in place, but it's still awkward (impossible?) to query for the actual < character.

@florianholzapfel Thoughts?

@florianholzapfel
Copy link
Owner

actually it should be possible by explicitly specifying the operator. e.g. to query for "name": "<Zertz" by using the query name==<Zertz, but i agree: this is a little bit awkward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants