Skip to content

Conversation

@wilsonge
Copy link
Contributor

Pull Request for Issue #39495 .

Summary of Changes

Fixes linked bug

Testing Instructions

In a webservice plugin (either custom or in a core one in the core add the following lines to the onBeforeApiRoute event (i used the content webservice plugin - but any will do)

        \JFactory::getApplication()->addFormatMap('application/ld+json', 'jsonld');
        $router->addRoute(
            new Route(['GET'], 'v1/format', '', [], ['format' => ['application/vnd.api+json', 'application/ld+json'], 'component' => 'com_content'])
        );

Then in the libraries/src/Application/ApiApplication.php add var_dump($this->input->get('format'), $this->input->get('option'));die; after this foreach loop (both before and after testing)

Generate a Bearer token and curl the url e.g. curl -H "Authentication: Bearer <bearer_token>" -H "Accept: application/ld+json" http://localhost/joomla-cms/api/index.php/v1/format

Actual result BEFORE applying this Pull Request

        array(2) {
          [0]=>
          string(22) "applicationvnd.apijson"
          [1]=>
          string(17) "applicationldjson"
        }
        string(11) "com_content"

Expected result AFTER applying this Pull Request

        string(7) "jsonld"
        string(11) "com_content"

i.e. the second dump (the component) is unchanged and the former (the format) now contains the Joomla format and not the array of possible options from the plugin

Link to documentations

Please select:

  • Documentation link for docs.joomla.org:

  • No documentation changes for docs.joomla.org needed

  • Pull Request link for manual.joomla.org:

  • No documentation changes for manual.joomla.org needed

@nikosdion
Copy link
Contributor

I have tested this item ✅ successfully on 83ac935

It works!

Just note that if you have Accept: application/ld+json you get the format applicationldjson instead of the expected jsonld.

To fix that, the plugin needs to have the following in its onBeforeApiRoute handler (where routes are registered):

$this->getApplication()->addFormatMap('application/ld+json', 'jsonld');

Since this is the expected behaviour of ApiApplication I consider my test successful. Thank you, George!


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/39498.

@nikosdion
Copy link
Contributor

I re-read my test description and it's… unclear. What I meant to say is that if you do not follow George's instruction to use addFormatMap you will get the "wrong" format, which is the expected behaviour in the Joomla API application. Even though there's this little gotcha, since it's an expected and documented gotcha, it does not affect me on marking this test successful.

I'm gonna go get some coffee now.

@carlitorweb
Copy link
Member

I have tested this item ✅ successfully on 83ac935

Using a custom component and only adding 'format' => ['application/json']


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/39498.

@richard67
Copy link
Member

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/39498.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Dec 27, 2022
@roland-d roland-d merged commit 4a999e0 into joomla:4.2-dev Dec 28, 2022
@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Dec 28, 2022
@roland-d
Copy link
Contributor

Thank you

@roland-d roland-d added this to the Joomla! 4.2.7 milestone Dec 28, 2022
@wilsonge wilsonge deleted the fix/api-format-42 branch December 28, 2022 22:29
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

Successfully merging this pull request may close these issues.

6 participants