luolingchun / flask-openapi3

Generate REST API and OpenAPI documentation for your Flask project.
https://luolingchun.github.io/flask-openapi3/
MIT License
189 stars 29 forks source link

[Feature Request] Support for Nested APIBlueprint #17

Closed dvaerum closed 2 years ago

dvaerum commented 2 years ago

Hey flask-openapi3 project :)

Are there any plans for adding support for nested APIBlueprint, similar to the Blueprint from Flask? Link to docs: https://flask.palletsprojects.com/en/2.1.x/api/?highlight=register_blueprint#flask.Blueprint.register_blueprint

I have been trying to play around with an implementation for this. Currently, it looks like this.

# ...

from urllib.parse import urljoin

# ...

class APIBlueprint

# ...

    def register_api(self: APIBlueprint, api: APIBlueprint) -> None:
        # Don't know if this will work, have not really used tags 😅 
        for tag in api.tags:
            if tag.name not in self.tag_names:
                self.tags.append(tag)

        # This seems to work, pretty well
        for path, path_item in api.paths.items():
            path: str
            path_item: PathItem
            joined_path = urljoin(self.url_prefix, path.lstrip("/"))
            self.paths[joined_path] = path_item

        # I could imagion that this could give some problems with classes name overlapping, but it is early testing
        self.components_schemas.update(**api.components_schemas)

        self.register_blueprint(api)
luolingchun commented 2 years ago

Looks to work and I will start working on this when I have time.

dvaerum commented 2 years ago

Nice to hear @luolingchun, I would like to help, if nothing else I can do some testing. I am also planing to work more on it, but I have a 3 weeks holidays so nothing is happening in this time :sweat_smile:

luolingchun commented 2 years ago

I have submitted a PR #18

You can do some testing and you can fix some bug if you find it.

dvaerum commented 2 years ago

Nice, I will differently try that when I am back at work :wink: For now, I will close this issue because I think it makes more sense to continue in the PR https://github.com/luolingchun/flask-openapi3/pull/18