OpenAPI generates code that doesn't pass its own tests

522 Views Asked by At

I am trying to use the OpenAPI generator to build a testing mock that simulates an existing service, so I don't have a lot of latitude in terms of changing the schema.

I've boiled down the schema to the following which highlights my issue:

openapi: 3.0.1
info:
  title: Example
  version: 1.0.0
servers:
- url: /
paths:
  /example:
    delete:
      description: Delete a thing
      operationId: delete_thing
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/deleteThing'
        required: true
      responses:
        "200":
          description: OK
        "400":
          description: Bad request
        "500":
          description: Unexpected error
      tags:
      - example
      x-amazon-apigateway-request-validator: basic
      x-amazon-apigateway-integration:
        type: aws_proxy
        httpMethod: POST
        uri: ${delete_publisher_arn}
      x-codegen-request-body-name: body
      x-openapi-router-controller: openapi_server.controllers.example_controller
components:
  schemas:
    deleteThing:
      properties:
        id:
          allOf:
          - minLength: 1
          - pattern: ^[^<>]*$
          - pattern: \S
          type: string
      required:
      - id
      type: object
x-amazon-apigateway-request-validators:
  basic:
    validateRequestBody: true
    validateRequestParameters: true

and I use the following command to generate the scaffold code for python-flask:

docker run --rm -v $PWD/openapi-example:/local openapitools/openapi-generator-cli generate -i /local/openapi_server/openapi/openapi.yaml -g python-flask -o /local/.

The issue that I am seeing is that the generated code for the example_controller expects a positional parameter named body which it never receives:

import connexion
import six

from openapi_server.models.delete_thing import DeleteThing  # noqa: E501
from openapi_server import util


def delete_thing(body):  # noqa: E501
    """delete_thing

    Delete a thing # noqa: E501

    :param body: 
    :type body: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        body = DeleteThing.from_dict(connexion.request.get_json())  # noqa: E501
    return 'do some magic!'

When I start the server and interact with the Swagger UI, I get the following exception:

[2021-04-20 15:03:30,107] ERROR in app: Exception on /example [DELETE]
Traceback (most recent call last):
  File "/home/peter/workspace/openapi-example/venv/lib64/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/peter/workspace/openapi-example/venv/lib64/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/peter/workspace/openapi-example/venv/lib64/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/peter/workspace/openapi-example/venv/lib64/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/peter/workspace/openapi-example/venv/lib64/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/peter/workspace/openapi-example/venv/lib64/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/peter/workspace/openapi-example/venv/lib64/python3.6/site-packages/connexion/decorators/decorator.py", line 48, in wrapper
    response = function(request)
  File "/home/peter/workspace/openapi-example/venv/lib64/python3.6/site-packages/connexion/decorators/uri_parsing.py", line 144, in wrapper
    response = function(request)
  File "/home/peter/workspace/openapi-example/venv/lib64/python3.6/site-packages/connexion/decorators/validation.py", line 184, in wrapper
    response = function(request)
  File "/home/peter/workspace/openapi-example/venv/lib64/python3.6/site-packages/connexion/decorators/parameter.py", line 121, in wrapper
    return function(**kwargs)
TypeError: delete_thing() missing 1 required positional argument: 'body'
127.0.0.1 - - [20/Apr/2021 15:03:30] "DELETE /example HTTP/1.1" 500 -
0

There are 0 best solutions below