Giter Site home page Giter Site logo

thnee / flask-api-framework Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 2.0 47 KB

Framework for building semantically correct HTTP API’s in Flask.

Home Page: https://flask-api-framework.rtfd.io

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%
flask flask-sqlalchemy flask-marshmallow http api

flask-api-framework's People

Contributors

thnee avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

notvad gamba47

flask-api-framework's Issues

View schema classes data is shared between requests

Since schema instance is assigned to the view, the same instance is reused for every request to the view. flask-api-framework uses marshmallow.Schema.load method to deserialize passed data to a schema instance. It means that missing non-required fields, dynamically assigned attributes and other instance attributes (eg schema context) will not be re-loaded. It may lead to the situation when a view can access schema data from the previous requests.

Here is an example:

import base64
from marshmallow import Schema, fields, validates


class MySchema(Schema):
    event = fields.String()
    b64_message = fields.String()

    @validates("b64_message")
    def validates_message(self, value):
        self.context["decoded_message"] = base64.b64decode(value).decode()


# assign schema instance to the view class
s = MySchema()

# handling first request
request_data = {"event": "first_event", "b64_message": base64.b64encode("hey".encode()).decode()}
loaded_data = s.load(data=request_data)
print(s.context["decoded_message"])  # prints "hey" which is correct

# handling second request
request_data = {"event": "second_event"}
loaded_data = s.load(data=request_data)
print(s.context["decoded_message"])  # prints "hey" but a KeyError is expected

A possible solution is to assign schema class to the view and create a new schema instance every time before calling marshmallow.Schema.load.

As an alternative, marshmallow.Schema class can be extended with a custom load_new method that returns a new schema instance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.