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

Redoc will never display in urls correctly #157

Closed
SultanSGillani opened this issue Jul 3, 2018 · 24 comments
Closed

Redoc will never display in urls correctly #157

SultanSGillani opened this issue Jul 3, 2018 · 24 comments
Labels
bug Bug report/fix

Comments

@SultanSGillani
Copy link

SultanSGillani commented Jul 3, 2018

Ever since I have installed drf-yasg (Almost 6 months ago), I cannot seem to get redoc to work correctly.

Here is the normal error I get:

"http://127.0.0.1:8000/api/v1/redoc/?format=openapi" is not a valid JSON Schema
Stack trace
SyntaxError: "http://127.0.0.1:8000/api/v1/redoc/?format=openapi" is not a valid JSON Schema
    at Function.syntax (http://127.0.0.1:8000/static/drf-yasg/redoc/redoc.min.js:55:18937)
    at http://127.0.0.1:8000/static/drf-yasg/redoc/redoc.min.js:73:51233

ReDoc Version: 2.0.0-alpha.25 

Here is my usual url layout for swagger and redoc:

swagger_info = openapi.Info(
    title="something api",
    default_version='v1',
    description="""something API Swagger Definition""",  # noqa
    terms_of_service="https://www.something.com/policies/terms/",
    contact=openapi.Contact(email="contact@something.xyz"),
    license=openapi.License(name="MIT License"),
)

SchemaView = get_schema_view(
    validators=['ssv', 'flex'],
    public=True,
    permission_classes=(permissions.IsAuthenticated,),
)

...

url(r'^swagger(?P<format>\.json|\.yaml)$', SchemaView.without_ui(cache_timeout=None), name='schema-json'),
    url(r'^swagger/$', SchemaView.with_ui('swagger', cache_timeout=None), name='schema-swagger-ui'),
    url(r'^redoc/$', SchemaView.with_ui('redoc', cache_timeout=None), name='schema-redoc'),

And my settings:

SWAGGER_SETTINGS = {
    'LOGIN_URL': '/session/auth/login',
    'LOGOUT_URL': '/session/auth/logout',
    'DEFAULT_INFO': 'api.urls.swagger_info',
    'SECURITY_DEFINITIONS': {
        'Basic': {
            'type': 'basic'
        },
    },
    'APIS_SORTER': 'alpha',
    'SUPPORTED_SUBMIT_METHODS': ['get', 'post', 'put', 'delete', 'patch'],
    'OPERATIONS_SORTER': 'alpha'
}
REDOC_SETTINGS = {
    'LAZY_RENDERING': True,
}

Quick note: if I change permissions to permissions.AllowAny Redoc fails but validation still works.

@SultanSGillani SultanSGillani changed the title redoc url issue Redoc will never display in urls correctly Jul 3, 2018
@SultanSGillani
Copy link
Author

I can get swagger to display just fine, but i have restricted security for validation so the validation doesn't work currently. If I change the redoc template to point to the /swagger.json url it seems to work fine.

@SultanSGillani
Copy link
Author

swagger validator response with permissions.AllowAny:

swagger-validator -s http://127.0.0.1:8000/api/v1/swagger.json Documents appear to be swagger compliant Swagger Validation Complete

@axnsan12
Copy link
Owner

axnsan12 commented Jul 6, 2018

I'm sorry, I don't really follow.

Perms\Schema url /swagger.json /redoc/?format=openapi
permissions.IsAuthenticated ReDoc fail, validation success ReDoc fail, validation fail
permissions.AllowAny ReDoc works, validation success ReDoc fail, validation success

Am I getting this right??

In any case, I would need more than this in order to trace the bug, like something to reproduce it - project code, or at least the generated swagger files.

@SultanSGillani
Copy link
Author

SultanSGillani commented Jul 23, 2018

Sorry for taking so long to get back to you.

I have tried a couple of things and here is what I have found:

current urls.py

swagger_info = openapi.Info(
    title="Streisand API",
    default_version='v1',
    description="""Streisand API Swagger Definition""",  # noqa
    terms_of_service="https://www.something.com/policies/terms/",
    contact=openapi.Contact(email="contact@something.xyz"),
    license=openapi.License(name="MIT License"),
)

SchemaView = get_schema_view(
    validators=['ssv', 'flex'],
    public=True,
    permission_classes=(permissions.IsAuthenticated,),
)

URLS for redoc and swagger:

url(r'^swagger(?P<format>.json|.yaml)$', SchemaView.without_ui(cache_timeout=0), name='schema-json'),
    url(r'^swagger/$', SchemaView.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    url(r'^redoc/$', SchemaView.with_ui('redoc', cache_timeout=0), name='schema-redoc'),

    url(r'^cached/swagger(?P<format>.json|.yaml)$', SchemaView.without_ui(cache_timeout=None), name='cschema-json'),
    url(r'^cached/swagger/$', SchemaView.with_ui('swagger', cache_timeout=None), name='cschema-swagger-ui'),
    url(r'^cached/redoc/$', SchemaView.with_ui('redoc', cache_timeout=None), name='cschema-redoc'),

swagger urls are fine.

Redoc urls are not.

If I use a base html file with redoc in it:
index.zip
I get no errors. but I am not sure if its the drf-yasg redoc template or what.

Because I still get the error for any redoc url.
My API is using the IsAdminUser default permissions if that makes a difference. Changing it to IsAuthenticated does not make a difference so I dont think thats it.
One thing to note over at Redoc:
https://github.com/Rebilly/ReDoc#redoc-options-object

lazyRendering - Not implemented yet if set, enables lazy rendering mode in ReDoc.

I have attached my swagger json file for you to review.
swagger.zip

@axnsan12 axnsan12 added the bug Bug report/fix label Aug 3, 2018
@SultanSGillani
Copy link
Author

Just an FYI I was able to spin up a stand-alone redoc instance and npm installed. Redoc had no problem with using swagger.json or swagger.yaml as the specurl but received the exact same error with ?format=openapi. I honestly have no idea how your demo site works using it.

@axnsan12
Copy link
Owner

axnsan12 commented Aug 8, 2018 via email

@SultanSGillani
Copy link
Author

Here is my package.json

{
"name": "redoc",
"version": "1.0.0",
"description": "redoc",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"license": "MIT",
"dependencies": {
"express": "^4.16.3",
"express-status-monitor": "^1.1.5",
"jsonformatter": "^0.7.0",
"mobx": "^5.0.3",
"mobx-react": "5.2.3",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-dropdown": "1.5.0",
"react-hot-loader": "4.3.4",
"react-tabs": "2.2.2",
"redoc": "^2.0.0-alpha.34",
"styled-components": "3.4.2"
}
}

@axnsan12
Copy link
Owner

axnsan12 commented Aug 8, 2018

And do you get differing swagger JSON content when downloading from /swagger.json vs /redoc/?format=openapi?

@SultanSGillani
Copy link
Author

Well I can’t set the specurl as itself/?format=openapi. Maybe I’m trying something wrong. I have to set the spec as the swagger file. Am I doing something wrong?

@axnsan12
Copy link
Owner

axnsan12 commented Aug 8, 2018

I mean if you go

wget http://localhost:8000/swagger.json
wget http://localhost:8000/redoc/?format=openapi

Do you get identical files?

@SultanSGillani
Copy link
Author

SultanSGillani commented Aug 9, 2018

You can’t wget that way it outputs To a file called index.html?format=openapi

@SultanSGillani
Copy link
Author

Wget for swagger.json works fine

@SultanSGillani
Copy link
Author

I could work on updating your Redoc file to requesting the swagger json file if that would help. Not sure where in your code you do this besides the static folder.

@axnsan12
Copy link
Owner

axnsan12 commented Aug 9, 2018

wget http://localhost:8000/redoc/?format=openapi -O redoc-openapi.json...?

@axnsan12
Copy link
Owner

axnsan12 commented Aug 9, 2018

The latest commit on master should allow you to set the spec url for the bundled ReDoc... can you confirm that it works if you set it to /swagger.json but not by default?

@SultanSGillani
Copy link
Author

Yes that worked perfectly thank you so much!! You are awesome and I love this package and your responsiveness to the repo!!

@SultanSGillani
Copy link
Author

Confirmed that the setting fixes it but it doesn’t work if I don’t use the new spec url setting.

Something went wrong...

"https://api.something.com/api/v1/redoc/?format=openapi" is not a valid JSON Schema
Stack trace
https://api.something.com/static/drf-yasg/redoc/redoc.min.js:55:16084
https://api.something.com/static/drf-yasg/redoc/redoc.min.js:73:73711
promiseReactionJob@[native code]

ReDoc Version: 2.0.0-alpha.34 
Commit: fc31145

@SultanSGillani SultanSGillani reopened this Aug 9, 2018
@axnsan12
Copy link
Owner

axnsan12 commented Aug 9, 2018

I'm having a hard time following...

If you set

REDOC_SETTINGS = {
    'SPEC_URL': 'https://api.something.com/api/v1/swagger.json'
}

Does it then magically start working? Or does it have no effect?

Make sure to install drf-yasg from this git repo (because the SPEC_URL change is not yet pushed to pypi).

@SultanSGillani
Copy link
Author

Sorry for the confusion. What I am saying is that yes it does work, with the new setting. I was just letting you know that the new setting is the only way to fix it and ?format=openapi continues to break it.

Thanks for adding that to the code!

@axnsan12
Copy link
Owner

axnsan12 commented Sep 9, 2018

Given that I can't reproduce this, and that a workaround has been implemented, I'm closing this as a heisenbug...

@axnsan12 axnsan12 closed this as completed Sep 9, 2018
@markddavidoff
Copy link

markddavidoff commented Oct 25, 2018

@axnsan12 @theSaltman I'm getting this same issue in 1.11.0. If I set the spec url setting like you posted above (changin the api.something.com... url to point to my url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json') api):

REDOC_SETTINGS = {
    'SPEC_URL': 'https://api.something.com/api/v1/swagger.json'
}

then redoc works fine. Without that and a setup like the one above and the one in the docs I get:
image

This is an issue as i want to have multiple docs endpoints and seeing as the redoc setting is global, this workaround isn't enough.

@axnsan12
Copy link
Owner

That's still not enough info to work on, I'd need a full project that reproduces this, or some output to compare from the two different URLs...

@markddavidoff
Copy link

What output do you need? the wget http://localhost:8000/redoc/?format=openapi returns the rendered html. is that what you want? It seems the issue is with whatever logic resolves ?format=openapi into the swagger spec url. That works for the swagger endpoint but not the redoc one. If you can point me at where the redoc renderer is supposed to get the swagger schema url, I can step through and see whats going on.

@brian-emarquez
Copy link

image

For the first time I face a problem like this, I can't find a solution

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

No branches or pull requests

4 participants