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

Different endpoints in swagger "Try it out" fire to the same endpoint when they have the same function name #127

Closed
fluffybrain3 opened this issue Nov 17, 2023 · 4 comments · Fixed by #133
Labels
bug Something isn't working

Comments

@fluffybrain3
Copy link

fluffybrain3 commented Nov 17, 2023

Environment:

  • Python version: 3.12
  • Operating system: Windows 11
  • Flask version: 3.0.0
  • flask-openapi3 version: 3.0.0

I will try to show that using a quick example:

parent1_bp = APIBlueprint('Parent 1', __name__, url_prefix='/parent1', abp_tags=[parent1_tag])
children_p1_bp  = APIBlueprint('Child p1', __name__, url_prefix='/children', abp_tags=[chilren_p1_tag])
parent1_bp.register_api(children_p1_bp)

parent2_bp = APIBlueprint('Parent 2', __name__, url_prefix='/parent2', abp_tags=[parent2_tag])
children_p2_bp  = APIBlueprint('Child p2', __name__, url_prefix='/children', abp_tags=[chilren_p2_tag])
parent2_bp.register_api(children_p2_bp)

root_parent_bp = APIBlueprint('Root Parent', __name__, url_prefix='/root', abp_tags=[root_tag])
root_parent_bp.register_api(parent1_bp)
root_parent_bp.register_api(parent2_bp)

Which gives 2 endpoints:
/root/parent1/children
/root/parent2/children

In swagger both endpoints are displayed separately, but both of them in the "Try it out" in swagger will fire to the first one.
When you make a request outside of the swagger everything works fine, but in swagger it doesn't.
Also, I didn't have this issue while using the flask-openapi3 version 2.5.4.

@fluffybrain3 fluffybrain3 added the bug Something isn't working label Nov 17, 2023
@luolingchun
Copy link
Owner

Please make sure that before using the register_api, the decorator decorates some api, here's my example, when nothing is wrong, if it doesn't solve your problem, paste your executeable code.

from flask_openapi3 import OpenAPI, APIBlueprint

app = OpenAPI(__name__)

parent1_bp = APIBlueprint('Parent 1', __name__, url_prefix='/parent1')
children_p1_bp = APIBlueprint('Child p1', __name__, url_prefix='/children')

parent2_bp = APIBlueprint('Parent 2', __name__, url_prefix='/parent2')
children_p2_bp = APIBlueprint('Child p2', __name__, url_prefix='/children')

root_parent_bp = APIBlueprint('Root Parent', __name__, url_prefix='/root')


@children_p1_bp.get('')
def get_children1():
    return {"message": "children1"}


@children_p2_bp.get('')
def get_children2():
    return {"message": "children2"}


parent1_bp.register_api(children_p1_bp)
parent2_bp.register_api(children_p2_bp)

root_parent_bp.register_api(parent1_bp)
root_parent_bp.register_api(parent2_bp)

app.register_api(root_parent_bp)

if __name__ == '__main__':
    app.run(debug=True)

@fluffybrain3
Copy link
Author

fluffybrain3 commented Nov 20, 2023

Your code works fine for me.
I created a working example with the issue.
I think the issue lies within the function names.
If you name the functions hello_1() and hello_2() instead of hello() it works fine.
But it was working fine with the same function names in the previous library version.

main.py

from flask_openapi3 import APIBlueprint, OpenAPI

from app_1 import app_1_bp
from app_2 import app_2_bp

app = OpenAPI(__name__)

root_bp = APIBlueprint('App', __name__, url_prefix='/app')
root_bp.register_api(app_1_bp)
root_bp.register_api(app_2_bp)
app.register_api(root_bp)

if __name__ == '__main__':
    app.run(debug=True)

app_1.py

from flask_openapi3 import Tag, APIBlueprint

app_1_tag = Tag(name='App 1')
app_1_bp = APIBlueprint('App 1', __name__, url_prefix='/app_1', abp_tags=[app_1_tag])


@app_1_bp.get('/hello')
def hello():
    return 'Hello, World 1!'

app_2.py

from flask_openapi3 import APIBlueprint, Tag

app_2_tag = Tag(name='App 2')
app_2_bp = APIBlueprint('App 2', __name__, url_prefix='/app_2', abp_tags=[app_2_tag])


@app_2_bp.get('/hello')
def hello():
    return 'Hello, World 2!'

image

@fluffybrain3 fluffybrain3 changed the title 2 APIBlueprints with different names, but same endpoint names fire to only the first endpoint in swagger "Try it out" Different endpoints in swagger "Try it out" fire to the same endpoint when they have the same function name Nov 20, 2023
@luolingchun
Copy link
Owner

Thanks! this is a problem caused by the same operationId, and I will fix it.

"paths": {
        "/app/app_1/hello": {
            "get": {
                "operationId": "hello_hello_get",
                "responses": {},
                "tags": [
                    "App 1"
                ]
            }
        },
        "/app/app_2/hello": {
            "get": {
                "operationId": "hello_hello_get",
                "responses": {},
                "tags": [
                    "App 2"
                ]
            }
        }
    },

@fluffybrain3
Copy link
Author

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants