From 3094ccb1b9ea6a2931c23082ff6b6872bc48ec97 Mon Sep 17 00:00:00 2001 From: khayashi4337 Date: Thu, 5 Nov 2026 12:14:09 +0900 Subject: [PATCH] [python] Make Connexion codegen unit tests independent of a python3 toolchain The unit tests must not require a python3 toolchain (#25556), so the python3-based checks are removed or replaced with Java assertions: - testJsonContentHeaderParamsGeneratedTestsCompile is removed; it only ran `python3 -m py_compile` on the generated test file, which needs the optional toolchain the unit tests must not depend on. - testInfoTitleEmbeddedAsPythonStringLiteral is re-enabled with Java-only assertions, tightened to whole-line assertFileContainsTrimmedLine checks for the setup.py kwargs literals in all four variants. The python3 helpers that evaluated setup() kwargs via ast and compiled generated tests via py_compile (assertSetupPyValuesMatchSpec, assertPythonSyntaxValid) are removed with it. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-Authored-By: Claude Sonnet 5.5 --- ...tractPythonConnexionServerCodegenTest.java | 134 +++++------------- 1 file changed, 38 insertions(+), 96 deletions(-) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/AbstractPythonConnexionServerCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/AbstractPythonConnexionServerCodegenTest.java index 3de1432b1059..0e6b2ec1e71f 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/AbstractPythonConnexionServerCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/AbstractPythonConnexionServerCodegenTest.java @@ -2,7 +2,6 @@ import com.google.common.collect.ImmutableMap; import io.swagger.parser.OpenAPIParser; -import io.swagger.v3.core.util.Json; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.parameters.Parameter; import io.swagger.v3.oas.models.security.SecurityScheme; @@ -15,13 +14,11 @@ import org.openapitools.codegen.config.CodegenConfigurator; import org.openapitools.codegen.languages.AbstractPythonConnexionServerCodegen; import org.openapitools.codegen.languages.PythonAiohttpConnexionServerCodegen; -import org.testng.SkipException; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -32,7 +29,6 @@ import java.util.Map; import java.util.Set; import java.util.Objects; -import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -1004,17 +1000,6 @@ public void testJsonContentHeaderParamsUseJsonLiteralInGeneratedTests() throws I } } - @Test(description = "generated tests for content (application/json) header params are valid Python syntax; kept in a separate test so a missing python3 toolchain skips only this check", enabled = false) - public void testJsonContentHeaderParamsGeneratedTestsCompile() throws IOException, InterruptedException { - String specPath = "src/test/resources/3_0/python/connexion-json-header-test-values.yaml"; - - for (Map.Entry variant : generateConnexionVariants(specPath).entrySet()) { - Path testFile = generatedTestPath(variant.getKey(), variant.getValue()); - assertFileExists(testFile); - assertPythonSyntaxValid(testFile); - } - } - @Test(description = "statements in generated tests keep their 8-column indent whether or not the operation declares cookie params (an empty {{#cookieParams}} section must not leak the open line's indent into the next statement)") public void testGeneratedTestsStatementIndentWithAndWithoutCookieParams() throws IOException { // /ping declares no parameters at all: the empty cookieParams section @@ -1166,14 +1151,10 @@ public void testAiohttpRequirementsIncludePythonDateutil() throws IOException { assertFileContains(aiohttpSetupPy, "python_dateutil>=2.6.0"); } - @Test(description = "info.title is embedded in generated Python source as a Python string literal, not HTML-escaped", enabled = false) - public void testInfoTitleEmbeddedAsPythonStringLiteral() throws IOException, InterruptedException { + @Test(description = "info.title is embedded in generated Python source as a Python string literal, not HTML-escaped") + public void testInfoTitleEmbeddedAsPythonStringLiteral() throws IOException { // spec title: Tom & Jerry 'quoted' "dquoted" \path String specPath = "src/test/resources/3_0/python/info-title-special-chars.yaml"; - OpenAPI spec = new OpenAPIParser().readLocation(specPath, null, new ParseOptions()).getOpenAPI(); - String expectedTitle = spec.getInfo().getTitle(); - String expectedDescription = spec.getInfo().getDescription(); - String expectedEmail = spec.getInfo().getContact().getEmail(); String singleQuotedTitle = "'Tom & Jerry \\'quoted\\' \"dquoted\" \\\\path'"; String doubleQuotedTitle = "\"Tom & Jerry 'quoted' \\\"dquoted\\\" \\\\path\""; String[] htmlEscaped = {"&", "<", ">", "'", """}; @@ -1185,23 +1166,38 @@ public void testInfoTitleEmbeddedAsPythonStringLiteral() throws IOException, Int assertFileExists(flaskSetup); assertFileContains(flaskMain, "arguments={'title': " + singleQuotedTitle + "},"); assertFileNotContains(flaskMain, htmlEscaped); + // the setup() kwargs must be complete Python literals evaluating to the + // raw spec values — pinned as whole lines so a truncated literal cannot + // pass; evaluating the setup() kwarg values is out of scope for this + // unit test + assertFileContainsTrimmedLine(flaskSetup, "description=" + doubleQuotedTitle + ","); + assertFileContainsTrimmedLine(flaskSetup, "keywords=[\"OpenAPI\", " + doubleQuotedTitle + "],"); + assertFileContainsTrimmedLine(flaskSetup, "author_email=\"\\\"weird\\\" \","); + assertFileContainsTrimmedLine(flaskSetup, "long_description=\"\"\"\\"); + assertFileContainsTrimmedLine(flaskSetup, "\"\"\""); assertFileContains(flaskSetup, - "description=" + doubleQuotedTitle + ",", - "keywords=[\"OpenAPI\", " + doubleQuotedTitle + "]", - "author_email=\"\\\"weird\\\" \"", "He said \\\"\\\"\\\"triple quotes\\\"\\\"\\\" end.", "Windows path C:\\\\tmp\\\\dir", "null\\x00byte\\x01end"); assertFileNotContains(flaskSetup, htmlEscaped); - assertSetupPyValuesMatchSpec(flaskSetup, expectedTitle, expectedDescription, expectedEmail); Path flask3Dir = generateOutputDir("python-flask", specPath, ImmutableMap.of("useConnexion3", "true")); Path flask3Main = flask3Dir.resolve(Paths.get("openapi_server", "__main__.py")); Path flask3Setup = flask3Dir.resolve("setup.py"); assertFileExists(flask3Main); + assertFileExists(flask3Setup); assertFileContains(flask3Main, "arguments={'title': " + singleQuotedTitle + "},"); assertFileNotContains(flask3Main, htmlEscaped); - assertSetupPyValuesMatchSpec(flask3Setup, expectedTitle, expectedDescription, expectedEmail); + assertFileContainsTrimmedLine(flask3Setup, "description=" + doubleQuotedTitle + ","); + assertFileContainsTrimmedLine(flask3Setup, "keywords=[\"OpenAPI\", " + doubleQuotedTitle + "],"); + assertFileContainsTrimmedLine(flask3Setup, "author_email=\"\\\"weird\\\" \","); + assertFileContainsTrimmedLine(flask3Setup, "long_description=\"\"\"\\"); + assertFileContainsTrimmedLine(flask3Setup, "\"\"\""); + assertFileContains(flask3Setup, + "He said \\\"\\\"\\\"triple quotes\\\"\\\"\\\" end.", + "Windows path C:\\\\tmp\\\\dir", + "null\\x00byte\\x01end"); + assertFileNotContains(flask3Setup, htmlEscaped); Path aiohttpDir = generateOutputDir("python-aiohttp", specPath, Collections.emptyMap()); Path aiohttpMain = aiohttpDir.resolve(Paths.get("openapi_server", "__init__.py")); @@ -1210,12 +1206,16 @@ public void testInfoTitleEmbeddedAsPythonStringLiteral() throws IOException, Int assertFileExists(aiohttpSetup); assertFileContains(aiohttpMain, "arguments={'title': " + singleQuotedTitle + "},"); assertFileNotContains(aiohttpMain, htmlEscaped); + assertFileContainsTrimmedLine(aiohttpSetup, "description=" + doubleQuotedTitle + ","); + assertFileContainsTrimmedLine(aiohttpSetup, "keywords=[\"OpenAPI\", " + doubleQuotedTitle + "],"); + assertFileContainsTrimmedLine(aiohttpSetup, "author_email=\"\\\"weird\\\" \","); + assertFileContainsTrimmedLine(aiohttpSetup, "long_description=\"\"\"\\"); + assertFileContainsTrimmedLine(aiohttpSetup, "\"\"\""); assertFileContains(aiohttpSetup, - "description=" + doubleQuotedTitle + ",", - "keywords=[\"OpenAPI\", " + doubleQuotedTitle + "]", - "author_email=\"\\\"weird\\\" \""); + "He said \\\"\\\"\\\"triple quotes\\\"\\\"\\\" end.", + "Windows path C:\\\\tmp\\\\dir", + "null\\x00byte\\x01end"); assertFileNotContains(aiohttpSetup, htmlEscaped); - assertSetupPyValuesMatchSpec(aiohttpSetup, expectedTitle, expectedDescription, expectedEmail); Path blueplanetDir = generateOutputDir("python-blueplanet", specPath, Collections.emptyMap()); Path blueplanetMain = blueplanetDir.resolve(Paths.get("app", "openapi_server", "__main__.py")); @@ -1224,12 +1224,16 @@ public void testInfoTitleEmbeddedAsPythonStringLiteral() throws IOException, Int assertFileExists(blueplanetSetup); assertFileContains(blueplanetMain, "arguments={'title': " + singleQuotedTitle + "})"); assertFileNotContains(blueplanetMain, htmlEscaped); + assertFileContainsTrimmedLine(blueplanetSetup, "description=" + doubleQuotedTitle + ","); + assertFileContainsTrimmedLine(blueplanetSetup, "keywords=[\"Swagger\", " + doubleQuotedTitle + "],"); + assertFileContainsTrimmedLine(blueplanetSetup, "author_email=\"\\\"weird\\\" \","); + assertFileContainsTrimmedLine(blueplanetSetup, "long_description=\"\"\"\\"); + assertFileContainsTrimmedLine(blueplanetSetup, "\"\"\""); assertFileContains(blueplanetSetup, - "description=" + doubleQuotedTitle + ",", - "keywords=[\"Swagger\", " + doubleQuotedTitle + "]", - "author_email=\"\\\"weird\\\" \""); + "He said \\\"\\\"\\\"triple quotes\\\"\\\"\\\" end.", + "Windows path C:\\\\tmp\\\\dir", + "null\\x00byte\\x01end"); assertFileNotContains(blueplanetSetup, htmlEscaped); - assertSetupPyValuesMatchSpec(blueplanetSetup, expectedTitle, expectedDescription, expectedEmail); } @Test(description = "a caller-provided infoEmail is rendered as the setup.py author_email literal when the spec declares no contact email") @@ -1247,48 +1251,6 @@ public void testProvidedInfoEmailUsedWhenSpecHasNoContactEmail() throws IOExcept } } - /** - * Parses the generated setup.py with python's ast and asserts that the - * description/long_description/author_email kwargs evaluate to the raw - * spec values (the long_description template wraps the description in - * " " + ... + "\n " inside its """...""" literal). - */ - private static void assertSetupPyValuesMatchSpec(Path setupPy, String expectedTitle, - String expectedDescription, String expectedEmail) - throws IOException, InterruptedException { - Map expected = new LinkedHashMap<>(); - expected.put("description", expectedTitle); - expected.put("author_email", expectedEmail); - expected.put("long_description", " " + expectedDescription + "\n "); - File expectedJson = File.createTempFile("expected-setup-kwargs", ".json"); - expectedJson.deleteOnExit(); - Json.mapper().writeValue(expectedJson, expected); - - String script = "import ast, json, sys\n" - + "tree = ast.parse(open(sys.argv[1]).read())\n" - + "call = next(n for n in ast.walk(tree)\n" - + " if isinstance(n, ast.Call) and getattr(n.func, 'id', None) == 'setup')\n" - + "kwargs = {k.arg: ast.literal_eval(k.value) for k in call.keywords\n" - + " if isinstance(k.value, ast.Constant)}\n" - + "expected = json.load(open(sys.argv[2]))\n" - + "for key, want in expected.items():\n" - + " got = kwargs.get(key)\n" - + " assert got == want, '%s mismatch: got %r want %r' % (key, got, want)\n" - + "print('setup() kwargs match:', sys.argv[1])\n"; - Process process; - try { - process = new ProcessBuilder("python3", "-c", script, - setupPy.toAbsolutePath().toString(), expectedJson.getAbsolutePath()) - .redirectErrorStream(true).start(); - } catch (IOException e) { - throw new SkipException("python3 is not available to evaluate generated setup.py", e); - } - boolean finished = process.waitFor(30, TimeUnit.SECONDS); - assertThat(finished).as("python3 assertion timed out for " + setupPy).isTrue(); - String output = new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8); - assertThat(process.exitValue()).as(output).isEqualTo(0); - } - @Test(description = "python-blueplanet generated swagger.yaml is not empty and contains the OpenAPI spec") public void testBlueplanetSwaggerYamlNotEmpty() throws IOException { Path blueplanetDir = generateOutputDir("python-blueplanet", Collections.emptyMap()); @@ -1356,24 +1318,4 @@ private static void assertFileContainsTrimmedLine(Path file, String expected) th .as("generated file %s must contain the line: %s", file, expected) .contains(expected); } - - /** - * Runs {@code python3 -m py_compile} on the generated file. Skips if - * {@code python3} is not available (optional toolchain). - */ - private static void assertPythonSyntaxValid(Path pythonFile) throws IOException, InterruptedException { - ProcessBuilder pb = new ProcessBuilder("python3", "-m", "py_compile", pythonFile.toString()); - pb.redirectErrorStream(true); - final Process p; - try { - p = pb.start(); - } catch (IOException e) { - throw new SkipException("python3 not available on PATH, skipping syntax check: " + e.getMessage()); - } - assertThat(p.waitFor(30, TimeUnit.SECONDS)).as("python3 -m py_compile timed out").isTrue(); - String out = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8).trim(); - assertThat(p.exitValue()) - .as("python3 -m py_compile must accept generated test %s: %s", pythonFile, out) - .isEqualTo(0); - } }