[bugfix] RESTServer: set Content-Type for JSON results#6478
Merged
line-o merged 1 commit intoJun 15, 2026
Conversation
writeResultJSON never called response.setContentType(), so REST query results serialized as JSON (a POST <query method="json"> envelope) were returned without a Content-Type and output:media-type was silently ignored on that path — clients received JSON under the wrong media type (the "XML Parsing Error in console" class of failure). The XML writer (writeResultXML) already honors output:media-type. Set the Content-Type in writeResultJSON before the output stream is opened: default to application/json, but honor an explicit (non-XML- default) output:media-type. The logic lives in a small helper so the method gains no extra branching. Add a reusable MimeType.JSON_TYPE constant alongside XML_TYPE/HTML_TYPE/TEXT_TYPE. Tests (RESTServiceTest): postQueryJsonContentType asserts the default application/json; postQueryJsonExplicitMediaType asserts an explicit media-type is honored. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
duncdrum
approved these changes
Jun 15, 2026
line-o
approved these changes
Jun 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[This PR was co-authored with Claude Code. -Joe]
Summary
The REST server's JSON result writer (
RESTServer.writeResultJSON) never set the responseContent-Type. As a result, query results serialized as JSON — a POST<query method="json">(the JSON result envelope used by the query sandbox in eXide and other clients) — were returned without aContent-Typeheader, andoutput:media-typewas silently ignored on that path. Clients then received JSON under the wrong media type, which is the "XML Parsing Error in console" class of failure (browsers try to parse a JSON body as XML).The XML writer (
writeResultXML) already honorsoutput:media-typeand setsContent-Type; the JSON writer simply never did.Fix
In
writeResultJSON, set the responseContent-Typebefore the output stream is opened (mirroring the XML path):Content-Type, leave it;application/json, but honor an explicitoutput:media-typewhen the query set one to something other than the XML default (so a custom type likeapplication/vnd.api+jsonis respected).The logic lives in a small private helper (
setJsonResponseContentType) so the (already large)writeResultJSONmethod gains no extra branching. A reusableMimeType.JSON_TYPEconstant (application/json) is added alongside the existingXML_TYPE/HTML_TYPE/TEXT_TYPE.Tests
Two new cases in
RESTServiceTest:postQueryJsonContentType— amethod="json"query returnsContent-Type: application/json.postQueryJsonExplicitMediaType— an explicitmedia-typeis honored rather than overridden by theapplication/jsondefault.Both pass; the change introduces no new PMD findings (the touched method drops back under the NPath threshold).
Context
This is an independent eXist-core bug that surfaced in discussion of the eXide login work (the same underlying gap behind eXist-db/eXide#253). It's worth fixing on its own because the JSON result envelope is used by any REST/JSON client, not just eXide — eXide's own login has separately moved onto Roaster. A companion effort adds reusable Accept-header content negotiation to the
requestmodule; this PR is just the narrow Content-Type bug and stands alone.