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

Do not remove all kwargs when calling view. #144

Closed

Conversation

maldoinc
Copy link

Checklist:

  • Run pytest tests and no failed.
  • Run flake8 flask_openapi3 tests examples and no failed.
  • Run mypy flask_openapi3 and no failed.
  • Run mkdocs serve and no failed.

This fixes #143

@luolingchun
Copy link
Owner

OpenAPI testing not passed.

@luolingchun
Copy link
Owner

Why don't you use the following way:

@app.get("/<version>/greet")
def view(query: GreetQuery):
    greeter = GreeterService()
    return greeter.greet(query.name)

I feel like you need to write a more typical case to illustrate the scenario you encountered.

@maldoinc
Copy link
Author

The scenario here is usage with a DI framework such as wireup which wraps all views that use its service objects. Dependencies are injected by the framework instead of the user having to create them manually.

@luolingchun
Copy link
Owner

I made some tweaks to use a single decorator instead of giving all the things that were trying to decorate.

def inject(fn):
    @wraps(fn)
    def _inner(*args, **kwargs):
        return fn(*args, **kwargs, greeter=GreeterService())

    return _inner


@app.get("/<version>/greet")
@inject
def view(greeter: GreeterService, query: GreetQuery):
    return greeter.greet(query.name)

app.view_functions = {k: inject(v) for k, v in app.view_functions.items()}

Inject a decorator like wireup under the routing decorator.

@app.get("/posts")
@container.autowire 
# Decorate all targets where the library must perform injection, such as views in an Api.
# Services are automatically injected based on annotated type.
# Optional for views when using flask or fastapi integration.
def get_posts(post_repository: PostRepository):
    return post_repository.find_all()

In theory, flask-openapi3 is fully compatible with Flask.

@maldoinc
Copy link
Author

That does work, yes. Per the previous post when the call order is flask -> flask_openapi3 -> anything else it works as it should. What doesn't work is when the call order is flask -> something else -> flask_openapi3, hence why the tests are that way.

I also think the changeset is small and makes sense: You're only removing path parameters and keeping everything else, because _validate_request should really touch only arguments that are related to path variables.

Maybe it's not a super common scenario but it is a buggy one regardless. Feel free to close this if it's not something you'd want to add.

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

Successfully merging this pull request may close these issues.

Issues using flask-openapi3 when its views are decorated
2 participants