question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I setup a POST /resources/{id}/subresource endpoint?

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Reactions:16
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

13reactions
algorun77commented, Nov 18, 2021

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 :

POST /parents/{id}/subresources
{
    "some": "field",
    "anotherValue": 42
}

IMO is way better and simpler to handle than

POST /subresources
{
    "some": "field",
    "anotherValue": 42,
    "parent": "/parents/{id}"
}

I’m really looking to see this in the near future!

5reactions
soyukacommented, Feb 19, 2018

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found