Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger using base path for resource definition #490

Closed
krukgit opened this issue Jul 11, 2014 · 12 comments
Closed

Swagger using base path for resource definition #490

krukgit opened this issue Jul 11, 2014 · 12 comments

Comments

@krukgit
Copy link

krukgit commented Jul 11, 2014

I'm serving my API from localhost:3000/api/. I keep swagger resources' definition at localhost:3000/docs/swagger/ and I want to serve the documentation from localhost:3000/docs.

I pointed swagger to the root resource definition file (http://localhost:3000/docs/swagger/api-docs.json). File content:

{
  "apiVersion": "2.0",
  "swaggerVersion": "1.2",
  "basePath": "http://localhost:3000/",
  "apis": [
    {
      "path": "api/v2/registrations.{format}",
      "description": "Creating and retrieving a user"
    }
  ]
}

Swagger correctly reads the file and displays registrations resource, however when it tries to fetch the resource specification, instead of using relative path and going to http://localhost:3000/docs/swagger/api/v2/registrations.json it uses api base path, which results in the error:

Unable to read api 'registrations' from path http://localhost:3000/api/v2/registrations.json (server returned undefined)

I traced the error to this line.

What am I doing wrong here?

I fixed it temporarily by changing the basePath in swagger.js:

@@ -171,13 +171,7 @@ SwaggerApi.prototype.buildFromSpec = function(response) {
       }
     }
   }
-  if (response.basePath) {
-    this.basePath = response.basePath;
-  } else if (this.url.indexOf('?') > 0) {
-    this.basePath = this.url.substring(0, this.url.lastIndexOf('?'));
-  } else {
-    this.basePath = this.url;
-  }
+  this.basePath = this.url.substr(0,this.url.lastIndexOf('/')+1);
   if (isApi) {
     var newName = response.resourcePath.replace(/\//g, '');
     this.resourcePath = response.resourcePath;
@fehguy
Copy link
Contributor

fehguy commented Jul 11, 2014

Hi, are you using grape-swagger by chance?

@krukgit
Copy link
Author

krukgit commented Jul 11, 2014

I'm using swagger-docs.

@webron
Copy link
Contributor

webron commented Jul 11, 2014

While there may still be a bug in swagger-ui, your api-docs.json is wrong as it shouldn't include the basePath property in it (it's not in accordance to the spec). Can you try removing it and test again?

@krukgit
Copy link
Author

krukgit commented Jul 11, 2014

It's somehow better, but still not fully correct:

Unable to read api 'registrations' from path http://localhost:3000/docs/swagger/api-docs.jsonapi/v2/registrations.json (server returned undefined)

Works with this modification:

    if (response.basePath) {
        this.basePath = response.basePath;
+   } else if (this.url.indexOf('/') > 0) {
+       this.basePath = this.url.substring(0, this.url.lastIndexOf('/')+1);
    } else if (this.url.indexOf('?') > 0) {
        this.basePath = this.url.substring(0, this.url.lastIndexOf('?'));
    } else {
        this.basePath = this.url;
    }

@webron
Copy link
Contributor

webron commented Jul 11, 2014

"apis": [
{
"path": "api/v2/registrations.{format}",
"description": "Creating and retrieving a user"
}
]

The path should be

  "path": "api/v2/registrations.json",

On 11 July 2014 13:02, Maciej Kruk [email protected] wrote:

It's somehow better, but still not fully correct:

Unable to read api 'registrations' from path http://localhost:3000/docs/swagger/api-docs.jsonapi/v2/registrations.json (server returned undefined)

Works with this modification:

if (response.basePath) {
    this.basePath = response.basePath;
  • } else if (this.url.indexOf('/') > 0) {
  •   this.basePath = this.url.substring(0, this.url.lastIndexOf('/')+1);
    
    } else if (this.url.indexOf('?') > 0) {
    this.basePath = this.url.substring(0, this.url.lastIndexOf('?'));
    } else {
    this.basePath = this.url;
    }


Reply to this email directly or view it on GitHub
#490 (comment).

@krukgit
Copy link
Author

krukgit commented Jul 11, 2014

Changing {format} to json doesn't change anything. swagger.js takes care of it.

@webron
Copy link
Contributor

webron commented Jul 11, 2014

right, you're missing a leading slash to the value -

  "path": "/api/v2/registrations.{format}",

On 11 July 2014 13:08, Maciej Kruk [email protected] wrote:

Changing {format} to json doesn't change anything. swagger.js takes care
of it.


Reply to this email directly or view it on GitHub
#490 (comment).

@fehguy
Copy link
Contributor

fehguy commented Aug 2, 2014

Hi, I'm going to close this out, please reopen if still an issue.

@fehguy fehguy closed this as completed Aug 2, 2014
@bkakadiya42
Copy link

on hitting https://api.company.com/api/doc, it gives me:

{
  "apiVersion": "v1",
  "swaggerVersion": "1.2",
  "produces": [
    "application/json"
  ],
  "operations": [],
  "apis": [
    {
      "path": "/api/doc/users.{format}"
    }
  ],
  "info": {},
  "basePath": "https://api.company.com"
}

on using swagger-codegen for ruby, it gives me:
base path is https://api.company.com/api/doc
java.io.FileNotFoundException: https://api.company.com/api/doc/api/doc/users.json

however api's(calls to https://api.company.com/v1/users etc) are working correctly as expected.
not sure if this is issue with swagger ui or swagger client lib generator.

@webron
Copy link
Contributor

webron commented Sep 16, 2014

@kakadiya91 - The output:

{
  "apiVersion": "v1",
  "swaggerVersion": "1.2",
  "produces": [
    "application/json"
  ],
  "operations": [],
  "apis": [
    {
      "path": "/api/doc/users.{format}"
    }
  ],
  "info": {},
  "basePath": "https://api.company.com"
}

is incorrect - this is an invalid swagger specification file. The ui may work properly, but the codegen may be a bit more sensitive to malformed structures.

@fehguy
Copy link
Contributor

fehguy commented Sep 17, 2014

@kakadiya91 are you using grape swagger by chance? If so there's a fork that addresses these issues, see this sample:

https://github.com/wordnik/rails-petstore

@bkakadiya42
Copy link

Thank you @fehguy, @webron. I pointed my app to that gem (https://github.com/CraigCottingham/grape-swagger), made some code changes to my api repo to work with this gem, now it works like a charm.

gunnarlium added a commit to gunnarlium/swagger-php that referenced this issue Dec 9, 2014
basePath is not a valid element in the resource list, and it causes
swagger-ui to load the wrong paths from the backend, in effect breaking
the entire service.

See this for a related issue: swagger-api/swagger-ui#490
bfanger pushed a commit to zircote/swagger-php that referenced this issue Apr 17, 2015
basePath is not a valid element in the resource list, and it causes
swagger-ui to load the wrong paths from the backend, in effect breaking
the entire service.

See this for a related issue: swagger-api/swagger-ui#490
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants