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

AttributeError: 'function' object has no attribute 'root_path' #61

Closed
njarvis opened this issue Mar 28, 2017 · 15 comments
Closed

AttributeError: 'function' object has no attribute 'root_path' #61

njarvis opened this issue Mar 28, 2017 · 15 comments

Comments

@njarvis
Copy link

njarvis commented Mar 28, 2017

Issue introduced between 0.5.14 and 0.6.1:

Traceback (most recent call last):
  File "/usr/local/bin/cci", line 7, in <module>
    from cci.main import start
  File "/usr/local/lib/python2.7/dist-packages/cci/main.py", line 13, in <module>
    from api import app
  File "/usr/local/lib/python2.7/dist-packages/cci/api/__init__.py", line 33, in <module>
    import views  # noqa
  File "/usr/local/lib/python2.7/dist-packages/cci/api/views.py", line 80, in <module>
    @swag_from(swag_file('api_versions.yml'))
  File "/usr/local/lib/python2.7/dist-packages/flasgger/utils.py", line 72, in decorator
    'root': function.root_path
AttributeError: 'function' object has no attribute 'root_path'

Our code:

def swag_file(yml):
    return pkg_resources.resource_filename('cci', os.path.join("api/swag", yml))

@app.route('/api/versions/', methods=['GET'])
@swag_from(swag_file('api_versions.yml'))
def get_version():
   // ...
@rochacbruno
Copy link
Member

@njarvis Is your view under a blueprint? I am trying to fix this in #59

@njarvis
Copy link
Author

njarvis commented Mar 28, 2017

We don't use blueprint just "vanilla" flask.

@rochacbruno
Copy link
Member

@njarvis ok, I think I found the problem.

Problem is occurring when @swag_from receives absolute path starting with /

I should fix this later today.

Thanks for reporting

@rochacbruno
Copy link
Member

I made a change in 745d017 that might fix this one, please test it and let me know if solved. thanks.

@njarvis
Copy link
Author

njarvis commented Mar 29, 2017

It fixes the traceback I reported, but isn't displaying correctly. I see 404s in the access logs:

[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:29 +0100] "GET /apidocs/ HTTP/1.1" 200 5049 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:29 +0100] "GET /flasgger_static/lib/object-assign-pollyfill.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:29 +0100] "GET /flasgger_static/lib/jquery-1.8.0.min.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:29 +0100] "GET /flasgger_static/lib/jquery.slideto.min.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:29 +0100] "GET /flasgger_static/lib/jquery.wiggle.min.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:29 +0100] "GET /flasgger_static/lib/jquery.ba-bbq.min.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:29 +0100] "GET /flasgger_static/lib/handlebars-4.0.5.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:30 +0100] "GET /flasgger_static/lib/lodash.min.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:30 +0100] "GET /flasgger_static/lib/backbone-min.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:30 +0100] "GET /flasgger_static/lib/highlight.9.1.0.pack.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:30 +0100] "GET /flasgger_static/lib/highlight.9.1.0.pack_extended.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:30 +0100] "GET /flasgger_static/lib/jsoneditor.min.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:30 +0100] "GET /flasgger_static/lib/marked.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"
[gunicorn.access:INFO] 127.0.0.1 - - [29/Mar/2017:19:08:30 +0100] "GET /flasgger_static/lib/swagger-oauth.js HTTP/1.1" 404 233 "http://localhost:8000/apidocs/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8"

With 0.5.14, I was using Swagger(api, config={...}). Tried replacing that with Swagger(api), but still see the above log entries.

@rochacbruno
Copy link
Member

Ok, looks like you have a problem for accessing static files, are you passing any config which replaces the static path?

what is the output of /apidocs/?json=true

and what is the output of each url in there?

Lets first see if specs is being generated correclty as JSON and then find the problem with static_files.

Thanks for your help.

@rochacbruno
Copy link
Member

Ok, got it!

83d2291

/lib was on /gitignore my bad.. please try update your local repo and try again :)

@njarvis
Copy link
Author

njarvis commented Mar 29, 2017

We're getting closer :)

I'm not doing any explicit configs, just Swagger(app)

I now see one path (of many) being displayed.

http://localhost:8000/apidocs/?json=true

{
  "specs": [
    {
      "endpoint": "apispec_1", 
      "title": "A swagger API", 
      "url": "/apispec_1.json", 
      "version": "1.0.1"
    }
  ], 
  "title": "Flasgger"
}

And

  "definitions": {
  # many...
  }
  "info": {
    "description": "API description", 
    "termsOfService": "Terms of service", 
    "title": "A swagger API", 
    "version": "1.0.1"
  }, 
  "paths": {
    "/api/v0.1/agents/": {
     # This one is displayed correctly.
    },
   # Many more paths that are not displayed
  }, 
  "swagger": "2.0"
}

@njarvis
Copy link
Author

njarvis commented Mar 29, 2017

Don't know if this is related.I also see this artefact in my browser (Safari), after the load/render completes. The text box looks wrong, and I don't think that text "Rendering Swagger UI..." should remain.

screen shot 2017-03-29 at 19 39 01

@njarvis
Copy link
Author

njarvis commented Mar 29, 2017

From error console after rendering the page:

screen shot 2017-03-29 at 19 43 20

@rochacbruno
Copy link
Member

rochacbruno commented Mar 29, 2017

I'll setup a Saucelabs environment to test safari! (I'm on Linux)

Maybe itis because produces is empty?

If you try:

on yaml file
---
...
produces:
  - 'application/json'

Changes something?

@rochacbruno
Copy link
Member

I want to solve this before releasing next version to PyPI but I am not able to reproduce it.

@rochacbruno
Copy link
Member

I need help from people who has mac and safari to run every example in /examples folder and please see if can reproduce the issue.

@njarvis
Copy link
Author

njarvis commented Mar 31, 2017

I've debugged further. The problem was with one of my swag definition files. I had

produces:
  image/png

and that caused the rendering to stop. The fix was

produces:
  - image/png

This was working in 0.5.14 (I suspect this may be a swaggerui issue, not flasgger?)

I'm also seeing a change in reference behaviour, which I don't know if it is expected. Again, maybe a change in swaggerui? For example, I was using a reference like this:

responses:
  404:
    description: Error
    schema:
      $ref: '#/definitions/get_version_get_error'

but the reference names have changed. Now I need to use:

responses:
  404:
    description: Error
    schema:
      $ref: '#/definitions/error'

@rochacbruno
Copy link
Member

rochacbruno commented Mar 31, 2017

@njarvis

Nice! this is fixed!

  1. OpenAPIspec always expected produces and consumes to be a list, the problem is that in 0.5.14 flasgger just ignored that value in pre-validation and maybe swagger UI also ignored.

fixed I included a fix for backwards compatibility, if flasgger founds a string it will convert to list.

  1. I changed the way references are created, before it was {function_name}_{http_method}_{schema_id} so it generated the get_version_get_error, but I figured out that this is wrong and not in conformity with OpenAPIspec, So the right way to use is having unique names for schemas (maybe pass them as definitions there are some examples/)

However, I know there are many users that may have this problem, so I included an option prefix_ids so the old behavior can also be used. Check examples/compat.py

Thanks

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

No branches or pull requests

2 participants