NeyJâh
NeyJâh

Reputation: 11

Sorting API paths in ReDoc with zircote/swagger-php (OpenAPI / Symfony)

I can't find a way to sort the API paths defined in my PHP annotations with zircote/swagger-php, i found nothing in OpenAPI specification nor in swagger-php documentation to achieve this. This related GitHub thread didn't help much.

open-api.json file is generated from the following command: bin/openapi -o doc/open-api.json --format json src.

ReDoc displays the paths in the order they are defined in the "paths" variables in open-api.json, but the order of the paths in the generated file are quite random (+ they don't always match the order of the php annotations in files).

Click for image : paths are sorted randomly

I tried to use the ReDoc sortOperationsAlphabetically option (seems not supported by zircote/swagger-php) and set an operationId to my paths.

I also tried to add use the SortComponents processor but failed to load it.

composer.json : PHP 7.4.33 Symfony 3.4 zircote/swagger-php 3.3.3

package.json (webpack) "redoc": "^2.0.0-rc.8-1"

Thanks for help :)

Upvotes: 1

Views: 870

Answers (1)

DerManoMann
DerManoMann

Reputation: 529

I just checked against the latest master and the SortComponents processor does work for me. You have to be careful to make sure the processor class is visible to the autoloader. From the Examples folder that is not the case.

Also, the example processor does sort components; for paths you probably want some variation of this:

<?php declare(strict_types=1);

namespace OpenApi\Processors;

use OpenApi\Analysis;

/**
 * Sorts paths so they appear in alphabetical order in the generated specs.
 */
class SortPaths
{
    public function __invoke(Analysis $analysis)
    {
        if (is_iterable($analysis->openapi->paths)) {
            usort($analysis->openapi->paths, function ($a, $b) {
                return strcmp($a->path, $b->path);
            });
        }
    }
}

Upvotes: 0

Related Questions