cms icon indicating copy to clipboard operation
cms copied to clipboard

Can't query a single entry or term on URI (REST-API)

Open christophstockinger opened this issue 1 year ago β€’ 8 comments

Bug description

When calling the API endpoint for a single entry or term, an exception is thrown and no result is returned.

Error message:

Object of class Statamic\Entries\Entry could not be converted to string

After an in-depth analysis, I found the following: Resource controllers are used, which already give the complete collection as a parameter in the controller. However, it is assumed in the controller that an ID comes in and then a ::find() query is made on the facade.

This leads to an object to string collision.

Translated with DeepL.com (free version)

How to reproduce

Activate the API for a collection and call the API endpoint for an entry with the ID.

Logs

No response

Environment

Environment
Application Name: Konkav & K16
Laravel Version: 11.24.1
PHP Version: 8.3.11
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: k16-konkav-cms.test
Maintenance Mode: OFF
Timezone: UTC
Locale: de

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: file
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Statamic
Addons: 7
Sites: 5 (Konkav - DE, Konkav - EN, K16 - DE, K16 - EN, K16 - PL)
Stache Watcher: Enabled
Static Caching: Disabled
Version: 5.26.0 PRO

Statamic Addons
aryehraber/statamic-uuid: 2.3.0
jonassiewertsen/statamic-documentation: 2.0.0
morethingsdigital/personio: dev-main
morethingsdigital/statamic-nextjs: 0.0.1
studio1902/statamic-peak-browser-appearance: 3.5.0
studio1902/statamic-peak-seo: 8.16.0
studio1902/statamic-peak-tools: 6.3.2

Installation

Existing Laravel app

Additional details

I create a PR that fixes the problem.

christophstockinger avatar Sep 26 '24 09:09 christophstockinger

Which endpoint are you seeing the error on?

duncanmcclean avatar Sep 26 '24 10:09 duncanmcclean

@duncanmcclean Sorry for my late reply, but I've been very busy the last few days.

I have now looked at the whole thing again in detail and also checked my PR again and tried to find out why the tests are failing due to my changes.

Specifically, it's about the show endpoint of an

  • Entry (https://statamic.dev/rest-api#entry)
  • Taxonomy Term (https://statamic.dev/rest-api#taxonomy-term)

Currently, the following error is displayed when I call one of the two endpoints

Object of class Statamic\Entries\Entry could not be converted to string

I have patched my changes into my project for testing and called the endpoints there. The manual tests all worked as desired and the endpoints also delivered the correct results.

After some error analysis as to why the tests in the pipeline could not be executed, I noticed the following difference.

For the sake of simplicity, I will use the entry endpoint, but this is also the case with the taxonomy term endpoint.

Two parameters are transferred in the show method of the controller Statamic\Http\Controllers\API\CollectionEntriesController:

  • $collection
  • $handle (after my change it is called $entry)

If I now execute the request with my changes, I get an instance of Statamic\Entries\Collection for $collection and an instance of Statamic\Entries\Entry for $handle($entry). For comparison in the test, an instance of Statamic\Entries\Collection is also passed for $collection and a string for $handle($entry).

And this is exactly what the error message currently says. That it is trying to convert an object to a value that is actually a string.

This leads to the discrepancy and also to the fact that my changes work in a real application and the test fails. Personally, I have to say that I don't really have an overview of the test and would like your help.

christophstockinger avatar Sep 30 '24 20:09 christophstockinger

Can you provide an example of the URL you're making a request to which isn't working?

duncanmcclean avatar Sep 30 '24 21:09 duncanmcclean

Sure:

<domain>/api/collections/pages/entries/e040bead-54de-453b-8ae0-896136e94be7

or

<domain>/api/taxonomies/news_categories/terms/brand

christophstockinger avatar Oct 01 '24 05:10 christophstockinger

Thanks!

Currently, the following error is displayed when I call one of the two endpoints

Hmm, I can't seem to reproduce this error myself. πŸ€”

CleanShot 2024-10-01 at 12 21 32

Do you have any custom route model bindings which could be conflicting with Statamic's? What does your AppServiceProvider look like?

duncanmcclean avatar Oct 01 '24 11:10 duncanmcclean

Is your sandbox project laravel v11 or laravel v10?

christophstockinger avatar Oct 01 '24 13:10 christophstockinger

Laravel 11

duncanmcclean avatar Oct 01 '24 13:10 duncanmcclean

I have found the problem. Please change the variable in your .env to the following value CP_ROUTE=β€œβ€ or add it. If I have a value like β€œadmin” or β€œcp”, the above two API endpoints will work.

In our use case we use Statamic Headless and don't need a frontend, so we decided to make the CMS admin panel directly available on the β€œ/” path.

christophstockinger avatar Oct 01 '24 18:10 christophstockinger

This issue has not had recent activity and has been marked as stale β€” by me, a robot. Simply reply to keep it open and send me away. If you do nothing, I will close it in a week. I have no feelings, so whatever you do is fine by me.

github-actions[bot] avatar Dec 01 '24 02:12 github-actions[bot]