Repository navigation
fix(python-flask, python-blueplanet): emitted tests are invalid Python when an operation has no cookie params - #25563
Open
khayashi4337 wants to merge 2 commits into
Conversation
…n when an operation has no cookie params
The {{#cookieParams}} block opens mid-line after 8 spaces of indentation,
so when the section is empty it leaves those spaces behind while the
standalone close tag swallows the line break. The dangling indent merges
into the next statement, producing ` response = ...` (an
unexpected-indent IndentationError) for any operation without cookie
parameters. Move the section tags onto their own lines so an empty
section emits nothing; operations with cookie params render the same
block, with continuation items indented under the headers assignment.
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ndentation without python3
Adds a Java-only regression check for the cookie-param indentation fix:
for an operation without cookie params and one with only cookie params,
the generated test_default_controller.py is read back and every affected
statement is pinned to its expected column — `response = ...`/`assert200`
at 8, plus `headers['Cookie'] = ...` (flask) or `headers = []` /
`headers += [('Cookie', ...)]` (blueplanet) for the cookie operation.
Reverting the template fix makes the check fail (response line rendered
at column 16), so it catches the IndentationError shape without invoking
python3; python-aiohttp is out of scope as its cookie block already opens
the section on its own line.
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
No issues found across 3 files
Tip: Get a Slack DM when cubic finishes reviewing your PRs. Connect Slack
View guided diff | Re-trigger cubic
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Generated
test_*.pyfiles forpython-flask(includinguseConnexion3=true) andpython-blueplanetare invalid Python —python3 -m py_compilefails withIndentationError: unexpected indent— for every operation that does not declare cookie parameters.Reproducer
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \ -c bin/configs/python-flask.yaml -o /tmp/oag-flask python3 -m py_compile /tmp/oag-flask/openapi_server/test/test_pet_controller.py # Sorry: IndentationError: unexpected indent (test_pet_controller.py, line 20)Broken output — the 8 leaked spaces (see Cause) attach to the next statement the template emits. For an operation without
in: cookieparameters that is normally theresponse = ...line (for an operation with form parameters it is thedata = {...line), which ends up indented by 16 columns instead of 8:Measured on
masterwith the official sample configs (bin/configs/python-flask.yaml,python-flask-connexion3.yaml,python-blueplanet.yaml), generating into a fresh output directory: the controller test file of every API failspy_compile— 4 files for python-flask, 4 foruseConnexion3=true, 3 for python-blueplanet (atest_*.pyglob over the python-flask / connexion3 output matches 7 files; 4 are the per-API controller tests, the other 3 are generated model modules for the spec'sTest*schemas, which are unaffected). The checked-in samples do not show this because generated test files are never overwritten on regeneration once they exist (DefaultGeneratorskips existing model/API test files), and the sample test files predate the{{#cookieParams}}statement block (#25487) — which is also why the Samples up-to-date CI job cannot surface the bug.Cause
In both
controller_test.mustachetemplates the{{#cookieParams}}section opened mid-line, after 8 literal spaces:A section tag that is not standalone leaves the text before it — here the 8 spaces — in the output even when the section renders nothing. With an empty section the open line emits just those spaces, and the standalone
{{/cookieParams}}swallows its own line break, so the dangling indent merges into the next emitted statement:response = self.client.open(→IndentationErroratpy_compile/import/pytest collection.The fix moves the section tags onto their own lines so an empty section emits nothing. Operations with cookie params render the same statement as before; only for an operation with two or more cookie parameters do the continuation lines of the
headers['Cookie'] = '; '.join([...expression move from column 0 to column 8 (both are valid Python).python-aiohttpis not affected: its cookie block already opens{{#cookieParams}}on its own line. The remaining{{#cookieParams}}{{#-first}}usages (aiohttp'scookies=cookies,request argument, blueplanet'sheaders=headersopen() argument, and the Javajersey2/jersey3cookie-arg lists) are inline inside call expressions — they only emit a continuation argument when the section renders, so they cannot leak indentation into the next statement.Tests added
AbstractPythonConnexionServerCodegenTest.testGeneratedTestsStatementIndentWithAndWithoutCookieParams— Java-only, no python3 toolchain needed. It generates both shapes —/ping(no parameters at all) andget_cookie_only(cookie params only) — for python-flask, python-flaskuseConnexion3=true, and python-blueplanet, reads the generatedtest_default_controller.py, and pinsresponse = self.client.open(andself.assert200(to column 8, plus the Cookie statement for the cookie operation:headers['Cookie'] = '; '.join([...(flask),headers = []+headers += [('Cookie', ...(blueplanet). Reverting the template fix makes the test fail withexpected: 8 but was: 16; the assertions are fail-fast, so the failure is reported for the first variant checked.Verification results
./mvnw -pl modules/openapi-generator -am -Dtest=AbstractPythonConnexionServerCodegenTest,PythonFlaskConnexionServerCodegenTest testrun withpython3absent fromPATH: 25 + 22 = 47 tests, 0 failures, 0 skipped.git apply -R):expected: 8 but was: 16(reported for the first variant checked, since the assertions are fail-fast) — i.e. it reproduces the exact IndentationError shape.python3 -m py_compile: 7/7 python-flask, 7/7 connexion3, 3/3 blueplanet (was 4/7, 4/7, 3/3 broken).bin/generate-samples.shfor the three configs leavesgit statusunchanged (existing generated test files are skipped byDefaultGenerator, and no other file changes).Note on conflicts
git merge-treereports a content conflict with open PR #25457 (quoted-namehandling) in the python-blueplanetcontroller_test.mustache— both changes touch the same cookie-param lines and whichever merges second will need a small manual resolution (keep thequoted-namerendering inside the now-standalone{{#cookieParams}}section). The python-flask template and the Java test file merge cleanly.cc @cbornet @tomplus @krjakbrjak @fa0311 (Python technical committee)
PR checklist
bin/generate-samples.shfor the three affected configs —python-flask,python-flask-connexion3,python-blueplanet— which leavesgit statusunchanged.)(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.
🤖 Generated with Claude Code
Summary by cubic
Fixes generated
test_*.pyfiles forpython-flask(includinguseConnexion3=true) andpython-blueplanetbeing invalid Python for operations without cookie parameters. A mid-line{{#cookieParams}}section tag left its 8 leading spaces behind when empty, merging into the next statement and producing anIndentationErroratpy_compile/import/pytest collection. The section tags now sit on their own lines so an empty section emits nothing; operations with cookie params render the same statements as before.python-aiohttpis unaffected since its cookie block already opens the section on its own line.Written for commit 1348d47. Summary will update on new commits.