OpenAPITools / OpenAPITools/openapi-generator

[python-flask] controller_test generates syntactically invalid python when given an example of type (string) to URL params

Open
#5,819 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Server: Python
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When I supply a configuration in the style of:

"paths": {
    "/a/{artifact_id}/b" : {
      "post" : {
        "parameters" : [ {
          "description" : "xyz",
          "example" : "some string",
          "explode" : false,
          "in" : "path",
          "name" : "artifact_id",
          "required" : true,
          "schema" : {
            "type" : "string"
          },
          "style" : "simple"
        } ],
...

and run openapi-generator, $output/$package/test/test_a_controller.py is generated containing python that reads:

        response = self.client.open(
            '/api/v1/a/{artifact_id}/b'.format(artifact_id=some string),   # <-- my emphasis, bug
            method='POST',
            headers=headers,
            data=json.dumps(attestation),
            content_type='application/json')

the argument parameter artifact_id=some string) is invalid -- but not quoted as a string to python, and supplied very literally. Eg. if the example read:

   example: "url_encode: 0/0"

it would would render that as unquoted python.

openapi-generator version

4.2.3 (supplied via python/pip) -- I have seen this apparent bug in master

OpenAPI declaration file content or url

(I'll render a full test case if requested, I think it's apparent from the above and probably near existing fixtures)

Command line used for generation
openapi-generator generate \
   -c generator-config.yaml \
   -i openapi/openapi.yaml \
   -g python-flask -o serverstub
# generator-config.yaml
packageName: xxxx
packageVersion: 1.0.0
serverPort: 8080
enablePostProcessFile: true
generateAliasAsModel: true
Steps to reproduce
Related issues/PRs

(not found)

Suggest a fix/enhancement

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python-flask/controller_test.mustache#L42

I believe the issue is in this line of code, with a lack of quoting -- but I have not traced into how th example object is ... typed in or by mustache yet.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/python-flask/controller_test.mustache around line 42, then run the supplied openapi-generator command with the configuration shown. Trace how the URL parameter example reaches the template. Done means the generated controller test remains valid Python when a string example contains spaces or other literal characters.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
backend, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.