OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.44k stars 6.49k forks source link

[BUG][python-flask] binary response type is generated with rtype `file` #4372

Open frjonsen opened 4 years ago

frjonsen commented 4 years ago
Description

When setting the response type as type: string and format: binary, the return type is generated as file, even though the documentation states that file is no longer a data type in OpenAPI 3.0, as mentioned here: https://swagger.io/docs/specification/data-models/data-types/#file

format: byte is correctly generated as bytearray.

This becomes a problem for us as we use custom templates to set the return type of functions ({{#returnType}} -> {{.}}{{/returnType}}), which will result in file, which is not valid in python, meaning the resulting generated code does not run.

openapi-generator version

Tested with 4.1.3, as well as these snapshots: openapi-generator-cli-4.2.1-20191104.150105-22 openapi-generator-cli-5.0.0-20191024.154544-16

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Python binary response
  version: 1.0.0
paths:
  "/foo":
    post:
      responses:
        "200":
          description: "Success"
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
Command line used for generation

java -jar openapi-generator-cli-4.1.3.jar generate -g python-flask -i openapi.yml

Steps to reproduce
  1. Copy yaml above to openapi.yml
  2. Run java -jar openapi-generator-cli-4.1.3.jar generate -g python-flask -i openapi.yml
  3. openapi_server/controllers/default_controller.py now contains a single function, with :rtype: file
Related issues/PRs

None which I could find

Suggest a fix/enhancement

I am unfamiliar with the code base, but what I could find was that here the type file is declared as a primitive of Python, which I don't think is the case. It also maps file to file. Presumably this should map to something like str or bytearray.

auto-labeler[bot] commented 4 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

tgpfeiffer commented 3 years ago

I think this issue still exists. The comments in https://github.com/OpenAPITools/openapi-generator/blob/7bb7c72cc53c8063c781ea0b38691cca4955a29f/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java#L94-L98 mention a "workaround", can anyone shed some light on what is worked around here?

alfechner commented 2 years ago

I use Docker image openapitools/openapi-generator-cli:v5.4.0. For me both binary as well as byte result in type file. Support for Python2 is disabled in the generator (supportPython2: false).

ajrice6713 commented 2 years ago

It looks like byte results in type string (with 5.4.0) - i dont believe this is correct either - a byte definition in the openapi spec should result in the generator expecting type bytes

edit: my findings are in python not python-flask

spacether commented 2 years ago

Per the spec https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md#data-types

type: string
format: byte

means base64 encoded characters so while one could convert the value to binary, the value starts out as a base64 encoded string, so I think that storing a string is appropriate for that base64 encoded string value One can also have a schema like:

someString:
  allOf:
    - type: string
    - type: string
      format: byte

So it helps to have the data type be consistent for the two allOf schemas inside someString.allOf