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

Update examples for Connexion 3.0 #1615

Merged
merged 3 commits into from
Dec 30, 2022
Merged

Conversation

RobbeSneyders
Copy link
Member

This PR updates the examples for Connexion 3.0 and merges them for OpenAPI and Swagger.

2 examples required some changes to make them work:

  • The reverse proxy example required some fixes to the SwaggerUIMiddleware to leverage the root_path correctly. This is included in the PR.
  • The enforced defaults example requires the json validator to adapt the body and pass it on. We currently pass on the original body after validation, and I'm not sure if we should change this. I'll submit a separate PR to discuss this.

@RobbeSneyders RobbeSneyders added this to the Connexion 3.0 milestone Dec 28, 2022
@RobbeSneyders RobbeSneyders force-pushed the feature/update-examples branch 2 times, most recently from 56e0c24 to 72dffa1 Compare December 28, 2022 20:17
@RobbeSneyders RobbeSneyders force-pushed the feature/update-examples branch from 72dffa1 to 90b2908 Compare December 28, 2022 20:21
Copy link
Member Author

@RobbeSneyders RobbeSneyders left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self review.

@@ -20,9 +20,7 @@ def __init__(
self,
import_name,
api_cls,
port=None,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no reason to pass these to the app instead of to app.run directly.

@@ -83,7 +83,11 @@ async def asgi_app(self, scope: Scope, receive: Receive, send: Send) -> None:
)

api_base_path = connexion_context.get("api_base_path")
if api_base_path and not api_base_path == self.base_path:
if (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle case when base path is "", which is the case both when endpoint is not part of an API, or when endpoint is part of the API registered on the root path.

@@ -124,89 +118,11 @@ def index():
logger.debug("Adding %s with decorator", rule, extra=kwargs)
return self.app.route(rule, **kwargs)

def run(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lifted to AbstractApp.

@@ -38,6 +38,7 @@ def parameter_to_arg(
sanitize = pythonic if pythonic_params else sanitized
arguments, has_kwargs = inspect_function_arguments(function)

# TODO: should always be used for AsyncApp
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs some work. Will submit a separate PR for this.

@@ -55,7 +55,7 @@ def _apply_middlewares(
for middleware in reversed(middlewares):
app = middleware(app) # type: ignore
apps.append(app)
return app, reversed(apps)
return app, list(reversed(apps))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure iterator doesn't run out when registering multiple apis.

@@ -42,16 +43,11 @@ def __init__(self, *args, default: ASGIApp, **kwargs):
def normalize_string(string):
return re.sub(r"[^a-zA-Z0-9]", "_", string.strip("/"))

def _base_path_for_prefix(self, request):
def _base_path_for_prefix(self, request: StarletteRequest) -> str:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed for proper usage of root_path behind reverse proxy.

@@ -2,6 +2,8 @@ openapi: 3.0.0
info:
title: API Key Example
version: '1.0'
servers:
- url: /openapi
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a base path now that we're registering two apis on the same app.

info:
title: Basic Auth Example
version: "1.0"

basePath: /swagger
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a base path now that we're registering two apis on the same app.



def echo(data):
# TODO: should work as sync endpoint when parameter decorator is fixed
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the parameter decorator uses a sync decorator when the view function is sync, even when the framework is async. This doesn't work as the async body cannot be read by a sync decorator.

@@ -100,7 +104,8 @@ def readme():
'tests': tests_require,
'flask': flask_require,
'swagger-ui': swagger_ui_require,
'docs': docs_require
'docs': docs_require,
'uvicorn': uvicorn_requires,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Install with connexion[uvicorn] to enable app.run. The standard extra includes the dependencies needed to watch files for reloading the app.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 3796655632

  • 17 of 34 (50.0%) changed or added relevant lines in 9 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.4%) to 91.001%

Changes Missing Coverage Covered Lines Changed/Added Lines %
connexion/apps/async_app.py 0 1 0.0%
connexion/apps/flask_app.py 2 3 66.67%
connexion/middleware/swagger_ui.py 4 8 50.0%
connexion/apps/abstract.py 4 15 26.67%
Totals Coverage Status
Change from base Build 3795637062: -0.4%
Covered Lines: 2983
Relevant Lines: 3278

💛 - Coveralls

@RobbeSneyders RobbeSneyders merged commit 073f0d4 into main Dec 30, 2022
@RobbeSneyders RobbeSneyders deleted the feature/update-examples branch December 30, 2022 19:34
@Subin-Qreative
Copy link

https://github.com/spec-first/connexion/blob/main/examples/jwt/app.py

the following example is targeted at FlaskApp would the same work out of the box by just replacing with AsyncApp ?

@RobbeSneyders
Copy link
Member Author

Yes it should @Subin-Qreative!

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.

3 participants