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

Uncaught TypeError: Cannot read property 'definitions' of null #58

Closed
abhilashak opened this issue Jun 15, 2016 · 12 comments
Closed

Uncaught TypeError: Cannot read property 'definitions' of null #58

abhilashak opened this issue Jun 15, 2016 · 12 comments
Labels

Comments

@abhilashak
Copy link

abhilashak commented Jun 15, 2016

Hi authors,

I followed the 'grape-swagger-rails' documentation and the /swagger_doc.json initial page loads fine from swagger ui as follows:

swagger-first-page-ok

But apart from this page all other requests, I am getting one of the 2 errors

1st Error: Unable to Load SwaggerUI
(I am testing '401 Unauthorised' page works or not with swagger documentation)

For this response Swagger ui is not loading and showing the following error:
Unable to Load SwaggerUI
apidoc:50 401 : http://g_m.lvh.me:3000/content/api/v1/activities/23

swagger-401-unauthorised

2nd Error: Uncaught TypeError: Cannot read property 'definitions' of null

(I am testing '200 Created' page works or not with swagger documentation)
For all other successful requests (200) where I am sending json response back, occurs the following error:
Uncaught TypeError: Cannot read property 'definitions' of null

screen shot 2016-06-15 at 1 57 54 pm

screen shot 2016-06-15 at 1 58 10 pm

NOTE: I implemented this on a Rails Engine that is mounted on '/content'

I tried a lot, feeling that any configuration I missed to include all these urls in swagger ui. But nothing I can found. Reply me if you have a solution for this else please fix this issue as soon as possible.

@dblock dblock added the bug? label Jun 15, 2016
@dblock
Copy link
Member

dblock commented Jun 15, 2016

It looks like your API requires authentication. What happens if you just go to /content/api/v1/activities in a browser?

@abhilashak
Copy link
Author

@dblock No, I commented all authentication and autorization part and started to test. In browser the path /content/api/v1/activities gives all activities json.I am testing it in postman. And it also gives json response back when simply accessing '/content/api/v1/activities'.

@dblock
Copy link
Member

dblock commented Jun 15, 2016

Well, the API responds with a 401, examine the network tab and see why. Could be CORS or something like that.

@abhilashak
Copy link
Author

abhilashak commented Jun 15, 2016

:) I think you are misunderstood because of I mentioned as '1st Error: 401 Unauthorised'. I modified the description now. Actually I meant I was testing this '401 Unauthorised' functionality in the 2nd case (here I am not commented the authentication). It works well. 401 is my expected answer. But in this case also Swagger shows Unable to load Swagger ui. Atleast I am expecting some ui from swagger in 401 page also.

And the real bug comes in the my 3rd test, where I commented the authentication and tested the response. Server sending the json response back and swagger shows some javascript error ( Uncaught TypeError: Cannot read property 'definitions' of null) in console. Why that? '/content/api/v1/activities' url cannot found by swagger?

@aschuster3
Copy link

That bar at the top is NOT to test your API endpoints. It is to put the JSON endpoint of your valid OpenAPI v2. When you put a URL in that bar, Swagger UI tries to build the API calls and if the endpoint doesn't return JSON, it errors out.

As you add your API calls to grape (which I assume you're using) and document them using grape-swagger, they will automatically begin populating on your documentation page as long as you're pointing to you JSON endpoint. Eventually your API will look like this and you'll be able to interact with it on the page.

@abhilashak
Copy link
Author

abhilashak commented Jun 15, 2016

@aschuster3 Great. I totally misunderstood this may be. Now I got the idea. Thank you. Yes I am using Grape. Here I used grape-swagger-rails.

screen shot 2016-06-15 at 8 30 17 pm

screen shot 2016-06-15 at 8 28 34 pm

My doubt is how to generate this 'Valid OpenAPI v2 JSON endpoint' to give in that top bar? Is that already generated by grape-swagger when I add 'add_swagger_documentation' as in screenshot no. 2 line no. 5?

@dblock
Copy link
Member

dblock commented Jun 15, 2016

Ah thanks @aschuster3 I see what's happening here ;)

@abhilashak the documentation endpoint should be /api/v1/swagger_doc I believe. That initial page should have all you need. Not sure what's in QuestionAPI and why it doesn't show more, I would look there next.

@abhilashak
Copy link
Author

abhilashak commented Jun 15, 2016

@dblock No, I given the path 'api/v1/swagger_doc' but it not works. It shows the same error: 'Unable to Load SwaggerUI'. I think 'content/swagger_doc.json' is the correct JSON Endpoint as @aschuster3 specified, as it loads well without any issue (see screenshot no.1). But it not showing my Grape API calls. Just showing

BASE URL: /content
API VERSION: 0.0.1

I don't know how to populate these Grape API calls that I written in swagger ui. I think it should be done by grape-swagger as we are calling 'add_swagger_documentation' method inside base api file (Where we mount all the Grape apis)

@aschuster3
Copy link

aschuster3 commented Jun 15, 2016

Yep, grape-swagger auto-generates the Valid OpenAPI v2 JSON endpoint. You should be able to check that the behavior is correct by visiting [your_url]/swagger_doc.json based on the defaults that grape-swagger has. Here's what the pet store example's JSON looks like for reference.

You can take a look over at grape-swagger's instructions for configuring to know what the default behavior is. By looking at your first picture in your original question, it appears that you are navigating to the correct path for the JSON.

After verifying all of that, the problem might be arising from how you're mounting your APIs. add_swagger_documentation should be the LAST thing that you call. So your file should look as follows:

require 'grape-swagger'

module ContentManager
  class API < Grape::API

    # All of your mount statements and logic

    add_swagger_documentation(hide_documentation_path: true)
  end
end

I think that should solve your problem!

@abhilashak
Copy link
Author

abhilashak commented Jun 15, 2016

@aschuster3 Wahhh Super great ...Thank you so much 💯 That was the issue!!

When I was adding the method 'add_swagger_documentation' I thought It will fetch all the API calls mentioned in that API class. But we are mounting the calls later. So the method cannot find the APIs!

When it was not working, I thought we should give each api urls in that top box to make it work!! Haha. even though it doesn't make sense. Just a Developer thought!

I added the method above the mount calls, just for nice code!

Guys finally it looks like this:

screen shot 2016-06-15 at 11 18 29 pm

Thank you, @dblock @aschuster3 👍

@aschuster3
Copy link

Great! I'm gonna go ahead and close this issue.

Glad I could help!

@dblock
Copy link
Member

dblock commented Jun 15, 2016

Excellent detective work @aschuster3!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants