swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.03k stars 6.03k forks source link

[python-flask] [connexion-flask] Parameter names can be mangled in server generation #2927

Open Spencatro opened 8 years ago

Spencatro commented 8 years ago
Description

When using swagger-codegen to generate a flask server, connexion and swagger-codegen do not agree on how to represent parameter names, which leads to connexion making requests to endpoints with variable names that do not match the output of swagger-codegen.

Swagger-codegen version

Using swagger-codegen built from source off of master at time of issue being opened (2e402da3efdc63c1fb6ae2238b171c7f488be80b is latest commit)

Swagger declaration file content or url

simpletest.yaml :


---
swagger: '2.0'
info:
  version: 0.0.0
  title: Simple API
paths:
  /:
    get:
      parameters:
        - name: myOkParameter
          required: false
          type: string
          in: formData
          description: |
            Connexion passes through myOkParameter correctly
        - name: my_broken_parameter
          required: false
          type: string
          in: formData
          description: |
            Connexion creates requests with parameter name my_broken_parameter, but swagger-codegen generates flask code with parameter name myBrokenParameter, resulting in 500 errors out of the box
      responses:
        200:
          description: OK

This command generates controllers/default_controller.py with the following contents:

def root_get(myOkParameter = None, myBrokenParameter = None) -> str:
    return 'do some magic!'

But it should generate these contents instead:

def root_get(myOkParameter = None, my_broken_parameter = None) -> str:
    return 'do some magic!'
Command line used for generation

swagger-codegen generate -i simpletest.yaml -l python-flask -o simple_out_before_fix

Steps to reproduce

1: Generate this server 2: Start the flask server 3: Attempt to make the get request 4: Flask 500 errors

Related issues

Appears to be related to https://github.com/swagger-api/swagger-codegen/issues/1938 , but I don't believe the fix implemented there goes far enough to fix the underlying issue (parameter names should not be modified at all, or rather, should be modified in the same way connexion expects them i.e. not at all).

Suggest a Fix

Suggested fix: changes in commit https://github.com/shawkinsl/swagger-codegen/commit/756f0ed64e60a5a3fe2c70ebee4a34d7a7b68abe . If changes are acceptable, I will open a PR.

It's worth noting that there are likely other edge cases that this fix does not resolve, but this is what I am able and willing to implement at the moment.

wing328 commented 8 years ago

@shawkinsl thanks for reporting the issue. Please open a PR and we'll review the change.

wing328 commented 8 years ago

@shawkinsl may I know if you need any help submitting a PR?

wing328 commented 8 years ago

@shawkinsl thanks for the PR and details in reporting the issue.

I think your fix will partially resolve the issue. For parameter name with special characters (e.g. created-at), I believe it will cause issue as the parameter name is not sanitized to conform to Python variable naming rule.

I believe we need a mapping similar to what we've done in https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/python/petstore_api/apis/pet_api.py#L754