jmcarp / flask-apispec

MIT License
655 stars 156 forks source link

Allow defining multple `flask-apispec` blueprints #234

Open lucatpa opened 2 years ago

lucatpa commented 2 years ago

I have an API with multiple versions and it would be nice to provide different URLs for different versions to have things more cleanly (I don't want to distract users with deprecated APIs but still provide docs for users stuck with the deprecated API).

I tried this trick to build multiple apispecs at different URLs:

    for bp in blueprints:
        app.config.update({
            'APISPEC_SPEC': APISpec(
                title='Accounts Service',
                version=bp.name,
                openapi_version='2.0',
                plugins=[MarshmallowPlugin()],
            ),
            'APISPEC_SWAGGER_URL': f'/swagger/{bp.name}',
            'APISPEC_SWAGGER_UI_URL': '/swagger/{bp.name}/ui',
        })
        spec = FlaskApiSpec(app)

        for name, endpoint in spec.app.view_functions.items():
            if not isinstance(endpoint, types.FunctionType):
                continue

            if '.' in name:
                spec.register(endpoint, blueprint=name[:name.index('.')])
            else:
                spec.register(endpoint)

But flask would complain about Both share the same name "flask-apispec". Blueprints that are created on the fly need unique names. because this blueprint name is hardcoded and can't be changed:

https://github.com/jmcarp/flask-apispec/blob/de6f5adbcf3e6fce14aa1e1288ac2e401fd9ca35/flask_apispec/extension.py#L75-L81

It would be nice to provide a way to override the blueprint name.

rprinsloo commented 2 years ago

Work was done back in 2020 to allow this PR 167. The PR is stale and might need some work.

I'm picking up a project using the above fix and will report back should I have time to visit this PR to get it into mainstream (if that is desired by the project maintainers).