diff --git a/.github/workflows/samples-python-connexion-generated-code.yaml b/.github/workflows/samples-python-connexion-generated-code.yaml new file mode 100644 index 000000000000..4a8df8ed4fac --- /dev/null +++ b/.github/workflows/samples-python-connexion-generated-code.yaml @@ -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 diff --git a/bin/utils/check-python-connexion-generated.py b/bin/utils/check-python-connexion-generated.py new file mode 100755 index 000000000000..d0389632a992 --- /dev/null +++ b/bin/utils/check-python-connexion-generated.py @@ -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()) diff --git a/bin/utils/test-python-connexion-generated.sh b/bin/utils/test-python-connexion-generated.sh new file mode 100755 index 000000000000..6401c0f27c9b --- /dev/null +++ b/bin/utils/test-python-connexion-generated.sh @@ -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