OpenAPITools / OpenAPITools/openapi-generator

[BUG] [python-flask] server doesn't work, crashes on tests

Open
#7,076 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)

Description

I generated code from a spec, and tried to run the tests.

The tests fail out of the box because they're missing required fields. (Which is a separate issue.)
When I add that, I get another test failure, because nothing is passed to the controller function.

openapi-generator version

$ docker run --rm     -v $PWD:/local     openapitools/openapi-generator-cli --version                        
openapi-generator-cli 5.0.0-SNAPSHOT
  commit : 59029d2
  built  : 2020-07-29T03:01:05Z
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/

OpenAPI declaration file content or url

openapi: 3.0.1
info:
  title: MWE
  description: example of bug in generator
  version: 1.0.0
servers:
- url: http://example.com/api
paths:
  /go:
    post:
      summary: POST request
      operationId: post_thing
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/post-request'
        required: true
      responses:
        201:
          description: pending
          content: {}
components:
  schemas:
    post-request:
      required:
      - user
      type: object
      properties:
        user:
          type: string
          description: The user who initiated the request
          example: test user

Command line used for generation

docker run --rm \
    -v $PWD:/local \
    openapitools/openapi-generator-cli \
    generate \
    -i /local/mwe.yaml \
    -g python-flask \
    -o /local/out-mwe \
    --package-name example

Steps to reproduce

  • generate with command and spec from above
  • modify test_default_controller.py, function test_post_thing, variable post_request to be {'user': 'some user'}
  • from the folder that is a parent of the example folder, run python3 -m unittest

Expected behavior

Tests pass

Actual behavior

$ python3 -m unittest
[2020-07-29 23:04:11,960] ERROR in app: Exception on /api/go [POST]
Traceback (most recent call last):
  File "/home/ec2-user/.local/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/ec2-user/.local/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/ec2-user/.local/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ec2-user/.local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/ec2-user/.local/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/ec2-user/.local/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ec2-user/.local/lib/python3.6/site-packages/connexion/decorators/decorator.py", line 48, in wrapper
    response = function(request)
  File "/home/ec2-user/.local/lib/python3.6/site-packages/connexion/decorators/uri_parsing.py", line 172, in wrapper
    response = function(request)
  File "/home/ec2-user/.local/lib/python3.6/site-packages/connexion/decorators/validation.py", line 184, in wrapper
    response = function(request)
  File "/home/ec2-user/.local/lib/python3.6/site-packages/connexion/decorators/parameter.py", line 121, in wrapper
    return function(**kwargs)
TypeError: post_thing() missing 1 required positional argument: 'post_request'
F
======================================================================
FAIL: test_post_thing (example.test.test_default_controller.TestDefaultController)
Test case for post_thing
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ec2-user/environment/dispatch_swagger/out-mwe/example/test/test_default_controller.py", line 34, in test_post_thing
    'Response body is : ' + response.data.decode('utf-8'))
  File "/home/ec2-user/.local/lib/python3.6/site-packages/flask_testing/utils.py", line 336, in assert200
    self.assertStatus(response, 200, message)
  File "/home/ec2-user/.local/lib/python3.6/site-packages/flask_testing/utils.py", line 324, in assertStatus
    self.assertEqual(response.status_code, status_code, message)
AssertionError: 500 != 200 : Response body is : {
  "detail": "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.",
  "status": 500,
  "title": "Internal Server Error",
  "type": "about:blank"
}


----------------------------------------------------------------------
Ran 1 test in 0.026s

FAILED (failures=1)

Related issues/PRs

Suggest a fix

No idea what's gone wrong here.

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

Reproduce the issue with the supplied OpenAPI declaration and generation command, then inspect the generated test/test_default_controller.py and the post_thing controller path. Run python3 -m unittest from the parent of the example folder and trace why post_request is not passed to post_thing. Done means the generated test receives the request body and passes with a 200 response.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.