Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/workflows/samples-python-connexion-generated-code.yaml
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

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 pytest

Copy link
Copy Markdown
Contributor Author

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-srclayout and python-blueplanet), plus the check script and the workflow file itself, following samples-python-server.yaml / samples-python-petstore.yaml.

Copy link
Copy Markdown
Contributor Author

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 line run: /bin/bash ./bin/utils/test-python-connexion-generated.sh, the way misc-test.yaml calls the bin/utils scripts. The script does not run the generated tests. It checks two things:

  • the generated Python compiles without warnings;
  • the setup.py metadata matches the spec.

I did not use plain pytest. I tried it on the python-flask output with useConnexion3=true, and it misses both of those checks:

  • An invalid escape such as "\d" in a generated test only produces a DeprecationWarning.
  • No generated test reads setup.py.

Separately, two of the generated tests for connexion-json-header-test-values.yaml already fail on master with 400 '7' is not of type 'integer'. I tried only that one variant.

The workflow still differs from samples-python-server.yaml in two ways:

  • It builds the CLI on every run.
  • It triggers on the petstore samples of the same generators, and those do not contain the special characters it checks.

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:

  • 8 configs in bin/configs (2 specs × 4 generators), plus 226 generated files, for example under samples/server/others/.
  • The workflow would then run the check script in each sample directory, with no CLI build, and trigger on exactly what it checks.
  • Running pytest there as well would first need a fix for the two failing tests.

Which would you prefer?

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
217 changes: 217 additions & 0 deletions bin/utils/check-python-connexion-generated.py
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())
51 changes: 51 additions & 0 deletions bin/utils/test-python-connexion-generated.sh
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
Loading