Categories
CMS Drupal Mastering Development

Make custom POST REST Resource available on OpenApi

Body section in Custom Rest Post Resource is not shown

I’ve created a custom resource

with drupal generate:plugin:rest:resource

and the result is

<?php

namespace Drupal\baxter_rest\Plugin\rest\resource;

use Drupal\rest\ModifiedResourceResponse;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
 * Provides a resource to get view modes by entity and bundle.
 *
 * @RestResource(
 *   id = "post_patient_rest_resource",
 *   label = @Translation("Post patient rest resource"),
 *   uri_paths = {
 *     "create" = "/api/v1/create-patient"
 *   }
 * )
 */
class PostPatientRestResource extends ResourceBase {

  /**
   * A current user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->logger = $container->get('logger.factory')->get('baxter_rest');
    $instance->currentUser = $container->get('current_user');
    return $instance;
  }

    /**
     * Responds to POST requests.
     *
     * @param string $payload
     *
     * @return \Drupal\rest\ModifiedResourceResponse
     *   The HTTP response object.
     *
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
     *   Throws exception expected.
     */
    public function post($payload) {

        // You must to implement the logic of your REST Resource here.
        // Use current user after pass authentication to validate access.
        if (!$this->currentUser->hasPermission('access content')) {
            throw new AccessDeniedHttpException();
        }

        return new ModifiedResourceResponse($payload, 200);
    }

}

Then I enabled it on Rest_ui,

it’s available on the list of Swagger, but the body section is empty.

I think I missed something, could you please tell me what is missed and how can I make the post body available on openui_swagger?

Leave a Reply

Your email address will not be published. Required fields are marked *