jmcarp / jmcarp/flask-apispec

Cannot Make Response Schema

Open
#253 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
652
Forks
151
PR merge metrics
No merged PRs in 30d

Description

I am trying to define a simple resource with a sample response schema so as to display in the docs. I only have two attributes in the class and every time I am trying to run it I get:
```
Traceback (most recent call last):
File "", line 1, in
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/tenacity/__init__.py", line 333, in wrapped_f
return self(f, *args, **kw)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/tenacity/__init__.py", line 423, in __call__
do = self.iter(retry_state=retry_state)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/tenacity/__init__.py", line 360, in iter
return fut.result()
File "/usr/local/lib/python3.8/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/usr/local/lib/python3.8/concurrent/futures/_base.py", line 388, in __get_result
raise self._exception
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/tenacity/__init__.py", line 426, in __call__
result = fn(*args, **kwargs)
File "/home/app/amp/helpers/check_db_available.py", line 14, in main
app = create_app()
File "/home/app/amp/app.py", line 92, in create_app
load_resources(app)
File "/home/app/amp/core/loaders/resource.py", line 19, in load_resources
app.docs.register(r, endpoint=r.endpoint)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/flask_apispec/extension.py", line 127, in register
self._defer(self._register, target, endpoint, blueprint,
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/flask_apispec/extension.py", line 71, in _defer
bound()
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/flask_apispec/extension.py", line 157, in _register
self.spec.path(**path)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/core.py", line 534, in path
plugin.operation_helper(path=path, operations=operations, **kwargs)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/ext/marshmallow/__init__.py", line 224, in operation_helper
self.resolver.resolve_operations(operations)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/ext/marshmallow/schema_resolver.py", line 35, in resolve_operations
self.resolve_response(response)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/ext/marshmallow/schema_resolver.py", line 183, in resolve_response
self.resolve_schema(response)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/ext/marshmallow/schema_resolver.py", line 225, in resolve_schema
data["schema"] = self.resolve_schema_dict(data["schema"])
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/ext/marshmallow/schema_resolver.py", line 295, in resolve_schema_dict
return self.converter.resolve_nested_schema(schema)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/ext/marshmallow/openapi.py", line 134, in resolve_nested_schema
self.spec.components.schema(name, schema=schema)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/core.py", line 171, in schema
ret.update(plugin.schema_helper(component_id, ret, **kwargs) or {})
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/ext/marshmallow/__init__.py", line 182, in schema_helper
json_schema = self.converter.schema2jsonschema(schema_instance)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/ext/marshmallow/openapi.py", line 253, in schema2jsonschema
jsonschema = self.fields2jsonschema(fields, partial=partial)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/ext/marshmallow/openapi.py", line 278, in fields2jsonschema
prop = self.field2property(field_obj)
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/ext/marshmallow/field_converter.py", line 172, in field2property
ret.update(attr_func(field, ret=ret))
File "/home/app/cache/virtualenvs/amp-_geWzZZ1-py3.8/lib/python3.8/site-packages/apispec/ext/marshmallow/field_converter.py", line 223, in field2default
default = field.load_default
AttributeError: 'String' object has no attribute 'load_default'
```

I am defining it according to the documentation:
``` python
from amp.core.models.base import APIResource
from flask_apispec import doc, marshal_with
from marshmallow import Schema, fields
from amp.core.extensions.db import db

__all__ = ('HealthCheckResource',)

class HealthCheckResponseSchema(Schema):
status = fields.String(default="ok")
db = fields.String(default="ok")

class HealthCheckResource(APIResource):
url = "/api/healthcheck"
endpoint = "api.healthcheck"

@doc(
summary="General health check",
description="My First GET Awesome API.",
tags=['Health Check'],
)
@marshal_with(
schema=HealthCheckResponseSchema,
description="Successful operation",
code=200)
def get(self):
db_status = "ok"
try:
db.engine.execute("SELECT 1")
except Exception as e:
db_status = db_status = "degraded"
statuses = [
db_status
]

if any([s != "ok" for s in statuses]):
return {
"status": "degraded",
"db": db_status
}
else:
return {
"status": "ok",
"db": db_status,
}
```

Any help is much appreciated!

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the failure from helpers/check_db_available.py and follow app.py into core/loaders/resource.py, where app.docs.register processes HealthCheckResource. Inspect the HealthCheckResponseSchema used by marshal_with and confirm that the health-check response can register successfully and appear in the generated docs without the AttributeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, documentation
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.