alt3 / cakephp-swagger

Swagger plugin for documenting your CakePHP APIs
MIT License
64 stars 17 forks source link

Swagger UI "Try it out" button misses basePath #25

Closed mikapomet closed 8 years ago

mikapomet commented 8 years ago

I have the following annotation : /** @SWG\Swagger( schemes={"http", "https"}, host="API_HOST", basePath="/api", @SWG\Info( version="1.0.0", title="...", ) ) */

But when i press "Try it out", the request url does not include the basePath. I've seen the lib/SwaggerTools file containing line 50 :
// set object properties required by UI to generate the BASE URL $swagger->host = $host; $swagger->basePath = '/' . Configure::read('App.base');

bravo-kernel commented 8 years ago

This works for me, basePath is auto-generated, settings like schemes coming from swagger configuration file.

app.php

    'App' => [
        'base' =>  'v0',
    ]

Controller annotation:

    @SWG\Swagger(
        @SWG\Info(
            title="API Documentation",
            description="MY API",
            termsOfService="http://swagger.io/terms/",
            version="1.0.0"
        )
    )

Generated swagger document (with basePath set by plugin):

{
    "swagger": "2.0",
    "info": {
        "title": "API Documentation",
        "description": "MY API",
        "termsOfService": "http://swagger.io/terms/",
        "version": "1.0.0"
    },
    "host": "api.app",
    "basePath": "/v0",
    "schemes": [
        "http",
        "https"
    ]
}
bravo-kernel commented 8 years ago

Wait a minute, I remember swagger constructing the "Try it out" url from the loaded page. Gave me some headaches but this is how I solved it.

\ 1. Setup nginx proxy**

I created an nginx proxy for the basePath (v0 in my case).

#
# Cakebox Nginx PHP-FMP proxy for passing basePath matches to matching vhost config.
#
server {
    listen 80;
    server_name api.app;

    access_log /var/log/nginx/api.app.access.log;
    error_log /var/log/nginx/api.app.error.log;

    #
    # Port numbers have corresponding vhost file (e.g. api.app.8010)
    #
    location /v0/ {
        proxy_pass http://127.0.0.1:8010/;
    }
}

2. Create host listening on 8010

The proxy will pass connections with /v0 matches to this vhost

#
# Cakebox-generated Nginx PHP-FMP virtual host using generic template.
#

#server {
#    listen 8010;
#    server_name www.api.app;
#    rewrite ^(.*) http://api.app$1 permanent;
#}

server {
    listen 127.0.0.1:8010;

    # root directive should be global
    root /home/vagrant/projects/api.app/webroot;
    index index.php;

    access_log /var/log/nginx/api.app.access.log;
    error_log /var/log/nginx/api.app.error.log;

    location / {
        try_files $uri \$uri /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass    unix:/var/run/php5-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # deny access to hidden
    location ~ /\. {
        deny all;
    }
}

3. Done

Swagger UI will use the correct URL after implementing this.

mikapomet commented 8 years ago

My problem was way more simple. I tried to set basePath in swaggerAnnotation instead of in app.php

app.php

'App' => [
    'base' =>  false,
]

Controller annotation:

@SWG\Swagger(
    host="API_HOST",
    basePath="/v0",
    @SWG\Info(
        title="API Documentation",
        description="MY API",
        termsOfService="http://swagger.io/terms/",
        version="1.0.0"
    )
)

This doesn't work, but setting the base in app.php helped me to fix it. Thanks

bravo-kernel commented 8 years ago

Good to hear, enjoy your swagger :relaxed: