Skip to content

Conversation

@chrisinmtown
Copy link
Contributor

@chrisinmtown chrisinmtown commented Jan 4, 2025

The example Connexion app processes JSON requests and responses as specified with OpenAPI v2 (aka Swagger) or OpenAPI v3 file format.
JSON responses are validated against the spec.
An error handler catches exceptions raised while processing a request.
Tests are run by tox which also reports code coverage.

Fixes: #1988

@chrisinmtown chrisinmtown force-pushed the example-testclient branch 2 times, most recently from bd956aa to a6706ef Compare January 4, 2025 13:22
@chrisinmtown
Copy link
Contributor Author

chrisinmtown commented Jan 4, 2025

Thanks for approving a run of the test pipeline on this PR. I botched the import order in my initial attempts, I'm learning to run isort on everything first. I'm struggling to figure out the exact version of isort to use, or the right configuration, because the import ordering set by my local isort was rejected by the tox pre-commit step :/

@coveralls
Copy link

coveralls commented Jan 4, 2025

Coverage Status

coverage: 94.292%. remained the same
when pulling 74a45e5 on chrisinmtown:example-testclient
into 0bf9d1f on spec-first:main.

Copy link
Member

@Ruwann Ruwann left a comment

Choose a reason for hiding this comment

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

Thanks! Already looking very nice, left some small comments/questions

:return: ConnexionResponse with RFC7087 problem details
"""
# log the request URL, exception and stack trace
logger.exception("Request to %s caused exception", request.url)
Copy link
Member

Choose a reason for hiding this comment

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

Does this work properly, don't we need to supply the exception as an argument via exc_info=ex?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The python logger does magic when you call its exception method, and that magic breaks when the context changes, like when running the function from a new thread pool. I will test what you suggest - pass the exception - and see if the full stack trace is shown.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for @Ruwann for pointing me to the exc_info parameter. Yes, the stack trace appears as expected when that parameter is passed. That seems like a better solution than using async to define the exception handler function.

Copy link
Member

Choose a reason for hiding this comment

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

What do you think of renaming this file instead of using __init__.py? Or add it to app.py? To keep it more consistent with other examples

Copy link
Contributor Author

@chrisinmtown chrisinmtown Feb 5, 2025

Choose a reason for hiding this comment

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

Would appreciate guidance on the right way, or the connexion way, or what have you :) to structure an app for use from tox. Creating the app object in __init__.py makes that easy but I'm sure it's not the only way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I restructured this example to use a single file, and eliminate the package hello. Please review again. Thanks in advance.

from . import conn_app

# reuse the configured logger
logger = logging.getLogger("uvicorn.error")
Copy link
Member

Choose a reason for hiding this comment

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

What's the reasong for using the uvicorn.error logger instead of a specific one for this file/application?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was basically a cheat, the level is set appropriately on this logger so the output from my code appears together with output from uvicorn, and no additional configuration is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please say if using the unicorn.error logger is a deal breaker and I will change the logging.

@Ruwann
Copy link
Member

Ruwann commented Feb 1, 2025

Thanks for approving a run of the test pipeline on this PR. I botched the import order in my initial attempts, I'm learning to run isort on everything first. I'm struggling to figure out the exact version of isort to use, or the right configuration, because the import ordering set by my local isort was rejected by the tox pre-commit step :/

No worries, I have been using ruff on my other projects as it combines a lot of these into a single tool, that's more easy to use imo. Is it still unclear?

@chrisinmtown chrisinmtown force-pushed the example-testclient branch 3 times, most recently from cd037f2 to 3088c4d Compare April 12, 2025 13:02
@chrisinmtown chrisinmtown changed the title Add example of the Starlette test client on a simple Connexion REST app Add example of using the Starlette test client with tox Apr 12, 2025
@chrisinmtown chrisinmtown force-pushed the example-testclient branch 4 times, most recently from 2ac0347 to 3ade3ab Compare April 13, 2025 11:07
@chrisinmtown
Copy link
Contributor Author

Greetings @Ruwann and @RobbeSneyders I think I addressed all your comments on this PR, please advise if you would like this example in the V3 codebase, thanks.

@chrisinmtown
Copy link
Contributor Author

This PR addresses issue #1988 with an example instead of a revised document. Please comment.

This example Connexion app processes JSON requests and responses as specified
  with OpenAPI v2 (aka Swagger) or OpenAPI v3 file format.
The app asks Connexion to validate JSON responses against the spec.
The app defines an error handler that catches exceptions raised while processing a request.
Define automated tests that are run by tox.
@chrisinmtown
Copy link
Contributor Author

Looks like two my PRs are fighting with each other :( This PR adds a "tests" directory, it's the only example with that. Unfortunately the very recently merged change to tox.ini (that allows choice of scope) now seems to cause pytest to pull in the newly added "tests/conftest.py" file from this new example instead of the top-level "test/conftest.py" file, leading to immediate failures. I speculate it's because of the entry "testspaths=tests" in tox.ini. Trying to figure out a workaround.

@chrisinmtown
Copy link
Contributor Author

I submitted a PR to restore the tests argument to the poetry invocation of pytest, that limits the directory scope, and then the files introduced by this PR are not picked up and used during a normal test run of Connexion, see #2092

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.

Extend migration guide with details about Connexion test_client for testing and tox

3 participants