api-doc-parser icon indicating copy to clipboard operation
api-doc-parser copied to clipboard

fetchResource loads actual items from the API due to wrong queryParameter

Open Sirs0ri opened this issue 3 years ago • 4 comments

API Platform version(s) affected: 2.6.8

Description

The API doc parser uses fetch() via the fetchResource() method to load API metadata, and fails to apply the query parameter { itemsperpage: 0 } correctly to limit the request to only metadata and no API items.

fetchResource() tries to set the query parameter via a secondary options parameter, however fetch() doesn't support that - params have to be part of the url instead.

// Current code:
fetchJsonLd(
  resourceUrl,
  Object.assign({ itemsPerPage: 0 }, options)
)

// Fixed code:
fetchJsonLd(
  resourceUrl + "?itemsPerPage=0",
  options
)

How to reproduce

Possible Solution

Replace the Object.assign() call with an URL including the query parameter, possibly by building an URL:

const url = new URL(resource.url);
const params = new URLSearchParams(["itemsPerPage", 0]);
url.search = params.toString();

fetchJsonLd(url, options)

Additional Context

Please excuse that my code examples are in plain JS, not TS, since I'm not familiar enough with typescript.

Sirs0ri avatar Jul 01 '22 17:07 Sirs0ri

ping @soyuka @alanpoulain this one is a real bummer: in every list page of API Platform Admin, it causes a double fetch to the getList endpoint, one without params for introspection (but still returning data), and the other with the correct params.

fzaninotto avatar Jul 27 '24 08:07 fzaninotto

ping @soyuka @alanpoulain this one is a real bummer: in every list page of API Platform Admin, it causes a double fetch to the getList endpoint, one without params for introspection (but still returning data), and the other with the correct params.

actually it's not always just two. i have scenarios where it does this more than 10 times before and after the actual fetch with the query params... see example here: https://github.com/api-platform/admin/issues/627

zolex avatar Jun 07 '25 22:06 zolex

I'm unsure how we should fix this on an RDF point of view, ie while respecting hydra's specification. On discovery we don't know the pagination parameters, also using HTTP cache should definitely mitigate these as the discovery queries should already be cached by your browser.

soyuka avatar Jun 13 '25 08:06 soyuka

I'm unsure how we should fix this on an RDF point of view, ie while respecting hydra's specification. On discovery we don't know the pagination parameters, also using HTTP cache should definitely mitigate these as the discovery queries should already be cached by your browser.

what about adding an option to allow hooking into an actual collection request from the application, the doc-parser is used in?

zolex avatar Jun 13 '25 08:06 zolex