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.33k stars 6.45k forks source link

[BUG] Generator python-flask generates parameters for header parameters and body request data #9266

Open alfechner opened 3 years ago

alfechner commented 3 years ago

Bug Report Checklist

Description

The OpenAPI spec bellow contains a header parameter (in:header) named dateTimeLastModified.

According to the docs, header parameters should be supported by the generator.

The generated controller looks like this:

def patientinfo_id_put(id, date_time_last_modified, patient_info_dto):  # noqa: E501
    date_time_last_modified = util.deserialize_datetime(date_time_last_modified)
    if connexion.request.is_json:
        patient_info_dto = PatientInfoDto.from_dict(connexion.request.get_json())  # noqa: E501
    return 'do some magic!'

With reference to the parameters of the generated handler function above:

1) The date_time_last_modified is passed as a parameter to the handler function. The flask documentation states that this is not supported and the header parameter should be accessed via the connexion.request.headers object. 2) The patient_info_dto obviously reflects the request body data. While date_time_last_modified is actually used in the generated code, patient_info_dto is ignored completely and overwritten by the correct way to access the body data.

Request error out with:

TypeError: api_v1_patientinfo_id_put() missing 2 required positional arguments: 'dateTimeLastModified' and 'patient_info_dto'

I guess the expected behaviour is to generate the handler function without header parameters and request body parameter and change the default code to accessing the header parameters directly from the connexion.request.headers object.

openapi-generator version
OpenAPI declaration file content or url
put:
  tags:
    - Patient Information
  summary: "..."
  description: "..."
  parameters:
    - name: id
      in: path
      description: The patient info identifier.
      required: true
      schema:
        type: string
    - name: dateTimeLastModified
      in: header
      description: The date and time when the patient information was last modified.
      required: true
      schema:
        type: string
        format: date-time
  requestBody:
    description: The patient information to update.
    content:
      application/json:
        schema:
          $ref: "#/components/schemas/PatientInfoDto"
    required: true
  responses:
    "200":
      description: successful update of the patient information.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/PatientInfoMetadataDto"
    "400":
      description: operation failed due to missing or invalid input parameters.
    "401":
      description: client cannot be authenticated.
    "403":
      description: client is not allowed to execute the operation.
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/StatusMessageDto"
    "404":
      description: patient information identifier not found.
    "409":
      description: operation cannot be processed, patient information is outdated.
Generation Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:v5.1.0 generate \
    -i /local/api_spec_v1.yml \
    -g python-flask \
    --minimal-update \
    -o /local/server
Steps to reproduce
vishnuagbly commented 9 months ago

Check this for a workaround, if anyone is looking for it:

https://github.com/spec-first/connexion/issues/788#issuecomment-1840442504