scassandra / scassandra-server

Stubbed Cassandra
http://www.scassandra.org
Other
87 stars 34 forks source link

Priming query patterns through JSON API requires single quotes to be unicode encoded #136

Open jbridger opened 8 years ago

jbridger commented 8 years ago

When priming simple queries through the JSON API, single quotes must be unicode encoded. If we don't unicode encode the single quotes, it doesn't match against our query.

An example query we want to prime for:

INSERT INTO my_table (col1, col2) VALUES (val1, 'SOME_CONSTANT')

To prime for this in Java, we do the following, where single quotes are used as-is:

PrimingRequest.PrimingRequestBuilder prime = PrimingRequest.queryBuilder()
                .withQueryPattern("INSERT INTO my_table \\(col1, col2\\) VALUES \\(.+, 'SOME_CONSTANT' \\)")
                .withThen(then().withResult(PrimingRequest.Result.valueOf(exception)));

Debugging this, we can see that the use of GSON is encoding the single quotes as \u0027.

When we try to prime using the JSON API to SCassandra using the same query pattern is we would in Java, it does not match the query:

{
    "when": {
        "queryPattern": "INSERT INTO my_table \\(col1, col2\\) VALUES \\(.+, 'SOME_CONSTANT'\\)"
    },
    "then": {...}
}

If we encode the single quotes, this will match our query:

{
    "when": {
        "queryPattern": "INSERT INTO my_table \\(col1, col2\\) VALUES \\(.+, \u0027SOME_CONSTANT\u0027\\)"
    },
    "then": {...}
}

So as you can see, when priming with Java we don't need to encode the single quote (but does it behind the scenes), and we need to do it when using the JSON API. It would be nice if we didn't have to encode it, so we can have the same query patterns in Java and when sent directly through the JSON API. I haven't found anything in the JSON spec that requires the encoding of single quotes in the JSON object.