Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger-php - how to add paths programmatically?

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?

like image 977
jesuisgenial Avatar asked Oct 18 '25 00:10

jesuisgenial


1 Answers

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.

like image 112
jesuisgenial Avatar answered Oct 19 '25 13:10

jesuisgenial