phpsymfonyswaggeropenapiapi-platform.com

How to ask for additional GET parameters in an endpoint of api-platform using swagger docs?


I have a symfony project, where I use api-platform.

I have an entity, and I have data providers for it. I am in trouble with definition of additional parameters for a collection endpoint.

An entity is called suggestion. It has to return collection of documents from elastic search.

An endpoint is:

/suggestion

This endpoint listens to additional GET parameters:

page, level

These two params are read each time, when the endpoint is requested.

In my SuggestionsCollectionDataProvider.php class I have:

/**
     * Retrieves a collection.
     *
     * @param string $resourceClass
     * @param string|null $operationName
     * @return \Generator
     */
    public function getCollection(string $resourceClass, string $operationName = null): \Generator
    {
        $query = $this->requestStack->getCurrentRequest()->query;
        // I am reading these two parameters from RequestStack
        
        // this one is built-in
        $page = max($query->get('page', 1), 1); 

        // this is a custom one
        $level = $query->get('level', 0); 
        ...

In my SuggestionRepository.php class:

/**
     * @return \Generator
     */
    public function find(int $page, int $level): \Generator
    {
        // here I can process with $level without problems

Page parameter is default parameter, that is generating in swagger for collections.

A screenshot from API platform generated Swagger doc:

enter image description here

But the page parameter is now the only parameter, that can be edited in web version.

I need to add more parameters (level in this case) to swagger and describe them, so the user/tester knows which parameter actually goes to this endpoint.

Main question:

How to tell api-platform, that I want a user/tester of the API (from client side) to enter some other parameters, i.e. level for example?


Solution

  • Finally figured it out.

    I haven't found a documentation for it yet, but I found a way.

    In an entity class Suggestion.php I've added some lines of annotations:

    namespace App\Entity;
    
    use ApiPlatform\Core\Annotation\ApiProperty;
    use ApiPlatform\Core\Annotation\ApiResource;
    use Symfony\Component\Serializer\Annotation\Groups;
    use Symfony\Component\Validator\Constraints as Assert;
    
    /**
     * Class Suggestion. Represents an entity for an item from the suggestion result set.
     * @package App\Entity
     * @ApiResource(
     *     collectionOperations={
     *          "get"={
     *              "method"="GET",
     *              "swagger_context" = {
     *                  "parameters" = {
     *                      {
     *                          "name" = "level",
     *                          "in" = "query",
     *                          "description" = "Levels available in result",
     *                          "required" = "true",
     *                          "type" : "array",
     *                          "items" : {
     *                              "type" : "integer"
     *                          }
     *                      }
     *                  }
     *               }
     *          }
     *     },
     *     itemOperations={"get"}
     * )
     */
    

    The result view in API platform swagger DOCs:

    enter image description here