-
Notifications
You must be signed in to change notification settings - Fork 1k
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
fix: /query
rest endpoint should return valid JSON (BREAKING CHANGE)
#3819
fix: /query
rest endpoint should return valid JSON (BREAKING CHANGE)
#3819
Conversation
fixes confluentinc#3408. Previously, the RESTful `/query` endpoint was returning invalid json. For example: ```json {"row":{"columns":["USER_1","PAGE_1",1,"1"], "errorMessage": null, "finalMessage": null,"terminal":true}}} {"row":{"columns":["USER_2","PAGE_2",2,"2"], "errorMessage": null, "finalMessage": null,"terminal":true}}} {"row": null, "errorMessage": null, "finalMessage":"Limit Reached","terminal":true}}" ``` Each _line_ contained a valid JSON object, but the payload as a whole was invalid and the parsing fails if a string contains an embedded new line character. The payload as a whole is now a valid JSON document, being a JSON array of rows, and can handle strings with embedded new line characters. In addition, I've hidden null fields and the 'terminal' field, which was never supposed to be in the JSON. The output now looks like: ```json {"row":{"columns":["USER_1","PAGE_1",1,"1"]}}, {"row":{"columns":["USER_2","PAGE_2",2,"2"]}}, {"finalMessage":"Limit Reached"}]" ``` The CLI is backwards compatible with older versions of the server, though it won't output column headings from older versions. BREAKING CHANGE: the response from the RESTful API for push queries has changed: it is now a valid JSON document containing a JSON array, where each element is JSON object containing either a row of data, an error message, or a final message. The `terminal` field has been removed.
Thank you @big-andy-coates! One high-level question, what happens if the query does not have the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM after the discussion we had in the meeting. Please get one more +1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Conflicting files ksql-rest-app/src/main/java/io/confluent/ksql/rest/server/resources/streaming/QueryStreamWriter.java ksql-rest-app/src/test/java/io/confluent/ksql/rest/integration/RestApiTest.java ksql-rest-app/src/test/java/io/confluent/ksql/rest/server/resources/streaming/StreamedQueryResourceTest.java
It's a valid question! The answer is that isn't not possible to reply with a valid JSON response if the response never ends. The HTTP standard says that a response with media type JSON should contain a single well formed JSON document or value. This is not possible for infinite result sets. Hence #3816 to look at redesigning our API. That said, the returned JSON is not invalid, it's just never ending / never completes. There are libraries that can happily parse such a stream of JSON, e.g. Jackson has a streaming API. |
NOTE: for the 5.4.x release the decision was made to NOT fix the badly formed JSON for existing push queries. This has only been fixed for pull queries :( Previously, the RESTful `/query` endpoint was returning invalid json. For example: ```json {"row":{"columns":["USER_1","PAGE_1",1,"1"], "errorMessage": null, "finalMessage": null,"terminal":true}}} {"row":{"columns":["USER_2","PAGE_2",2,"2"], "errorMessage": null, "finalMessage": null,"terminal":true}}} {"row": null, "errorMessage": null, "finalMessage":"Limit Reached","terminal":true}}" ``` Each _line_ contained a valid JSON object, but the payload as a whole was invalid and the parsing fails if a string contains an embedded new line character. The payload as a whole is now a valid JSON document, being a JSON array of rows, and can handle strings with embedded new line characters. In addition, I've hidden null fields and the 'terminal' field, which was never supposed to be in the JSON. The output now looks like: ```json {"row":{"columns":["USER_1","PAGE_1",1,"1"]}}, {"row":{"columns":["USER_2","PAGE_2",2,"2"]}}, {"finalMessage":"Limit Reached"}]" ``` The CLI is backwards compatible with older versions of the server, though it won't output column headings from older versions. BREAKING CHANGE: the response from the RESTful API for push queries has changed: it is now a valid JSON document containing a JSON array, where each element is JSON object containing either a row of data, an error message, or a final message. The `terminal` field has been removed. (cherry picked from commit 13ced13)
Description
fixes #3408 and, I think, Grafana's issues with our
/query
responsePreviously, the RESTful
/query
endpoint was returning invalid json. For example:Each line contained a valid JSON object, but the payload as a whole was invalid and the parsing fails if a string contains an embedded new line character.
The payload as a whole is now a valid JSON document, being a JSON array of rows, and can handle strings with embedded new line characters.
In addition, I've hidden null fields and the 'terminal' field, which was never supposed to be in the JSON. The output now looks like:
Note the opening
[
on the first row, the trailing,
each row and the final]
on the last row.Of course, for infinite queries, i.e. push queries with no termination/limit clause, the client will never see the terminal
]
. However, there are plenty of JSON parsers out there that will happily process such a stream of JSON, e.g. the Jackson streaming API.Note: this PR fixes the JSON for both the existing push queries and the pull queries added in #3820.
The CLI is backwards compatible with older versions of the server, though it won't output column headings from older versions.
BREAKING CHANGE: the response from the RESTful API for push queries has changed: it is now a valid JSON document containing a JSON array, where each element is JSON object containing either a row of data, an error message, or a final message. The
terminal
field has been removed.Testing done
tested added,
mvn test
and manual testing of CLIReviewer checklist