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

Alternative way to hide all routes on an Entity? #1697

Closed
paullallier opened this issue Jan 11, 2023 · 1 comment
Closed

Alternative way to hide all routes on an Entity? #1697

paullallier opened this issue Jan 11, 2023 · 1 comment

Comments

@paullallier
Copy link
Contributor

paullallier commented Jan 11, 2023

API-Platform Version 3.0.8, PHP 8.2.0, Symfony 6.2

I've got an entity which I want to only be assessable via another entity - in this case a table of invoice line items which is updated and read only by using the endpoint for the invoices.

Looking at the documentation, it suggests I can hide the endpoints for invoice_lines/ this way: https://api-platform.com/docs/core/operations/#expose-a-model-without-any-routes

I've discovered that I can also use this attribute: #[ApiResource(operations: [])]
This removes the endpoints from the swagger interface (though possibly not the "schemas" section at the bottom of the page). There's still a route for the GET item operation, but it returns this:

{
  "@context": "/contexts/Error",
  "@type": "hydra:Error",
  "hydra:title": "An error occurred",
  "hydra:description": "This route does not aim to be called."
}

This seems much easier (barring possibly some work to hide the schemas). Is that a valid method to use - and possibly worth documenting - or am I using accidental behaviour that might change at any point, please?

[Edit]
And a few caveats:

  • I am using my own StateProviders / StateProcessors and not using Doctrine ORM to retrieve data, so hiding the endpoint for invoice_lines/ doesn't break anything for me. I not sure how a Doctrine ORM Invoice entity would retrieve the InvoiceLines - and whether it relies on the invoice_lines/{id} GET endpoint?

  • My invoiceLine collection items, returned by the call to invoices/{id} still have IRIs - like invoice_line/1. These are obviously not actually usable, since the endpoint for them doesn't exist. Since I'm returning full details for the invoice lines with the invoice, this doesn't worry me too much - though I guess it's not entirely JSON-LD compliant.

@paullallier
Copy link
Contributor Author

I'm not sure why I thought I needed to do this. This bug: api-platform/core#5501, caused me to try without the annotation at all - which fixed the bug for me and seemed to work just as well.

I'll close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant