Summary: | Add a parameter to recursively embed objects in Koha::Object(s)->to_api | ||
---|---|---|---|
Product: | Koha | Reporter: | Tomás Cohen Arazi (tcohen) <tomascohen> |
Component: | REST API | Assignee: | Agustín Moyano <agustinmoyano> |
Status: | CLOSED FIXED | QA Contact: | Martin Renvoize (ashimema) <martin.renvoize> |
Severity: | enhancement | ||
Priority: | P5 - low | CC: | 1joynelson, jonathan.druart, josef.moravec, kyle, lari.taskula, martin.renvoize, nick, oleonard, tomascohen |
Version: | Main | ||
Hardware: | All | ||
OS: | All | ||
Change sponsored?: | --- | Patch complexity: | Small patch |
Documentation contact: | Documentation submission: | ||
Text to go in the release notes: |
This patch introduces a parameter to the Koha::Object class ('embed') that should be a hashref pointing to a data structure following what's documented in the code. This parameter allows the caller to specify things to embed recursively in the API representation of the object. For example: you could request a biblio object with its items attached, like this:
$biblio_json = $biblio->to_api({ embed => { items => {} } });
The names specified for embedding, are used as attribute names on the resulting JSON object, and are expected to be class accessors.
The main use of this is the API, as introduced by bug 24302.
Koha::Objects->to_api is adjusted to pass its parameters down to the Koha::Object.
|
Version(s) released in: |
20.05.00, 19.11.02
|
Circulation function: | |||
Bug Depends on: | 23843, 23893 | ||
Bug Blocks: | 24302, 24366 | ||
Attachments: |
Bug 24228: Add parameters to Koha::Object->to_api to automatically embed objects
Bug 24228: Add parameters to Koha::Object(s)->to_api to automatically embed objects Bug 24228: Make ->to_api params a hashref Bug 24228: Make ->to_api params a hashref Bug 24228: Make ->to_api params a hashref Bug 24228: Add parameters to Koha::Object(s)->to_api to automatically embed objects Bug 24228: Make ->to_api params a hashref Bug 24228: (follow-up) Check wether each child or children can('to_api') and throw exception otherwise Bug 24228: Make ->to_api params a hashref Bug 24228: Add parameters to Koha::Object(s)->to_api to automatically embed objects Bug 24228: Make ->to_api params a hashref Bug 24228: Add parameters to Koha::Object(s)->to_api to automatically embed objects Bug 24228: Make ->to_api params a hashref |
Description
Tomás Cohen Arazi (tcohen)
2019-12-12 13:02:17 UTC
Created attachment 96543 [details] [review] Bug 24228: Add parameters to Koha::Object->to_api to automatically embed objects This patch adds the posibility to define 'x-koha-embed' parameter in swagger and in request headers. If 'x-koha-embed' header is present, in Koha::REST::V1::Auth it's compared with the whitelist defined in swagger through the 'x-koha-embed' parameter, placed at the same level of x-koha-authorization. Whitelist should be composed of DBIC relations (optinally chained by '.') that has the corresponding method in Koha::Object. To test: 1. Apply this patch 2. prove t/db_dependent/Koha/Object.t Created attachment 96612 [details] [review] Bug 24228: Add parameters to Koha::Object(s)->to_api to automatically embed objects This patch makes Koha::Object(s)->to_api have an 'embeds' parameter that using dot notation (e.g. resource.related_resource.another_one) allows embedding objects recursively. To test: 1. Apply this patch 2. prove t/db_dependent/Koha/Object.t Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Created attachment 96613 [details] [review] Bug 24228: Make ->to_api params a hashref This patch makes the parameters for Koha::Object(s)->to_api a hashref preparing the ground for new parameters. The proposed syntax is: $object->to_api( { embed => [ { accessor => 'items', children => [ ... ] } ] } ); Tests are added for the Koha::Objects implementation and tests for Koha::Object are adjusted to the new syntax, and a test for the single result accessor is added as well. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t t/db_dependent/Koha/Objects.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Created attachment 96615 [details] [review] Bug 24228: Make ->to_api params a hashref This patch makes the parameters for Koha::Object(s)->to_api a hashref preparing the ground for new parameters. The proposed syntax is: $object->to_api( { embed => [ { accessor => 'items', children => [ ... ] } ] } ); Tests are added for the Koha::Objects implementation and tests for Koha::Object are adjusted to the new syntax, and a test for the single result accessor is added as well. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t t/db_dependent/Koha/Objects.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Created attachment 96622 [details] [review] Bug 24228: Make ->to_api params a hashref This patch makes the parameters for Koha::Object(s)->to_api a hashref preparing the ground for new parameters. The proposed syntax is: $object->to_api( { embed => { 'items' => { 'children' => { 'homebranch' => {} } } } } ); Tests are added for the Koha::Objects implementation and tests for Koha::Object are adjusted to the new syntax, and a test for the single result accessor is added as well. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t t/db_dependent/Koha/Objects.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Changed the embed structure to ease x-koha-embed -> to_api param translation. Created attachment 96644 [details] [review] Bug 24228: Add parameters to Koha::Object(s)->to_api to automatically embed objects This patch makes Koha::Object(s)->to_api have an 'embeds' parameter that using dot notation (e.g. resource.related_resource.another_one) allows embedding objects recursively. To test: 1. Apply this patch 2. prove t/db_dependent/Koha/Object.t Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Agustin Moyano <agustinmoyano@theke.io> Created attachment 96645 [details] [review] Bug 24228: Make ->to_api params a hashref This patch makes the parameters for Koha::Object(s)->to_api a hashref preparing the ground for new parameters. The proposed syntax is: $object->to_api( { embed => { 'items' => { 'children' => { 'homebranch' => {} } } } } ); Tests are added for the Koha::Objects implementation and tests for Koha::Object are adjusted to the new syntax, and a test for the single result accessor is added as well. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t t/db_dependent/Koha/Objects.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Agustin Moyano <agustinmoyano@theke.io> Created attachment 96646 [details] [review] Bug 24228: (follow-up) Check wether each child or children can('to_api') and throw exception otherwise (In reply to Agustín Moyano from comment #9) > Created attachment 96646 [details] [review] [review] > Bug 24228: (follow-up) Check wether each child or children can('to_api') and > throw exception otherwise Agustín, I'm not sure about your follow-up. I think it might be possible that we want to embed things that are not strictly Koha::Object-derived. I would prefer to leave it to the controller or the OpenAPI plugin to validate the result. Also, there should be tests for the changed behavior. Created attachment 96685 [details] [review] Bug 24228: Make ->to_api params a hashref This patch makes the parameters for Koha::Object(s)->to_api a hashref preparing the ground for new parameters. The proposed syntax is: $object->to_api( { embed => { 'items' => { 'children' => { 'homebranch' => {} } } } } ); Tests are added for the Koha::Objects implementation and tests for Koha::Object are adjusted to the new syntax, and a test for the single result accessor is added as well. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t t/db_dependent/Koha/Objects.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Rebasing on top of 23893. Created attachment 96686 [details] [review] Bug 24228: Add parameters to Koha::Object(s)->to_api to automatically embed objects This patch makes Koha::Object(s)->to_api have an 'embeds' parameter that using dot notation (e.g. resource.related_resource.another_one) allows embedding objects recursively. To test: 1. Apply this patch 2. prove t/db_dependent/Koha/Object.t Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Agustin Moyano <agustinmoyano@theke.io> Created attachment 96687 [details] [review] Bug 24228: Make ->to_api params a hashref This patch makes the parameters for Koha::Object(s)->to_api a hashref preparing the ground for new parameters. The proposed syntax is: $object->to_api( { embed => { 'items' => { 'children' => { 'homebranch' => {} } } } } ); Tests are added for the Koha::Objects implementation and tests for Koha::Object are adjusted to the new syntax, and a test for the single result accessor is added as well. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t t/db_dependent/Koha/Objects.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Created attachment 96954 [details] [review] Bug 24228: Add parameters to Koha::Object(s)->to_api to automatically embed objects This patch makes Koha::Object(s)->to_api have an 'embeds' parameter that using dot notation (e.g. resource.related_resource.another_one) allows embedding objects recursively. To test: 1. Apply this patch 2. prove t/db_dependent/Koha/Object.t Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Agustin Moyano <agustinmoyano@theke.io> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Created attachment 96955 [details] [review] Bug 24228: Make ->to_api params a hashref This patch makes the parameters for Koha::Object(s)->to_api a hashref preparing the ground for new parameters. The proposed syntax is: $object->to_api( { embed => { 'items' => { 'children' => { 'homebranch' => {} } } } } ); Tests are added for the Koha::Objects implementation and tests for Koha::Object are adjusted to the new syntax, and a test for the single result accessor is added as well. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t t/db_dependent/Koha/Objects.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> The uses here are clear and the code is mostly simple to follow.. Passing QA and pushing, no doubt there will be refinements down the line but there's nothing that stands out as introducing regressions or bad practice. Nice work everyone! Pushed to master for 20.05 Pushed to 19.11.x branch for 19.11.02 What does $curr mean? (In reply to Jonathan Druart from comment #20) > What does $curr mean? ok, it's current I guess. |