[Bug 44035] New: Negotiate request and response body formats in the REST API, replacing the XML/JSON hooks
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 Bug ID: 44035 Summary: Negotiate request and response body formats in the REST API, replacing the XML/JSON hooks Initiative type: --- Sponsorship --- status: Product: Koha Version: Main Hardware: All OS: All Status: NEW Severity: normal Priority: P5 - low Component: REST API Assignee: koha-bugs@lists.koha-community.org Reporter: martin.renvoize@openfifth.co.uk QA Contact: tomascohen@gmail.com CC: tomascohen@gmail.com Target Milestone: --- Background Bug 37762 added two hooks to Koha::REST::V1 so ISO18626 routes could use XML: - around_action rewrites any request body sent as application/xml into JSON, replacing $c->req->body. - after_dispatch rewrites any response with an application/xml content type from JSON back into XML. Both run for every route, whatever the route declares. The ISO18626 controller relies on the rewrite and calls decode_json( $c->req->body ). For plugins that implement a native XML protocol, such as an NCIP server, the controller never sees the message the client sent. Element attributes and ordering are lost, and the response hook dies if the plugin returns XML it built itself. Bug 43460 addressed this with an x-koha-raw-payload spec extension to opt a route out, plus a series of follow-ups (the conversion running twice because around_action also fires for the auth bridge, an unreachable 400, a 500 on XML responses). I think we over-reached in 37762, and 43460 is a patch over the underlying design. The routes already say what they want via consumes, produces and their body schema. The hooks ignore all of that and guess from one header. What we actually want Controllers should not make format choices, or even know which format was used. They should be able to ask for the request as a validated Perl structure and render a Perl structure back, and the REST layer should handle the format from Content-Type, Accept, consumes and produces. Mojolicious::Plugin::OpenAPI already does the checking half. It rejects a request whose Content-Type is not in consumes and a request whose Accept cannot be satisfied from produces, and it records the negotiated response type. What it cannot do is read or write anything but JSON, and it documents openapi.parse_request_body as the helper to override for other formats. Proposal Add a Koha::REST::Plugin::Payload plugin that wraps two OpenAPI plugin helpers with a small table of codecs keyed by media type (JSON and XML to start with): - openapi.parse_request_body decodes the body with the codec for its Content-Type, and that structure is what gets validated against the body schema. The request is never modified: $c->req->body stays the bytes the client sent. Form bodies, bodies declared as a string, and bodies that cannot be decoded fall through to the default parser, so validation still rejects them. - openapi.build_response_body encodes a structure rendered with render( openapi => ... ) using the codec for the type negotiated from Accept and produces. - A new $c->payload helper returns the request body as a structure whatever format arrived. It is the structure that was validated, coerced to the types the schema declares. A controller then looks the same regardless of format: my $c = shift->openapi->valid_input or return; my $data = $c->payload; ... return $c->render( status => 200, openapi => $result ); Adding a format later is a new codec entry and does not touch controllers. Spec-driven behaviour replaces the opt-out flag: - A body declared as an object is decoded and validated. The controller gets a structure. - A body declared as a string is left alone and the controller gets the raw bytes. This covers NCIP-style protocols where attributes and ordering matter. This removes both hooks, x-koha-raw-payload, and the $last guard and double-conversion problems that came with them. Conversion now happens during input validation, after the authentication bridge, so a request rejected there is never parsed. Design notes and open questions - $c->payload is deliberately not built on $c->validation->output. Bugs 33556, 40417 and 40424 moved us off it because the plugin author said it is an internal structure that is not stable. Controllers keep using normal Mojo accessors, with $c->payload as the format-agnostic one for routes that accept more than one format. Existing $c->req->json callers are unaffected and need no migration. - I looked at adding $c->req->xml for symmetry with $c->req->json. That would need adding a method to Mojo::Message::Request, either by monkey-patching it globally or by installing a subclass in an after_build_tx hook. Mojolicious does not emit after_build_tx for embedded apps, and the REST API is embedded in the staff and OPAC apps through Koha::App::Plugin::RESTV1, so the hook would silently do nothing there. - Third-party plugins: anything that currently reads the JSON-rewritten $c->req->body on an XML route would need to switch to $c->payload. That deserves a release-notes entry. The ISO18626 controller is the only in-tree case and is updated as part of this. - x-koha-raw-payload was added by bug 43460. If it has not been in a release we can drop it cleanly. If it has, we should decide between removing it and keeping it as a documented no-op for a cycle. - In core, the old after_dispatch conversion appears to have been effectively unused. ISO18626 renders its own XML with a content type of "application/xml; charset=UTF-8", which the hook's exact match on "application/xml" never matched. I have not audited third-party plugins for it. - With codecs applied by negotiated type, error bodies on an XML-producing route will come out as XML too. This needs to_xml to behave sensibly for multi-key hashes, which currently pick an arbitrary root (see the "Move parse_xml and to_xml to Koha::Util::XML" bug this depends on). I would like opinions on whether errors should follow the negotiated format or always be JSON. - XML is schema-guided. A lone element versus a list of one is ambiguous in XML, so the XML codec uses the spec definitions to decide which properties are arrays. JSON never needs this. The codec interface leaves room for passing the body schema rather than the global definitions. - XML element attributes are still not represented in the decoded structure. Protocols where they matter should declare the body as a string. -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Depends on| |44034, 37762, 43460 Referenced Bugs: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=37762 [Bug 37762] Expand ILL to allow for supplying agency/lending library workflows https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43460 [Bug 43460] REST API XML payload conversion breaks plugin API routes that consume XML https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44034 [Bug 44034] Move parse_xml and to_xml out of Koha::REST::V1 into Koha::Util::XML -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 --- Comment #1 from Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> --- Created attachment 211065 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211065&action=edit Bug 44035: Negotiate request and response body formats in the OpenAPI helpers Bug 37762 made Koha::REST::V1 rewrite every request body sent as application/xml into JSON in an around_action hook, and every application/xml response from JSON back to XML in an after_dispatch hook. They applied to all routes regardless of what the route declared, which destroyed the body for plugins with a native XML protocol (NCIP), and bug 43460 had to add an x-koha-raw-payload spec extension to opt out. Mojolicious::Plugin::OpenAPI already checks Content-Type against consumes and Accept against produces, but only knows how to read and write JSON. This adds Koha::REST::Plugin::Payload, which wraps its openapi.parse_request_body and openapi.build_response_body helpers with a table of codecs keyed by media type (JSON and XML to start with): * the request body is decoded with the codec for its content type and that is what gets validated against the body schema. The request itself is never modified. * a structure rendered with render( openapi => ... ) is encoded with the codec for the type negotiated from Accept and produces. Controllers no longer care which format was used. They call $c->payload for the request as a structure and render( openapi => $structure ) for the response. $c->payload is the structure that was validated, coerced to the types the schema declares, whichever format it arrived in. It deliberately does not rely on $c->validation->output, which we moved away from in bugs 33556, 40417 and 40424 as the OpenAPI plugin does not keep it stable. What a route does is driven by its spec: declare the body as a string to get the XML untouched, or as an object to get a structure. x-koha-raw-payload is no longer needed and is removed. The ISO18626 controller reads $c->payload instead of decoding the rewritten request body. To test: 1. prove t/db_dependent/Koha/REST/V1.t \ t/db_dependent/Koha/REST/Plugin/PluginRoutes.t \ t/db_dependent/api/v1/iso18626/ Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Depends on|37762 | Referenced Bugs: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=37762 [Bug 37762] Expand ILL to allow for supplying agency/lending library workflows -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Assignee|koha-bugs@lists.koha-commun |martin.renvoize@openfifth.c |ity.org |o.uk Status|NEW |Needs Signoff -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Blocks| |44038 Referenced Bugs: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44038 [Bug 44038] Decide how controllers read request bodies: $c->payload versus $c->req->json -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Depends on| |37762 Referenced Bugs: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=37762 [Bug 37762] Expand ILL to allow for supplying agency/lending library workflows -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Depends on|37762 | Referenced Bugs: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=37762 [Bug 37762] Expand ILL to allow for supplying agency/lending library workflows -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |dcook@prosentient.com.au -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |pedro.amorim@openfifth.co.u | |k -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 --- Comment #2 from Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> --- Created attachment 211087 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211087&action=edit Bug 44035: (follow-up) Rename payload to validated_payload and drop the fallback The helper returns what the OpenAPI plugin validated, coerced to the types the body schema declares. That is what sets it apart from $c->req->body, the raw bytes, and $c->req->json, a plain parse of them, so the name should say so. $c->validated_payload does. The name also exposed a hole: when nothing had been stashed yet, the helper fell back to parsing the body itself. That value was neither validated nor coerced, so called before $c->openapi->valid_input it quietly returned something the name would no longer describe. The fallback is gone. Without validation there is nothing validated to return, so a warning is logged and undef is returned, and the raw body is still $c->req->body. Considered and not done: exposing it as $c->openapi->validated_payload, next to the OpenAPI plugin's own helpers. It would avoid adding a top-level helper name, but it is longer to type in every controller, and the plugin does not own this value. It would be a small change to register it there too if we prefer that. To test: 1. prove t/db_dependent/Koha/REST/V1.t \ t/db_dependent/Koha/REST/Plugin/PluginRoutes.t \ t/db_dependent/api/v1/iso18626/ Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 David Nind <david@davidnind.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|Needs Signoff |Signed Off -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 David Nind <david@davidnind.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #211065|0 |1 is obsolete| | Attachment #211087|0 |1 is obsolete| | -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 --- Comment #3 from David Nind <david@davidnind.com> --- Created attachment 211128 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211128&action=edit Bug 44035: Negotiate request and response body formats in the OpenAPI helpers Bug 37762 made Koha::REST::V1 rewrite every request body sent as application/xml into JSON in an around_action hook, and every application/xml response from JSON back to XML in an after_dispatch hook. They applied to all routes regardless of what the route declared, which destroyed the body for plugins with a native XML protocol (NCIP), and bug 43460 had to add an x-koha-raw-payload spec extension to opt out. Mojolicious::Plugin::OpenAPI already checks Content-Type against consumes and Accept against produces, but only knows how to read and write JSON. This adds Koha::REST::Plugin::Payload, which wraps its openapi.parse_request_body and openapi.build_response_body helpers with a table of codecs keyed by media type (JSON and XML to start with): * the request body is decoded with the codec for its content type and that is what gets validated against the body schema. The request itself is never modified. * a structure rendered with render( openapi => ... ) is encoded with the codec for the type negotiated from Accept and produces. Controllers no longer care which format was used. They call $c->payload for the request as a structure and render( openapi => $structure ) for the response. $c->payload is the structure that was validated, coerced to the types the schema declares, whichever format it arrived in. It deliberately does not rely on $c->validation->output, which we moved away from in bugs 33556, 40417 and 40424 as the OpenAPI plugin does not keep it stable. What a route does is driven by its spec: declare the body as a string to get the XML untouched, or as an object to get a structure. x-koha-raw-payload is no longer needed and is removed. The ISO18626 controller reads $c->payload instead of decoding the rewritten request body. To test: 1. prove t/db_dependent/Koha/REST/V1.t \ t/db_dependent/Koha/REST/Plugin/PluginRoutes.t \ t/db_dependent/api/v1/iso18626/ Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Signed-off-by: David Nind <david@davidnind.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 --- Comment #4 from David Nind <david@davidnind.com> --- Created attachment 211129 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211129&action=edit Bug 44035: (follow-up) Rename payload to validated_payload and drop the fallback The helper returns what the OpenAPI plugin validated, coerced to the types the body schema declares. That is what sets it apart from $c->req->body, the raw bytes, and $c->req->json, a plain parse of them, so the name should say so. $c->validated_payload does. The name also exposed a hole: when nothing had been stashed yet, the helper fell back to parsing the body itself. That value was neither validated nor coerced, so called before $c->openapi->valid_input it quietly returned something the name would no longer describe. The fallback is gone. Without validation there is nothing validated to return, so a warning is logged and undef is returned, and the raw body is still $c->req->body. Considered and not done: exposing it as $c->openapi->validated_payload, next to the OpenAPI plugin's own helpers. It would avoid adding a top-level helper name, but it is longer to type in every controller, and the plugin does not own this value. It would be a small change to register it there too if we prefer that. To test: 1. prove t/db_dependent/Koha/REST/V1.t \ t/db_dependent/Koha/REST/Plugin/PluginRoutes.t \ t/db_dependent/api/v1/iso18626/ Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Signed-off-by: David Nind <david@davidnind.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 --- Comment #5 from David Cook <dcook@prosentient.com.au> --- Why not just override the methods in https://metacpan.org/release/JHTHORSEN/Mojolicious-Plugin-OpenAPI-5.05/view/... like the documentation suggests? -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44035 --- Comment #6 from David Cook <dcook@prosentient.com.au> --- (In reply to David Cook from comment #5)
Why not just override the methods in https://metacpan.org/release/JHTHORSEN/Mojolicious-Plugin-OpenAPI-5.05/view/ lib/Mojolicious/Plugin/OpenAPI/Parameters.pm like the documentation suggests?
""" openapi.build_response_body $bytes = $c->openapi->build_response_body(Mojo::Asset->new); $bytes = $c->openapi->build_response_body($data); Takes validated data and turns it into bytes that will be used as HTTP response body. This method is useful to override, in case you want to render some other structure than JSON. openapi.parse_request_body $hash_ref = $c->openapi->parse_request_body; Returns a structure representing the request body. The default is to parse the input as JSON: {content_type => "application/json", exists => !!$c->req->body_size, value => $c->req->json}; This method is useful to override, in case you want to parse some other structure than JSON. """ -- You are receiving this mail because: You are watching all bug changes.
participants (1)
-
bugzilla-daemon@bugs.koha-community.org