How do I setup a POST /resources/{id}/subresource endpoint?
See original GitHub issueI’ve got the following entities / relationships…
class Service {
}
class Shortlist {
/**
* @var Collection<Service>
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Service")
* @ApiSubresource()
*/
private $services;
}
I want to setup an endpoint where I can add an (existing) services to an (existing) shortlist. My thoughts where that I would be able to setup the POST /resources/{id}/subresource
endpoint to do this, with the body something like…
{
"@id": "/services/{id}",
}
How can I do this in a way that will automatically be included in the API swagger docs?
I’ve tried adding the following to the Shortlist
class…
/**
* @ApiResource(
* ...
* subresourceOperations={
* "shortlist_post_service"={"method"="POST", "route_name"="api_shortlist_post_service"}
* },
* ...
* )
*/
class Shortlist {
}
… and setup the action…
<?php
declare(strict_types=1);
namespace AppBundle\Action\Shortlist;
use AppBundle\Entity\Service;
use AppBundle\Entity\Shortlist;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class PostServiceAction
{
/**
* Retrieves your own User
*
* @Route(
* name="api_shortlist_post_service",
* path="/shortlists/{id}/services.{_format}",
* defaults={"_format"=null, "_api_resource_class"=Shortlist::class, "_api_collection_operation_name"="post"}
* )
* @Method("POST")
*
* @param Service $data
*
* @return Service
*/
public function __invoke(Service $data)
{
// ...
}
}
… but this didn’t add the route to the Swagger docs.
Any help or advise on this (or a better way of performing this action) is much appreciated. Perhaps just pointers on how to add custom Swagger doc details?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:16
- Comments:16 (6 by maintainers)
Top Results From Across the Web
REST: Creating resources - mscharhag
Resources are typically created by sending a POST request to the parent collection resource. This creates a new subordinate resources with a ...
Read more >GET /help/resources/{resource_id} | REST API Version 13.1 ...
Retrieves a single resource documentation object. ... endpoint_ids - Array of Numbers - A list of endpoint documentation IDs for endpoints on this...
Read more >Getting a resource using another resource's id naming in ...
Usually a resource is uniquely identified by one single identifier. In your case that would be the user_id . So your GET /users/{user_id} ......
Read more >REST API Design Best Practices for Sub and Nested Resources
First, we will look into the reasons that speak for nested resources. After that, we will talk about the reasons that make nested...
Read more >Resource IDs - Smile CDR Documentation
Every FHIR resource stored in the repository has a resource ID. ... using an HTTP POST (i.e. a FHIR "create") the server will...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hello, After almost 4 years, this feature is really missing to the project. Being able to control subresources from the parent resource would be way simpler than having to specify the parent id in the request. Aka :
IMO is way better and simpler to handle than
I’m really looking to see this in the near future!
you can’t for now subresources are not writable atm 😃.
All you can do is to add a custom route like the above. Too enable it it swagger try to link the ApiResource with your custom route!