1

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 :)

NeyJâh
  • 11
  • 3
  • What are the actual paths (URLs) of the endpoints? Is it sorting by those alphabetically? – Lorna Mitchell Apr 06 '23 at 10:29
  • @LornaMitchell paths are sorted in the order they are parsed in annotations, so kind of alphabetically but not really. src folder is parsed alphabetically, but if paths are not already sorted in the files, (e.g: "/api/company/{id}" is before "/api/companies"), they are not ordered. Tags also group paths, and that make paths not necessarily follow the annotations order... – NeyJâh Apr 07 '23 at 08:17

1 Answers1

0

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);
            });
        }
    }
}
DerManoMann
  • 409
  • 2
  • 3