Search code examples
phpswaggeropenapiopenapi-generatorswagger-php

openapi-generator-cli not generating documentation from PHP file


Background: I have installed composer installed zircote/swagger-php and have installed openapi-generator-cli with apt-get.

I am not sure whether or not I am attempting to use these tools correctly, however I've been unable to find documentation pointing me in any direction.

I have a controller file with a whole bunch of code in it. I want to test whether or not I can generate a json file from it using open api annotation.

Here's a sample of my code (I've cut out unrelated chunks of it):

<?php

/**
 * @OA\Info(title="My First API", version="0.1")
 */
class Apiv1_LocationController extends App_Controller_API
{
    /* Publicly exposed attributes and the field type for filtering */
    protected $_exported = array('id','created','modified','name','address','phone','external_id','postcode','country','timezone','date_format','lacps','staff_count');
    protected $_schematypes = array(
        'string' => ['name','address','phone','external_id','postcode','country','timezone','date_format'],
        'int'    => ['id','staff_count'],
        'timestamp' => ['created','modified'],
        'complex'=> ['lacps'],
    );
    {more unrelated code...}

    /**
     * @OA\Get(
     *     path="/api/resource.json",
     *     @OA\Response(response="200", description="An example resource")
     * )
     */
    public function getAction()
    {
        {code inside action...}
    }
}

The cli command I use:

openapi-generator-cli generate -g php -i <path_to_LocationController>

I get the following error:

[main] INFO  o.o.c.ignore.CodegenIgnoreProcessor - No .openapi-generator-ignore file found.
Exception in thread "main" java.lang.RuntimeException: Issues with the OpenAPI input. Possible causes: invalid/missing spec, malformed JSON/YAML files, etc.

This leads me to believe I am using the openapi-generator-cli tool incorrectly, since I wouldn't be expecting to need a JSON or YAML file, I am trying to generate that file.

I'll keep trying, but if someone could help me realized what I'm doing wrong or how I'm misunderstanding the tool, I'd really appreciate it.


Solution

  • So I realized I'd been going about this in entirely the wrong way.

    I used the zircote/swagger-php library to generate the JSON file I required with the following command (In the directory where I wanted the JSON to be generated):

    ./<path_to_vendor_directory>/vendor/zircote/swagger-php/bin/openapi --pattern "*Controller.php" --output <name_of_file>.json --format json <location_to_search_from_recursively>