Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undocumented routes #110

Closed
mvanderlee opened this issue Nov 26, 2019 · 4 comments
Closed

Undocumented routes #110

mvanderlee opened this issue Nov 26, 2019 · 4 comments

Comments

@mvanderlee
Copy link

I have a use-case where I want to have an undocumented internal route, while still using the arguments and response parsing.

Perhaps something like
@blp.route('/hidden-api', document=False)

@lafrech
Copy link
Member

lafrech commented Nov 26, 2019

I understand the feature but I'm not sure it is worth the added API surface.

It should be easy to achieve in user code.

Something like

class MyBlueprint(Blueprint):
    def route(self, rule, *, parameters=None, document=True, **options):
        if document:
            return super().route(rule, parameters=parameters, document=document, **options)
        # Copy else case from Flask Blueprint
        else:
            def decorator(f):
                endpoint = options.pop("endpoint", f.__name__)
                self.add_url_rule(rule, endpoint, f, **options)
                return f
            return decorator      

Would this work for you?

I'm not definitively and strongly opposed to the feature. Just unsure.

@mvanderlee
Copy link
Author

This certainly works for me.

@0x78f1935
Copy link

0x78f1935 commented Aug 24, 2021

I would love to see this feature integrated.

Edit:

Here is an updated version!

# encoding: utf-8
"""
Blueprint adapter
-----------------
Flask-Smorest is not supporting hidden endpoints.
This class follows the following suggestion: https://github.com/marshmallow-code/flask-smorest/issues/110
"""
from flask_smorest import Blueprint
from flask.views import MethodViewType


class EnhancedBlueprint(Blueprint):
    def route(self, rule, *, parameters=None, render=True, **options):
        if render: return super().route(rule, parameters=parameters, **options)
        else: # Hide documentation and swagger UI elements
            def decorator(func):

                # By default, endpoint name is function name
                endpoint = options.pop('endpoint', func.__name__)

                # Prevent registering several times the same endpoint
                # by silently renaming the endpoint in case of collision
                if endpoint in self._endpoints:
                    endpoint = '{}_{}'.format(endpoint, len(self._endpoints))
                self._endpoints.append(endpoint)

                if isinstance(func, MethodViewType):
                    view_func = func.as_view(endpoint)
                else:
                    view_func = func

                # Add URL rule in Flask and store endpoint documentation
                self.add_url_rule(rule, endpoint, view_func, **options)
                # self._store_endpoint_docs(endpoint, func, parameters, **options)

                return func

            return decorator

Edit 2: I created a Pull Request

@mvanderlee
Copy link
Author

This solutions no longer works due to internal updates in flask-smorest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants