Beter Collection metadata extension #2696
signor-pedro
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
|
I believe I'm in a similar situation. Another application is attempting to generate its code based on the OpenAPI documentation. However, there is no schema for the pagination on the @signor-pedro, I really like your suggestion of having a |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi. I love API Platform and would like to use it on a commercial project.
However, there is one major flaw I found while experimenting with it 😢 there is no reasonable mechanism to add metadata to collection responses.
The
hydrastandard allows for arbitrary metada on collections. I tried all possible approaches over a week...Recommended solution
Currently, it is recommended to compose/decorate a Normalizer, e.g.
Problems:
❌ metadata are invisible in the Responses section of the endpoint documentation
❌ DTOs of metadata do not have a Schema generated
In order for the documentation to work properly, you have to:
SchemaFactory,OpenApiFactoryetc) so that an "Example response" corresponds with actual response schema.ApiResourceand expose no operations on it, so that the DTO is recognized in the "Schema" section of the docs.Ideal solution
Allow to implement a custom collection, e.g.
CustomPaginatorthat can contain other properties.#[GetCollection( provider: CollectionProvider, // built-in or custom, doesn't matter output: ItemDTO::class, // optionally, hydrate a DTO instead of an entity collectionOutput: CustomPaginator::class, // pass the hydrated entities/DTOs to a custom collection instance )]The custom paginator needs to conform to the
PaginatorInterface, but can define arbitrary properties. The items are passed into it using something like a new,PaginatedItemsInterface:The remaining piece of puzzle - some mechanism needs to serialize the Paginator, generate the schema&docs, ...
Rather than manually serializing the collection, as happens now in multiple places:


Instead:
jsonld,jsonetc) that adds thehydra:***annotations forjsonld, completely ignores metadata forjson, etc.jsonld) or ignores the metadata completely (json), ...Example of the final outcome
Facet search built with ElasticSearch:
Schema for the Example response docs is automatically generated:
{ "hydra:member": ["..."], "hydra:totalItems": 15, "hydra:view": { "@id": "string", "type": "string", "hydra:first": "string", "hydra:last": "string", "hydra:previous": "string", "hydra:next": "string" }, "hydra:search": { "@type": "string", "hydra:template": "string", "hydra:variableRepresentation": "string", "hydra:mapping": [ { "@type": "string", "variable": "string", "property": "string", "required": true } ] }, "facets": { "@context": "string", "@id": "string", "@type": "string", "company": [ { "@context": "string", "@id": "string", "@type": "string", "name": "string", "items": 0 } ] } }Actual response is properly built with correct IRIs for metadata as well:
{ "@context": "/api/contexts/SearchOffer", "@id": "/api/search_offers", "@type": "hydra:Collection", "hydra:totalItems": 1, "hydra:member": ["..."], "hydra:view": { "@id": "/api/search_offers?fulltext=php", "@type": "hydra:PartialCollectionView" }, "hydra:search": { "@type": "hydra:IriTemplate", "hydra:template": "/api/search_offers{?fulltext}", "hydra:variableRepresentation": "BasicRepresentation", "hydra:mapping": [ { "@type": "IriTemplateMapping", "variable": "fulltext", "property": "fulltext", "required": false } ] }, "facets": { "@type": "SearchOfferFacets", "@id": "/api/.well-known/genid/9b3baf0ebba142588980", "company": [ { "@type": "FacetBucket", "@id": "/api/.well-known/genid/ce2d0b1399b413efbf67", "name": "Microsoft", "items": 15 } ] } }Beta Was this translation helpful? Give feedback.
All reactions