Swagger-php - how to add paths programmatically?

591 Views Asked by At

I'm using Swagger for Documentation. Generating json file from annotation works great. Further, I want to add some more paths to the generated json file programmatically.

I discovered that the Swagger class has merge method so I tried to deserialize my json string and merge into Swagger object like below, but had no luck.

$swagger = \Swagger\scan($appDir);
$jsonString = json_encode([
        "path" => [
            "path" => "/api/task/{taskName}",
            "parameter" => [
                "ref" => "#/parameters/taskName"
            ]
        ],
    ]);
$objectToMerge = (new Serializer())->deserialize($jsonString, 'Swagger\Annotations\Path');
$swagger->merge($object);

I don't know I'm doing right way. Anyone had used Swagger merge method or mergeProperties method? Or are there another way to achieve my goal?

1

There are 1 best solutions below

0
On

Okay I figured out the problem.

The problem was because the json string I put into deserialize method as the first parameter was not proper.

The formation of json string is not an OpenAPI spec one. I think this formation is used in Swagger-php internally.

Here is the proper json formation which is working.

[
    "path" => "/api/task/{taskName}",
    "post" => [
        "path" => "/api/task/{taskName}",
        "tags" => ['test'],
        "summary" => "summary test",
        "description" => "description test",
        "produces" => ['application/json']
    ]
]

Hope this help someone in trouble.