Skip to content
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

Support empty collections in _embedded section #80

Merged
merged 38 commits into from
Aug 1, 2023
Merged

Support empty collections in _embedded section #80

merged 38 commits into from
Aug 1, 2023

Conversation

ghostwriter
Copy link
Contributor

@ghostwriter ghostwriter commented Jun 28, 2023

Q A
Bugfix yes

Description

The goal/convention is to include both non-empty and empty collections within the _embedded section of the response, to maintain consistency in the structure of the response, regardless of whether the collection is empty or not.

Discussion: https://laminas.slack.com/archives/C4RF57B0E/p1686214950164379

Maybe Related #4


empty-contacts-collection-not-embedded.json

{
  "contacts": []
}

empty-contacts-collection-embedded.json

{
  "_embedded": {
    "contacts": []
  }
}

non-empty-contacts-collection.json

{
  "_embedded": {
    "contacts": [
      {
        "id": 1,
        "name": "Jane",
        "email": "jane@example.com"
      },
      {
        "id": 2,
        "name": "John",
        "email": "john@example.com"
      }
    ]
  }
}

The user also explained that adding the code below to the constructor worked for them (in case we need to refactor the constructor)

if ($value === null || $value === []) {
    $this->embedded[$name] = [];
    return;
}

Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
@ghostwriter ghostwriter marked this pull request as draft June 28, 2023 22:26
@ghostwriter
Copy link
Contributor Author

Hey @weierophinney,

I would greatly appreciate your valuable insight to ensure its completeness, per your offer.

Thank you in advance. 🙏🏾

@ghostwriter ghostwriter marked this pull request as ready for review June 28, 2023 23:08
@froschdesign
Copy link
Member

@ghostwriter
I would not consider the missing of the _embedded section as an error, because the specification explicitly marks this element as optional. And if nothing is present, then the entire section can be omitted.
The specification only assumes existing objects:

…values are either a Resource Object or an array of Resource Objects.

https://datatracker.ietf.org/doc/html/draft-kelly-json-hal#section-4.1.2


The problem I see here is that there is a change in the output and this can cause errors in existing applications and tests.

@ghostwriter
Copy link
Contributor Author

ghostwriter commented Jul 13, 2023

@froschdesign I agree, the missing of the _embedded section is not the issue.

The pr/discussion is regarding: an empty array of Resource Objects.

non-empty and empty collections within the _embedded section of the response

Please look at the test fixtures for examples.

https://github.com/mezzio/mezzio-hal/pull/80/files

@froschdesign
Copy link
Member

@ghostwriter
I understand your suggestion, but the problem is still that the output changes and this can potentially cause tests to fail for existing API endpoints.

@weierophinney
Copy link
Contributor

the problem is still that the output changes and this can potentially cause tests to fail for existing API endpoints.

We have two competing issues:

  • A number of users would like empty collections to appear in the _embedded section, as it simplifies consumers of their APIs. They can rely on the value being present, even if empty, which can ensure that payloads are valid, and no extra logic has to be created to create an empty collection based on absence of the collection in the payload.
  • Doing so by default would cause issues for those who relied on empty collections either not being present or being in a different part of the payload.

I'd recommend we make this functionality opt-in, then. @ghostwriter , do you have any ideas on how you could adapt the PR to enable that?

@ghostwriter
Copy link
Contributor Author

ghostwriter commented Jul 14, 2023

my idea was to introduce this change as a bug fix with a breaking change in a new minor version and communicate the breaking change to your users and provide documentation on how to handle the change in their applications.

based on our discussion in Slack, I was under the impression we all wanted to include both non-empty and empty collections within the _embedded section of the response, to maintain consistency in the structure of the response, regardless of whether the collection is empty or not.

Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
@ghostwriter
Copy link
Contributor Author

based on both of your feedbacks, it now uses the configuration key config.mezzio-hal.embed-empty-collections to optionally enable and disable this behavior.

Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Copy link
Contributor

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great, @ghostwriter !

My primary concern at this point is that it looks like you rewrote existing tests that tested the existing behavior to test the behavior when the flag is toggled. Since we'll be doing both behaviors, we need to ensure we also test the default behavior.

docs/book/v2/links-and-resources.md Show resolved Hide resolved
src/HalResource.php Outdated Show resolved Hide resolved
src/ResourceGenerator.php Outdated Show resolved Hide resolved
test/HalResourceTest.php Outdated Show resolved Hide resolved
test/HalResourceTest.php Outdated Show resolved Hide resolved
test/HalResourceTest.php Outdated Show resolved Hide resolved
test/HalResourceTest.php Outdated Show resolved Hide resolved
ghostwriter and others added 8 commits July 31, 2023 11:17
Commit suggestion: The above makes the order of operations more clear. Additionally, our CS has us put operators on the next line.

Co-authored-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Co-authored-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Co-authored-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Co-authored-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Commit suggestion: Since the default behavior remains unchanged, we should note in the test that the tested behavior is specifically for when the embed empty collections option is toggled on.

Co-authored-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Add a note here that this feature is available since version 2.7.0.

Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Use constructor property promotion here

Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>

if (
$resource instanceof self ||
$resource === [] ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$resource === [] || because old behavior allows empty arrays.

Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
@ghostwriter
Copy link
Contributor Author

Ready for review.

Copy link
Member

@froschdesign froschdesign left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only minor changes to the documentation are necessary. Otherwise it looks good. 👍🏻

docs/book/v2/links-and-resources.md Outdated Show resolved Hide resolved
docs/book/v2/links-and-resources.md Outdated Show resolved Hide resolved
docs/book/v2/links-and-resources.md Outdated Show resolved Hide resolved
@froschdesign froschdesign added the Enhancement New feature or request label Aug 1, 2023
@froschdesign froschdesign added this to the 2.8.0 milestone Aug 1, 2023
ghostwriter and others added 4 commits August 1, 2023 05:27
commit suggestion

Co-authored-by: Frank Brückner <info@froschdesignstudio.de>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
commit suggestion

Co-authored-by: Frank Brückner <info@froschdesignstudio.de>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
commit suggestion

Co-authored-by: Frank Brückner <info@froschdesignstudio.de>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Fix Documentation Linting issues

Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
Copy link
Contributor

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found one little typo, but otherwise, all my feedback has been addressed! I'll apply that change and merge; thanks for the patch, @ghostwriter ! 🎉

test/HalResourceTest.php Outdated Show resolved Hide resolved
Signed-off-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
@weierophinney weierophinney merged commit 3d94ebd into mezzio:2.7.x Aug 1, 2023
14 checks passed
@weierophinney
Copy link
Contributor

ARGH - this was targeted at 2.7.x, but slated for 2.8.0. I'll figure out how to untangle it.

weierophinney added a commit that referenced this pull request Aug 1, 2023
…-collections-in-embedded-section"

This reverts commit 3d94ebd, reversing
changes made to 07dff20.
@ghostwriter ghostwriter deleted the feature/support-empty-collections-in-embedded-section branch August 1, 2023 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request Review Needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants