core icon indicating copy to clipboard operation
core copied to clipboard

Issue with link when creating (POST) a resource using a subresource

Open loic425 opened this issue 3 years ago • 2 comments

API Platform version(s) affected: 2.7.x

Description

#[ApiResource(
    uriTemplate: '/libraries/{id}/books',
    uriVariables: [
        'id' => new Link(
            fromClass: Library::class,
            toProperty: 'library',
        )
    ],
    operations: [new GetCollection(), new Post()]
)]
class Book

Route

_api_/libraries/{id}/books_get_collection   GET      ANY      ANY    /api/libraries/{id}/books 
_api_/libraries/{id}/books_post             POST     ANY      ANY    /api/libraries/{id}/books

Doctrine query to create the link to library

SELECT b0_.id AS id_0, b0_.name AS name_1, b0_.created_at AS created_at_2, b0_.library_id AS library_id_3 
FROM book b0_ 
INNER JOIN library l1_ 
    ON b0_.library_id = l1_.id 
WHERE l1_.id = ?"

This query returns more than one row to get the library.

How to reproduce

You can use this "testing" project to reproduce the bug => https://github.com/loic425/test-api-platform-2.7/blob/main/tests/Controller/BookApiTest.php#L65

loic425 avatar Apr 12 '22 08:04 loic425

I don't get why POST would try to read data, for now we didn't try to use subresources to do writes.

soyuka avatar Apr 22 '22 12:04 soyuka

@soyuka Yes, but that'd be awesome ;)

loic425 avatar Apr 26 '22 10:04 loic425

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 04 '22 21:11 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 04 '23 00:01 stale[bot]