Skip to content

Support for Automatic Schema Object Instantiation in Flask-Smorest #759

@IdleSys

Description

@IdleSys

Overview

Currently, Flask-Smorest provides request data to view functions as a dictionary or keyword arguments after validation through Marshmallow schemas. While this approach works well for simple data handling, it lacks an option for automatically instantiating schema-defined objects, forcing developers to manually create instances of their domain models within view functions.

This proposal suggests adding an optional make_instance=True parameter to @blp.arguments(), allowing Flask-Smorest to return instantiated schema objects directly, improving type safety and enabling object-oriented design patterns.

Motivation

  • By allowing automatic instantiation of schema objects, developers can:

  • Improve Type Safety: View functions can operate on structured objects rather than untyped dictionaries.

  • Encapsulate Business Logic: Schema objects can have methods that encapsulate logic, reducing redundancy in views.

  • Enhance Code Readability: Working with objects is more intuitive than handling raw dictionaries, particularly in larger codebases.

  • Decouple Views from Data Parsing: Business models can be constructed directly from validated input data without needing additional transformation.

Proposed Implementation

Introduce an optional parameter to the @blp.arguments() decorator:

@blp.arguments(UserSchema, make_instance=True)
def create_user(user: UserSchema):
    user.some_business_method()  # Call methods on the instantiated object
    return {"message": "User created"}, 201

This would require:

  • Extending the arguments() method to check for make_instance=True.

  • Modifying the behavior of request parsing so that Schema.load() is called with make_instance=True (if available) or using a custom method like make_object().

  • Ensuring compatibility with existing functionalities (e.g., as_kwargs).

Potential Drawbacks

While this feature brings significant benefits, there are some potential downsides:

  • Additional Complexity: Introducing this feature requires extra logic in Flask-Smorest to handle schema instantiation dynamically.

  • Dependency on Marshmallow Behavior: If Marshmallow changes how load() or instantiation works, it could introduce breaking changes.

Conclusion

Adding automatic schema instantiation as an optional feature would enhance Flask-Smorest’s flexibility and usability for object-oriented programming. By making it opt-in, the framework can maintain backward compatibility while providing a structured alternative for developers who prefer working with objects instead of raw dictionaries.

Would love to hear thoughts from the maintainers and community regarding this feature!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions