Skip to content

Conversation

@soapun
Copy link
Contributor

@soapun soapun commented Sep 24, 2025

Add httpx (async) as library for python generator

to fix #19255

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@wing328
Copy link
Member

wing328 commented Sep 25, 2025

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

@wing328
Copy link
Member

wing328 commented Sep 25, 2025

thanks for the PR.

what about adding async support to the existing httpx library via an option (e.g. supportAsync) instead creating another library?

(have a single httpx library to support both async and sync requests will be easier to maintain)

@wing328
Copy link
Member

wing328 commented Sep 25, 2025

cc @cbornet (2017/09) @tomplus (2018/10) @krjakbrjak (2023/02) @fa0311 (2023/10) @multani (2023/10)

@soapun
Copy link
Contributor Author

soapun commented Sep 25, 2025

I've linked my commits, thanks for noticing!
Yeah I'll add sync support

@wing328
Copy link
Member

wing328 commented Sep 25, 2025

Yeah I'll add sync support

sorry please put it on hold

we can add it later if that's a demand for it (sync support)

@@ -0,0 +1,15 @@
generatorName: python
outputDir: samples/openapi3/client/petstore/python-httpx
Copy link
Member

Choose a reason for hiding this comment

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

additionalProperties.put("tornado", "true");
} else if ("httpx".equals(getLibrary())) {
supportingFiles.add(new SupportingFile("httpx/rest.mustache", packagePath(), "rest.py"));
additionalProperties.put("asyncio", "true");
Copy link
Member

Choose a reason for hiding this comment

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

can you please explain why asyncio (library option) needs to be set to true even for httpx library?

Copy link
Contributor Author

@soapun soapun Sep 25, 2025

Choose a reason for hiding this comment

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

For all request methods in clients higher than RESTClientObject to be async def

like here:

api_client.mustache

api.mustache

So "asyncio" is actually not only a library option

Copy link
Member

Choose a reason for hiding this comment

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

my take is to refactor it if both asyncio and httpx need the same piece of code

for example, add the tag {{#async}} ....{{/async}} that is enabled by both asyncio and httpx libraries

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 agree

Copy link
Contributor Author

Choose a reason for hiding this comment

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

btw current library name "asyncio" is a bit misleading

asyncio - part of python stdlib (coros, eventloop and so on), not a client library
aiohttp - 3rd party rest client library

Copy link
Member

Choose a reason for hiding this comment

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

please PM me via Slack if you want to further discussion this

2. hand written tests for python-httpx
3. CI workflow updated
@wing328
Copy link
Member

wing328 commented Sep 26, 2025

please review the build failure when you've time

@wing328
Copy link
Member

wing328 commented Sep 26, 2025

also I wonder if you can add a simple test to add a pet to the petstore server and get it by id similar to https://github.com/OpenAPITools/openapi-generator/blob/master/samples/openapi3/client/petstore/python/tests/test_pet_api.py#L137

this can ensure requests using httpx work with add_pet and get_pet_by_id

@soapun
Copy link
Contributor Author

soapun commented Sep 26, 2025

also I wonder if you can add a simple test to add a pet to the petstore server and get it by id similar to https://github.com/OpenAPITools/openapi-generator/blob/master/samples/openapi3/client/petstore/python/tests/test_pet_api.py#L137

this can ensure requests using httpx work with add_pet and get_pet_by_id

i've just copied hand written tests from plain python and fixed some implementation specific things. So this test is already present

https://github.com/soapun/openapi-generator/blob/python-httpx/samples/openapi3/client/petstore/python-httpx/tests/test_pet_api.py#L130

@wing328
Copy link
Member

wing328 commented Sep 26, 2025

all tests passed. i'll get it merged over the weekend before the upcoming release.

thanks for the contribution.

have a nice weekend.

@wing328 wing328 added this to the 7.16.0 milestone Sep 27, 2025
@wing328 wing328 changed the title [python] fix #19255 add async httpx support [python] add async httpx support Sep 27, 2025
@wing328 wing328 merged commit bab5ca2 into OpenAPITools:master Sep 27, 2025
36 checks passed
@thoumasd
Copy link

Thanks for the contribution @soapun!

@wing328
Copy link
Member

wing328 commented Oct 1, 2025

@soapun when you've time, can you please PM me via Slack?

https://join.slack.com/t/openapi-generator/shared_invite/zt-36ucx4ybl-jYrN6euoYn6zxXNZdldoZA

rajvesh pushed a commit to rajvesh/openapi-generator that referenced this pull request Dec 25, 2025
* [python] fix OpenAPITools#19255 add async httpx support

* update docs

* 1. "async" parameter for templates
2. hand written tests for python-httpx
3. CI workflow updated

* fix mypy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Doesn't support sync/async generation python client. Something like httpx

3 participants