Comments (5)
Not a problem, glad you got it resolved!
from flask_accepts.
Hi, do you have any info on this?
Thanks!
from flask_accepts.
I've not been able to replicate this. Could you provide a code snippet that reproduces the issue so that I can take a look?
from flask_accepts.
I have basically the same structure as flask_api_example
has, but i changed fields.Number
in schema to fields.Integer
, at first i thought that could be the issue but can't replicate doing the same in your example. It could be possible that it is my fault, but couldn't find the issue. Excuse me if that's the case, i don't have much experience at programming and i don't fully understand how the abstractions and interactions between the layers are.
I don't know which parts do you need to take a look at, sorry for the long comment.
app/barbota/ranks/schema.py
from marshmallow import fields, Schema
class RanksSchema(Schema):
"""Barbota Ranks schema"""
rankId = fields.Integer(attribute="rank_id")
rankName = fields.String(attribute="rank_name")
skinName = fields.String(attribute="skin_name")
levelNeeded = fields.Integer(attribute="level_needed")
hpGain = fields.Integer(attribute="hp_gain")
dmgGain = fields.Integer(attribute="dmg_gain")
curationGain = fields.Integer(attribute="curation_gain")
armorGain = fields.Integer(attribute="armor_gain")
price = fields.Integer(attribute="price")
userCount = fields.Integer(attribute="user_count")
app/barbota/ranks/model.py
from sqlalchemy import Integer, Column, String
from app import db # noqa
from .interface import RanksInterface
from typing import Any
class Ranks(db.Model): # type: ignore
"""A Rank"""
__tablename__ = "ranks"
rank_id = Column(Integer(), primary_key=True)
rank_name = Column(String(255))
skin_name = Column(String(255))
level_needed = Column(Integer())
hp_gain = Column(Integer())
dmg_gain = Column(Integer())
curation_gain = Column(Integer())
armor_gain = Column(Integer())
price = Column(Integer())
user_count = Column(Integer())
def update(self, changes: RanksInterface):
for key, val in changes.items():
setattr(self, key, val)
return self
app/barbota/ranks/interface.py
from mypy_extensions import TypedDict
class RanksInterface(TypedDict, total=False):
rank_id: int
rank_name: str
skin_name: str
level_needed: int
hp_gain: int
dmg_gain: int
curation_gain: int
armor_gain: int
price: int
user_count: int
app/barbota/ranks/controller.py
@accepts(schema=RanksSchema, api=api)
@responds(schema=RanksSchema)
def put(self, rankId: int) -> Ranks:
"""Update Rank"""
try:
if request.headers["Apikey"] != API_KEY:
api.abort(403)
except KeyError:
api.abort(403)
changes: RanksInterface = request.parsed_obj
Ranks = RanksService.get_by_id(rankId)
return RanksService.update(Ranks, changes)
app/barbota/ranks/service.py
@staticmethod
def update(ranks: Ranks, Ranks_change_updates: RanksInterface) -> Ranks:
ranks.update(Ranks_change_updates)
db.session.commit()
return Ranks
- example table contents (
/api/barbota/ranks/
)
[
{
"price": 0,
"curationGain": 0,
"hpGain": 0,
"rankId": 1,
"dmgGain": 0,
"skinName": "barbita",
"armorGain": 0,
"rankName": "Recluta",
"userCount": 0,
"levelNeeded": 0
},
{
"price": 0,
"curationGain": 0,
"hpGain": 0,
"rankId": 2,
"dmgGain": 0,
"skinName": "barbita",
"armorGain": 0,
"rankName": "Ayudante",
"userCount": 0,
"levelNeeded": 5
},
{
"price": 10000,
"curationGain": 0,
"hpGain": 0,
"rankId": 3,
"dmgGain": 0,
"skinName": "barbita",
"armorGain": 0,
"rankName": "Aspirante",
"userCount": 0,
"levelNeeded": 10
}
]
- put request to:
/api/barbota/ranks/1
{
"price": 0,
"curationGain": 0,
"hpGain": 0,
"rankId": 1,
"dmgGain": 0,
"skinName": "barbita",
"armorGain": 0,
"rankName": "Recluta",
"userCount": 0,
"levelNeeded": 0
}
- response body (
500 Error: INTERNAL SERVER ERROR
)
{
"message": "Internal Server Error"
}
- response headers
connection: keep-alive
content-length: 37
content-type: application/json
date: Wed, 18 Dec 2019 17:39:41 GMT
server: gunicorn/19.9.0
vary: Origin
via: 1.1 vegur
- traceback (heroku, cleaner in original issue comment)
2019-12-18T17:43:17.932906+00:00 app[web.1]: [2019-12-18 17:43:17,932] ERROR in app: Exception on /api/barbota/ranks/1 [PUT]
2019-12-18T17:43:17.932938+00:00 app[web.1]: Traceback (most recent call last):
2019-12-18T17:43:17.932940+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
2019-12-18T17:43:17.932941+00:00 app[web.1]: rv = self.dispatch_request()
2019-12-18T17:43:17.932943+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
2019-12-18T17:43:17.932945+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2019-12-18T17:43:17.932946+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask_restplus/api.py", line 325, in wrapper
2019-12-18T17:43:17.932948+00:00 app[web.1]: resp = resource(*args, **kwargs)
2019-12-18T17:43:17.932949+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/views.py", line 89, in view
2019-12-18T17:43:17.932951+00:00 app[web.1]: return self.dispatch_request(*args, **kwargs)
2019-12-18T17:43:17.932953+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask_restplus/resource.py", line 44, in dispatch_request
2019-12-18T17:43:17.932954+00:00 app[web.1]: resp = meth(*args, **kwargs)
2019-12-18T17:43:17.932955+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask_accepts/decorators/decorators.py", line 100, in inner
2019-12-18T17:43:17.932956+00:00 app[web.1]: return func(*args, **kwargs)
2019-12-18T17:43:17.932958+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask_accepts/decorators/decorators.py", line 186, in inner
2019-12-18T17:43:17.932959+00:00 app[web.1]: serialized = schema.dump(rv)
2019-12-18T17:43:17.932960+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/marshmallow/schema.py", line 553, in dump
2019-12-18T17:43:17.932962+00:00 app[web.1]: result = self._serialize(processed_obj, many=many)
2019-12-18T17:43:17.932963+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/marshmallow/schema.py", line 517, in _serialize
2019-12-18T17:43:17.932965+00:00 app[web.1]: value = field_obj.serialize(attr_name, obj, accessor=self.get_attribute)
2019-12-18T17:43:17.932966+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/marshmallow/fields.py", line 325, in serialize
2019-12-18T17:43:17.932968+00:00 app[web.1]: return self._serialize(value, attr, obj, **kwargs)
2019-12-18T17:43:17.932969+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/marshmallow/fields.py", line 899, in _serialize
2019-12-18T17:43:17.932970+00:00 app[web.1]: ret = self._format_num(value) # type: _T
2019-12-18T17:43:17.932971+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/marshmallow/fields.py", line 874, in _format_num
2019-12-18T17:43:17.932973+00:00 app[web.1]: return self.num_type(value)
2019-12-18T17:43:17.932979+00:00 app[web.1]: TypeError: int() argument must be a string, a bytes-like object or a number, not 'InstrumentedAttribute'
from flask_accepts.
Hey, i was able to figure out the issue, it was my mistake.
I was returning Ranks
instead of ranks
in service.py
...
So sorry for wasting your time and thanks.
from flask_accepts.
Related Issues (20)
- When using the @responds decorator, X-Fields headers are automatically applied to the swagger request HOT 2
- Swagger fails to load when a schema does not have a default set in one of the fields. HOT 4
- Marshmallow schema for form data HOT 1
- Query parameter and header schemas do not respect the data key attribute HOT 1
- Passing a schema type to the `schema` argument of @accepts ignores the type's Meta attributes
- Marshmallow 4 breaking changes
- Overriding flask_restx field
- Nested Marshmallow Schemas throwing error on Swagger HOT 5
- Partial param for Schema loading HOT 4
- [Q] Customize schema_errors? HOT 2
- Only swagger generation for end-point HOT 10
- What is the best practice for file uploads? HOT 2
- Working with UUIDs, Marshmallow & 'missing' fields
- Nested schemas with many=true do not generate a list of schemas in the swagger docs HOT 6
- The new partial keyword of accepts() breaks the marshmallow schema
- Using multiple different schemas in accepts decorator raises KeyError if there is a ValidationError
- Cookie header validation works on gunicorn, does not on NGINX
- Static typing on request.parsed_obj
- More forgiving loading of the JSON schema payload ignoring the mimetype
- Skip response fields where value is None
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flask_accepts.