question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to structure a real world application

See original GitHub issue

Hi,

I believe that due to recent updates in both apispec and marshmallow my application started to throw warnings and exceptions (e.g., marshmallow.exceptions.RegistryError, apispec.exceptions.DuplicateComponentNameError) that were not happening a few weeks ago.

I couldn’t came up with a minimal example to illustrate this yet. But I wonder if you could provide an example (beyond the basic “Petstore example” from the docs) of how to structure a more realistic application (like using the application factory pattern, splitting schemas and views in different modules, etc).

Regards,

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
svidelacommented, Feb 22, 2019

Hi again,

I wanted to share the 2 things I had to do in order to adapt my application to be compatible with flask-rest-api 0.13 ( actually, apispec>1.0.0b6, marshmallow>3.0.0rc3).

  • Make sure to import all my schemas before calling init_app Otherwise, I’d get:

marshmallow.exceptions.RegistryError: Class with name ‘PetSchema’ was not found. You may need to import the class.

when using the Api.definition decorator with a schema having a nested field declared with a class name string like in two-way nesting in marshmallow schemas .

You can solve this either not using the Api.definition or making sure to import all your schemas before calling init_app.

  • Don’t let apispec to register nested schemas automatically in the spec’s components Otherwise, I’d get either:

apispec.exceptions.DuplicateComponentNameError: Another schema with name “Pet” is already registered.

when using the Api.definition decorator, or

UserWarning: Multiple schemas resolved to the name Person. The name has been modified. Either manually add each of the schemas with a different name or provide a custom schema_name_resolver. UserWarning,

when the Api.definition decorator is not used at all but there are nested fields declared with modifiers (like only=('id', )).

In the first case (using the Api.definition decorator), the issue is that apispec is also registering schemas in the spec’s components using its default schema_name_resolver.

In the second case (not using the Api.definition and having nested fields with modifiers), apispec tries to register the same schema name for each modifiers combination used and shows the warning.

The workaround I found for this is to don’t let apispec to register nested schemas automatically and use the Api.definition decorator. For this I call init_app passing a custom marshmallow plugin:

from flask import Flask

from flask_rest_api import Api
from apispec.ext.marshmallow import MarshmallowPlugin

api = Api()

def create_app():
    app = Flask(__name__)
    
    ma_plugin = MarshmallowPlugin(schema_name_resolver=lambda _: False)
    api.init_app(app, spec_kwargs=dict(marshmallow_plugin=ma_plugin))
    
    return app

For a minimal example reproducing these two issues you may want to checkout flask-rest-api-example.

Regards,

1reaction
lafrechcommented, Oct 10, 2019

I’ve been working on a simple real-life example and I came up with a minimal application using sqlalchemy.

https://github.com/lafrech/flask-smorest-sqlalchemy-example

Not done (are such things ever done ?) but feedback welcome already.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you Learn to Build Substantial, Real-World Apps?
Building a real-world application requires a lot of work and a lot of skills other than technology if you want to set up...
Read more >
How to structure a real-world monorepo Serverless app
In this post we look at how to organize monorepo Serverless Framework apps in detail. We'll also look at how to share common...
Read more >
How Story Structure Relates to the Real World - Storymind
In this article I'd like to share with you some of the insights into the relationship of story structure to real life that...
Read more >
How to Plan a Real World Web Application from Scratch? in 2020 by ...
How to plan a website structure? In today's video I will teach you how you go from idea to actually crafting a professional...
Read more >
What are the real world applications of data structure ... - Quora
Take a business ledger. They are arrays of pages. The pages are arrays of structures, each structure has a number in each column....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found