Problem/Motivation

Currently, the module provides only three default status codes.
200 successful operation resp. 201 from openapi
400 Bad request
500 Internal server error

It is not possible to describe the own custom REST resources responses.

Proposed resolution

Add the possibility to describe the responses of custom REST resources.

Comments

marco-s created an issue. See original summary.

marco-s’s picture

Proposal: I added a new method to RestGenerator which checks for response information in the REST resources's plugin definition.
This makes it possible to define the response info in the custom REST resources.

Example (in your REST resource class):

public function getPluginDefinition() {
  $definition = parent::getPluginDefinition();
  $definition['responses'] = [
    Response::HTTP_OK => [
      'description' => Response::$statusTexts[Response::HTTP_OK],
      'properties' => [
        'button_text' => [
          'type' => 'string',
          'example' => 'some text',
        ],
        'link' => [
          'type' => 'string',
          'example' => 'https://some-link.com',
        ],
      ],
    ],
    Response::HTTP_BAD_REQUEST => [
      'description' => Response::$statusTexts[Response::HTTP_BAD_REQUEST],
      'properties' => [
        'error' => [
          'type' => 'string',
          'example' => 'Received wrong request body!',
        ],
      ],
    ],
  ];

  return $definition;
}

Theoretically, you could also define it in the RestResource annotation.

/**
 * Provides REST API endpoint for a test case.
 *
 * @RestResource(
 *   id = "resource_test_case",
 *   label = @Translation("Test case resource"),
 *   uri_paths = {
 *     "canonical" = "/api/test-case"
 *   },
 *   responses = {
 *     200 = {
 *        "description" = "OK"
 *     },
 *     404 = {
 *        "description" = "Not found"
 *     }
 *   }
 * )
 */

This is just a proposal. Maybe there is a better solution?!

marco-s’s picture

I have updated the patch so that the developer has full control in the plugin definition method.

Example usage:

public function getPluginDefinition() {
  return NestedArray::mergeDeep(
    parent::getPluginDefinition(),
    [
      'responses' => [
        Response::HTTP_OK => [
          'description' => Response::$statusTexts[Response::HTTP_OK],
          'schema' => [
            'type' => 'object',
            'properties' => [
              'button_text' => [
                'type' => 'string',
                'example' => 'some text',
              ],
              'link' => [
                'type' => 'string',
                'example' => 'https://some-link.com',
              ],
            ],
          ],
        ],
        Response::HTTP_BAD_REQUEST => [
          'description' => Response::$statusTexts[Response::HTTP_BAD_REQUEST],
        ],
      ],
    ]
  );
}
grathbone’s picture

StatusFileSize
new3.44 KB

Patch doesn't apply along with the patch #14 in 3116760, so wrote a patch of 530 that will allow both.

grathbone’s picture

StatusFileSize
new3.45 KB

Updated patch from #4 to allow for comment #15 patch.

grathbone’s picture

Related issues: +#3116760: Add parameter description
StatusFileSize
new3.44 KB
new3.45 KB

Responses need to be based on their appropriate method. Updated patch to reflect passing in responses keyed by method.

As well, attached a patch to allow using the patch in ticket issue #3116760 as well

grathbone’s picture

StatusFileSize
new3.59 KB

Attached the wrong patch above for the #3116760 patch.

grathbone’s picture

StatusFileSize
new3.46 KB
new3.61 KB

Responses need to be based on their appropriate method. Updated patch to reflect passing in responses keyed by method.

Note: Messed up the patch in #06. So this is a fixed version with both patches.

grndlvl’s picture

StatusFileSize
new3.46 KB

Re-roll for latest 8.x-2.x. and as I see now the previous comment had two patches and 3171530-08.patch applied just fine. Sorry for the noise.

grndlvl’s picture

broon’s picture

StatusFileSize
new5.02 KB

The patch from #9 works. However, we required to not only to be able to define the response but also the available parameters. Thus, we extended the patch to work similarly for parameters in the class annotation.

/**
 * Provides a REST resource for retrieving object name based on id.
 *
 * @RestResource(
 * id = "get_object_name",
 * label = @Translation("Get Object Name"),
 * uri_paths = {
 * "canonical" = "/restapi/object-name"
 * },
 * parameters = {
 *   "GET" = {
 *     "id" = {
 *       "name" = "id",
 *       "in" = "query",
 *       "type" = "integer",
 *       "required" = TRUE,
 *       "description" = "The object ID"
 *     }
 *   }
 * },
 * responses = {
 *   "GET" = {
 *     "200" = {
 *       "description" = "successful operation",
 *       "schema" = {
 *         "type" = "array",
 *         "items" = {
 *           "type" = "object",
 *           "properties" = {
 *             "name" = {"type" = "string"}
 *           }
 *         }
 *       }
 *     }
 *   }
 * }
 * )
 */
broon’s picture

Status: Active » Needs review
broon’s picture

Version: 8.x-2.0-rc2 » 8.x-2.0