joomla / joomla-cms

Home of the Joomla! Content Management System
https://www.joomla.org
GNU General Public License v2.0
4.75k stars 3.64k forks source link

Joomla API URL Issue #44241

Open lushdomain opened 4 days ago

lushdomain commented 4 days ago

Steps to reproduce the issue

I am working on an app that uses the Joomla API to query articles in a specific category.

Example: api/index.php/v1/content/articles?filter[category]=2

However, this returns all articles regardless of their published state. Which in turn is causing other issues in the way I'm processing the returned json data.

That aside:

I used the filters option in the URL to only return those articles that are either published [1], or unpublished [0]

api/index.php/v1/content/articles?filter[category]=2&filter[state]=1&filter[state]=0

However, this does not work.

Regardless of the order I place the filters, only the last filter in the string is being returned.

Example 1: api/index.php/v1/content/articles?filter[category]=2&filter[state]=1&filter[state]=0

Returns only a list of unpublished articles

Example 2: api/index.php/v1/content/articles?filter[category]=2&filter[state]=0&filter[state]=1

Returns only a list of published articles

I also tried concatenated versions, which returned the json results, but again only the final part.

api/index.php/v1/content/articles?filter[category]=2&filter[state]=1,0

api/index.php/v1/content/articles?filter[category]=2&filter[state]=[0,1]

ETC...

It would seem from the online Postman documentation that Joomla can accept multiple filters in the same URL

For example when querying users:

Example: /api/index.php/v1/users?filter[groupid]=8&filter[state]=0&filter[registrationDateEnd]=2099-12-31T20%3A36%3A01Z' \

But, in this case for article retrieval maybe not, or there is a bug.

Expected result

The API to return all published and unpublished articles in the selected category

Actual result

Only articles linked to the last filter in the API url are being returned.

System information (as much as possible)

Joomla 5.1.4 php 8.2

Additional comments

brianteeman commented 4 days ago

When using filters I believe that the filters are published and category_id

not at my pc to check

richard67 commented 3 days ago

@lushdomain Try the following: api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0&filter[state][]=1. Mind the new array element [] after the filter[state]. Does that work?

lushdomain commented 3 days ago

@richard67 It works, but not as I'd expect. I did look at this option before, but couldn't get it to work either at the time. However, with pared down articles for testing, ie 2 (1 published and 1 unpublished) it does work to return both articles.

However, there is a new twist now.

/api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0 returns unpublished as well as published. /api/index.php/v1/content/articles?filter[category]=2&filter[state][]=1 returns published as well as unpublished.

/api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0&filter[state][]=1 returns unpublished and published.

The json response seems to place the unpublished article first regardless of how the url is structured, I don't know if this matters or is a normal response.

richard67 commented 3 days ago

However, there is a new twist now.

/api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0 returns unpublished as well as published. /api/index.php/v1/content/articles?filter[category]=2&filter[state][]=1 returns published as well as unpublished.

/api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0&filter[state][]=1 returns unpublished and published.

@lushdomain You have to use the [] only when specifying more than one value so the filter[state] shall be an array. When only filtering for one value, do not use []. If the following works for you then it works as desired:

/api/index.php/v1/content/articles?filter[category]=2&filter[state]=0

returns unpublished only.

/api/index.php/v1/content/articles?filter[category]=2&filter[state]=1

returns published only.

/api/index.php/v1/content/articles?filter[category]=2&filter[state][]=0&filter[state][]=1

returns unpublished and published but not archived and not trashed.

richard67 commented 3 days ago

In general use somearray[somekey]=value to assign one value to that somearray[somekey], so the somearray[somekey] is a scalar, and use somearray[somekey][]=value1&somearray[somekey][]=value2&somearray[somekey][]=value3 to assign mutliple values so the somearray[somekey] is an array.

Quy commented 3 days ago

Example: api/index.php/v1/content/articles?filter[category]=2

However, this returns all articles regardless of their published state. Which in turn is causing other issues in the way I'm processing the returned json data.

Are you sure? For me, it returns published and unpublished articles only without having to specify state filter.