Repository navigation
ci(python): check generated Python Connexion server code in a GitHub workflow #25654
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
Open
khayashi4337
wants to merge
4
commits into
OpenAPITools:master
Choose a base branch
from
khayashi4337:ci/python-connexion-generated-code-workflow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
88e0000
ci(python): check generated Python Connexion server code in a GitHub …
khayashi4337 df29e14
ci(python): trigger the Connexion code check on sample paths
khayashi4337 8d55e90
ci(python): run the Connexion code check through a single script
khayashi4337 24db256
ci(python): also trigger the Connexion code check on its test specs
khayashi4337 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
.github/workflows/samples-python-connexion-generated-code.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| name: Python Connexion generated code | ||
|
|
||
| on: | ||
| push: | ||
| paths: | ||
| - samples/server/petstore/python-flask/** | ||
| - samples/server/petstore/python-flask-connexion3/** | ||
| - samples/server/petstore/python-aiohttp-srclayout/** | ||
| - samples/server/petstore/python-blueplanet/** | ||
| - bin/utils/check-python-connexion-generated.py | ||
| - bin/utils/test-python-connexion-generated.sh | ||
| - modules/openapi-generator/src/test/resources/3_0/python/connexion-json-header-test-values.yaml | ||
| - modules/openapi-generator/src/test/resources/3_0/python/info-title-special-chars.yaml | ||
| - .github/workflows/samples-python-connexion-generated-code.yaml | ||
| pull_request: | ||
| paths: | ||
| - samples/server/petstore/python-flask/** | ||
| - samples/server/petstore/python-flask-connexion3/** | ||
| - samples/server/petstore/python-aiohttp-srclayout/** | ||
| - samples/server/petstore/python-blueplanet/** | ||
| - bin/utils/check-python-connexion-generated.py | ||
| - bin/utils/test-python-connexion-generated.sh | ||
| - modules/openapi-generator/src/test/resources/3_0/python/connexion-json-header-test-values.yaml | ||
| - modules/openapi-generator/src/test/resources/3_0/python/info-title-special-chars.yaml | ||
| - .github/workflows/samples-python-connexion-generated-code.yaml | ||
| jobs: | ||
| build: | ||
| name: Check generated Python Connexion server code | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - name: Set up JDK 11 | ||
| uses: actions/setup-java@v6 | ||
| with: | ||
| java-version: 11 | ||
| distribution: 'temurin' | ||
| - name: Cache maven dependencies | ||
| uses: actions/cache@v6 | ||
| env: | ||
| cache-name: cache-maven-repository | ||
| with: | ||
| path: | | ||
| ~/.m2/repository | ||
| ~/.gradle | ||
| !~/.gradle/caches/*/plugin-resolution/ | ||
| !~/.m2/repository/org/openapitools/ | ||
| key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/pom.xml') }} | ||
| restore-keys: | | ||
| ${{ runner.os }}-build-${{ env.cache-name }}- | ||
| ${{ runner.os }}-build- | ||
| - uses: actions/setup-python@v7 | ||
| with: | ||
| python-version: "3.11" | ||
| - name: Install Python dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install pyyaml | ||
| - name: Build openapi-generator-cli | ||
| run: ./mvnw -B -ntp -DskipTests -pl modules/openapi-generator-cli -am package | ||
| - name: Test | ||
| run: /bin/bash ./bin/utils/test-python-connexion-generated.sh | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| #!/usr/bin/env python3 | ||
| """Verify generated Python Connexion server code. | ||
|
|
||
| Replaces the python3-dependent checks that used to live in | ||
| AbstractPythonConnexionServerCodegenTest (moved to a GitHub workflow so the | ||
| Java test suite does not require a python3 toolchain): | ||
|
|
||
| 1. ``--check-tests``: run ``py_compile`` on the generated | ||
| ``test_default_controller.py`` with warnings promoted to errors, the | ||
| same syntax check the removed Java test performed via | ||
| ``python3 -m py_compile``. | ||
| 2. ``--check-setup``: parse the generated ``setup.py`` with ``ast`` and | ||
| assert that the ``setup()`` keyword arguments ``description``, | ||
| ``author_email`` and ``long_description`` evaluate to the values the | ||
| generator derives from the input spec's ``info`` object (see | ||
| ``expected_setup_kwargs`` for how each expected value, including the | ||
| generator defaults used when the spec provides no value, is | ||
| determined). | ||
|
|
||
| Reports every mismatch it finds, then exits 1 if any check failed. | ||
| """ | ||
|
|
||
| import argparse | ||
| import ast | ||
| import py_compile | ||
| import sys | ||
| import warnings | ||
| from pathlib import Path | ||
|
|
||
| # Where each Connexion server variant puts the generated test module and | ||
| # setup.py, relative to the output directory (see generatedTestPath and the | ||
| # per-generator output layout in AbstractPythonConnexionServerCodegenTest). | ||
| TEST_FILE_CANDIDATES = ( | ||
| "openapi_server/test/test_default_controller.py", # python-flask | ||
| "tests/test_default_controller.py", # python-aiohttp | ||
| "app/openapi_server/test/test_default_controller.py", # python-blueplanet | ||
| ) | ||
| SETUP_PY_CANDIDATES = ( | ||
| "setup.py", # python-flask, python-aiohttp | ||
| "app/setup.py", # python-blueplanet | ||
| ) | ||
|
|
||
|
|
||
| def find_generated_file(generated_dir, candidates, name): | ||
| for rel in candidates: | ||
| path = generated_dir / rel | ||
| if path.is_file(): | ||
| return path | ||
| matches = sorted(p for p in generated_dir.rglob(name) if p.is_file()) | ||
| if len(matches) == 1: | ||
| return matches[0] | ||
| if matches: | ||
| raise FileNotFoundError( | ||
| f"multiple {name} files found under {generated_dir}, refusing " | ||
| f"to pick one: {', '.join(str(m) for m in matches)}" | ||
| ) | ||
| raise FileNotFoundError( | ||
| f"no {name} found under {generated_dir} " | ||
| f"(looked at {', '.join(candidates)} and recursively)" | ||
| ) | ||
|
|
||
|
|
||
| def check_tests_compile(test_file): | ||
| try: | ||
| with warnings.catch_warnings(): | ||
| warnings.simplefilter("error") | ||
| py_compile.compile(str(test_file), doraise=True) | ||
| except (py_compile.PyCompileError, Warning) as e: | ||
| print(f"FAIL: generated test {test_file} does not compile:\n{e}", file=sys.stderr) | ||
| return 1 | ||
| print(f"OK: {test_file} compiles") | ||
| return 0 | ||
|
|
||
|
|
||
| def load_spec_info(spec_path): | ||
| try: | ||
| import yaml | ||
| except ImportError: | ||
| sys.exit("PyYAML is required: pip install pyyaml") | ||
| try: | ||
| doc = yaml.safe_load(spec_path.read_text(encoding="utf-8")) | ||
| except OSError as e: | ||
| sys.exit(f"cannot read spec file {spec_path}: {e}") | ||
| except yaml.YAMLError as e: | ||
| sys.exit(f"cannot parse spec file {spec_path}: {e}") | ||
| if not isinstance(doc, dict) or not isinstance(doc.get("info"), dict): | ||
| sys.exit(f"{spec_path}: expected an OpenAPI document with an 'info' object") | ||
| return doc["info"] | ||
|
|
||
|
|
||
| # The boilerplate sentence AbstractPythonConnexionServerCodegen emits for | ||
| # long_description when the spec's info.description is missing or empty | ||
| # (mirroring the default DefaultGenerator assigns to the unescaped | ||
| # description), wrapped by setup.mustache's triple-quoted block. | ||
| DEFAULT_LONG_DESCRIPTION = ( | ||
| "No description provided (generated by Openapi Generator " | ||
| "https://github.com/openapitools/openapi-generator)" | ||
| ) | ||
|
|
||
|
|
||
| def expected_setup_kwargs(info): | ||
| """The setup() kwarg values the generator emits for a spec's info. | ||
|
|
||
| Mirrors what AbstractPythonConnexionServerCodegen.preprocessOpenAPI and | ||
| setup.mustache produce, so all three kwargs are always compared: | ||
|
|
||
| - ``description`` is ``info.title`` (``""`` when the spec has none). | ||
| - ``author_email`` is ``info.contact.email`` (``""`` when the spec has | ||
| none; a different value would only appear if an ``infoEmail`` | ||
| generator option were passed, which this workflow does not do). | ||
| - ``long_description`` is ``info.description`` inside the | ||
| `` ...`` triple-quoted block of setup.mustache | ||
| (``" " + description + "\\n "``), or ``DEFAULT_LONG_DESCRIPTION`` | ||
| when the spec's description is missing or empty. | ||
| """ | ||
| contact = info.get("contact") or {} | ||
| if not isinstance(contact, dict): | ||
| contact = {} | ||
| title = info.get("title") | ||
| email = contact.get("email") | ||
| description = info.get("description") | ||
| return { | ||
| "description": str(title) if title is not None else "", | ||
| "author_email": str(email) if email is not None else "", | ||
| "long_description": " " | ||
| + (str(description) if description not in (None, "") else DEFAULT_LONG_DESCRIPTION) | ||
| + "\n ", | ||
| } | ||
|
|
||
|
|
||
| def setup_call_kwargs(setup_py): | ||
| tree = ast.parse(setup_py.read_text(encoding="utf-8"), filename=str(setup_py)) | ||
| call = next( | ||
| ( | ||
| n | ||
| for n in ast.walk(tree) | ||
| if isinstance(n, ast.Call) and getattr(n.func, "id", None) == "setup" | ||
| ), | ||
| None, | ||
| ) | ||
| if call is None: | ||
| raise ValueError(f"{setup_py}: no setup() call found") | ||
| return { | ||
| k.arg: ast.literal_eval(k.value) | ||
| for k in call.keywords | ||
| if isinstance(k.value, ast.Constant) | ||
| } | ||
|
|
||
|
|
||
| def check_setup_kwargs(setup_py, spec_path): | ||
| expected = expected_setup_kwargs(load_spec_info(spec_path)) | ||
| try: | ||
| kwargs = setup_call_kwargs(setup_py) | ||
| except (SyntaxError, ValueError) as e: | ||
| print(f"FAIL: could not evaluate setup() kwargs in {setup_py}: {e}", file=sys.stderr) | ||
| return 1 | ||
| failures = 0 | ||
| for key, want in expected.items(): | ||
| got = kwargs.get(key) | ||
| if got != want: | ||
| failures += 1 | ||
| print( | ||
| f"FAIL: {setup_py} setup() kwarg {key!r} mismatch:\n" | ||
| f" got {got!r}\n" | ||
| f" want {want!r}", | ||
| file=sys.stderr, | ||
| ) | ||
| if failures: | ||
| return 1 | ||
| print(f"OK: {setup_py} setup() kwargs match {spec_path}") | ||
| return 0 | ||
|
|
||
|
|
||
| def main(argv=None): | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "generated_dir", | ||
| type=Path, | ||
| help="output directory of a generated Connexion server variant", | ||
| ) | ||
| parser.add_argument( | ||
| "--check-tests", | ||
| action="store_true", | ||
| help="py_compile the generated test_default_controller.py", | ||
| ) | ||
| parser.add_argument( | ||
| "--check-setup", | ||
| metavar="SPEC", | ||
| type=Path, | ||
| help="verify setup.py kwargs against the values the generator " | ||
| "derives from the info object of SPEC", | ||
| ) | ||
| args = parser.parse_args(argv) | ||
| if not (args.check_tests or args.check_setup): | ||
| parser.error("nothing to do: pass --check-tests and/or --check-setup SPEC") | ||
|
|
||
| rc = 0 | ||
| try: | ||
| if args.check_tests: | ||
| rc |= check_tests_compile( | ||
| find_generated_file( | ||
| args.generated_dir, TEST_FILE_CANDIDATES, "test_default_controller.py" | ||
| ) | ||
| ) | ||
| if args.check_setup: | ||
| rc |= check_setup_kwargs( | ||
| find_generated_file(args.generated_dir, SETUP_PY_CANDIDATES, "setup.py"), | ||
| args.check_setup, | ||
| ) | ||
| except FileNotFoundError as e: | ||
| print(f"ERROR: {e}", file=sys.stderr) | ||
| return 1 | ||
| return rc | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #!/bin/bash | ||
| # | ||
| # Generate the 2 test specs with the 4 Connexion-related generators | ||
| # (python-flask, python-flask with useConnexion3, python-aiohttp, | ||
| # python-blueplanet) and check each output with | ||
| # check-python-connexion-generated.py. | ||
| # | ||
| # Prerequisites: | ||
| # - modules/openapi-generator-cli/target/openapi-generator-cli.jar | ||
| # must already be built. | ||
| # - java and python3 with PyYAML (python3 -m pip install pyyaml). | ||
| # Usage: test-python-connexion-generated.sh [output_dir] (default: mktemp -d) | ||
| # A relative output_dir is resolved against the repository root, | ||
| # because this script changes to the repository root first. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| cd "$(dirname "$0")/../.." | ||
|
|
||
| executable="modules/openapi-generator-cli/target/openapi-generator-cli.jar" | ||
| if [ ! -f "$executable" ]; then | ||
| echo "ERROR: $executable not found. Build it first with:" >&2 | ||
| echo " ./mvnw -B -ntp -DskipTests -pl modules/openapi-generator-cli -am package" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| out_dir="${1:-$(mktemp -d)}" | ||
| echo "Output directory: $out_dir" | ||
| specs=( | ||
| modules/openapi-generator/src/test/resources/3_0/python/connexion-json-header-test-values.yaml | ||
| modules/openapi-generator/src/test/resources/3_0/python/info-title-special-chars.yaml | ||
| ) | ||
| variants=( | ||
| "python-flask" | ||
| "python-flask useConnexion3=true" | ||
| "python-aiohttp" | ||
| "python-blueplanet" | ||
| ) | ||
| for spec in "${specs[@]}"; do | ||
| for variant in "${variants[@]}"; do | ||
| read -r generator option <<< "$variant" | ||
| dest="$out_dir/$(basename "$spec" .yaml)/${generator}${option:++connexion3}" | ||
| args=(generate -g "$generator" -i "$spec" -o "$dest") | ||
| if [ -n "${option:-}" ]; then | ||
| args+=(--additional-properties "$option") | ||
| fi | ||
| java -jar "$executable" "${args[@]}" | ||
| python3 bin/utils/check-python-connexion-generated.py "$dest" \ | ||
| --check-tests --check-setup "$spec" | ||
| done | ||
| done |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the PR
usually we put the sample paths for triggering the workflow, e.g.
https://github.com/OpenAPITools/openapi-generator/blob/master/.github/workflows/samples-python-server.yaml#L6
https://github.com/OpenAPITools/openapi-generator/blob/master/.github/workflows/samples-python-petstore.yaml#L6-L13
can you do the same please in this new workflow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and to run the tests, can we simply do something similar to https://github.com/OpenAPITools/openapi-generator/blob/master/.github/workflows/samples-python-server.yaml#L27 by running a single script/command? maybe as simple as just running
pytestThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, done in df29e14: the workflow now triggers on the sample paths of the four generators it checks (
samples/server/petstore/python-flask,python-flask-connexion3,python-aiohttp-srclayoutandpython-blueplanet), plus the check script and the workflow file itself, followingsamples-python-server.yaml/samples-python-petstore.yaml.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, and sorry I missed this one: it crossed with my reply above. Done in 8d55e90 and 24db256. The generate-and-check loop is now
bin/utils/test-python-connexion-generated.sh, and the job's test step is the single linerun: /bin/bash ./bin/utils/test-python-connexion-generated.sh, the waymisc-test.yamlcalls thebin/utilsscripts. The script does not run the generated tests. It checks two things:setup.pymetadata matches the spec.I did not use plain
pytest. I tried it on thepython-flaskoutput withuseConnexion3=true, and it misses both of those checks:"\d"in a generated test only produces aDeprecationWarning.setup.py.Separately, two of the generated tests for
connexion-json-header-test-values.yamlalready fail on master with400 '7' is not of type 'integer'. I tried only that one variant.The workflow still differs from
samples-python-server.yamlin two ways:To narrow the second gap, 24db256 also triggers on the two test specs. A template change that only affects those characters can still leave the petstore samples unchanged, though, so my earlier note to cubic ("so the check still runs") was too strong.
The alternative is committed samples:
bin/configs(2 specs × 4 generators), plus 226 generated files, for example undersamples/server/others/.pytestthere as well would first need a fix for the two failing tests.Which would you prefer?