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.

Documenting path parameters in blueprint url_prefix

See original GitHub issue

How do I document path parameters that appear in url_prefix in a Blueprint? Example:

blueprint = Blueprint(
    'Media',
    __name__,
    url_prefix='/brands/<int:brand_id>/media',
)

@blueprint.route(...)
...

I’ve read through https://github.com/marshmallow-code/flask-smorest/issues/23 but it only seems to apply to @blueprint.route(), not Blueprint().

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lafrechcommented, Jan 9, 2022

I just sent a PR for this. Sorry about the delay. It was less complicated than I expected.

url_prefix can be specified both at Blueprint init and when calling register_blueprint.

I was not totally comfortable with overriding Blueprint.__init__ and I figured it was enough to just add parameters to register_blueprint. The downside I see is that it requires the user to pass that parameter on each call to register_blueprint, which could lead to code duplication in the unlikely case of a Blueprint being registered several times with the same url_prefix (I don’t think this could happen until perhaps we support nested blueprints). It probably also adds a bit of complexity for apps where all blueprints are registered in a loop, as they must manage this special case. I assume codes with such advanced patterns can easily handle the added complexity.

I’m happy with this resolution and I shall merge #313 pretty soon.

0reactions
kaoscommented, Dec 11, 2020

how about

class MyBlueprint (Blueprint):
    def __init__(self, *args, prefix_parameters=None, **kwargs):
        super().__init__(*args, **kwargs)
        self._prefix_parameters = prefix_parameters

    def route(self, *args, parameters=None, **kwargs):
        if self._prefix_parameters is not None:
            parameters = self._prefix_parameters + (parameters or [])

        return super().route(*args, parameters=parameters, **kwargs)


# Usage:

blueprint = MyBlueprint(
    'Media',
    __name__,
    url_prefix='/brands/<int:brand_id>/media',
    prefix_parameters=[
        {...}
    ]
)

@blueprint.route(...)
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

Blueprints and Views — Flask Documentation (2.2.x)
A Blueprint is a way to organize a group of related views and other code. ... The url_prefix will be prepended to all...
Read more >
Blueprints — Sanic 22.9.1 documentation - Read the Docs
See user guide re: blueprints. Parameters. name (str) – unique name of the blueprint. url_prefix (Optional[str]) – URL to be prefixed before all...
Read more >
Flask blueprint and registering path - Stack Overflow
Since you have url_prefix in your blueprint, it means the system is looking for all routes that start with that value i.e. /search...
Read more >
APIBlueprint - flask-openapi3
Generate RESTful API and OpenAPI document for your Flask project. ... trail_slash = path_url.endswith("/") # merge url_prefix and new api blueprint path url ......
Read more >
Blueprints — Explore Flask 1.0 documentation
route () . Using a dynamic URL prefix¶. Continuing with the Facebook example, notice how all of the profile routes start with the...
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