OpenAPITools / OpenAPITools/openapi-generator

[BUG] python-flask unit tests do not pass, required arguments not passed

Open
#6,885 1 comment 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

If you generate code for python-flask, then try to run the unit tests,
they fail, because the unit tests don't include mandatory arguments.

I expect that all unit tests should work out of the box.

openapi-generator version
openapi-generator-cli 5.0.0-SNAPSHOT
  commit : 068ad02
  built  : 2020-07-02T17:04:18Z
OpenAPI declaration file content or url
---
swagger: "2.0"
info:
  description: "API for demonstrating unit test bug"
  version: "1.0.0"
  title: "MWE"
host: "example.com"
basePath: "/v1"
schemes:
- "http"
paths:
  /example:
    post:
      description: "sample call"
      operationId: "sample_post"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "requestBody"
        description: "input body"
        required: true
        schema:
          $ref: "#/definitions/example-post-request"
      responses:
        "201":
          description: "pending"
        "400":
          description: "Invalid request body"
        "401":
          description: "Invalid HTTP basic username/password"
        "403":
          description: "Invalid certificate"
definitions:
  example-post-request:
    type: "object"
    required:
    - "user"
    properties:
      user:
        type: "string"
        example: "test user"
        description: "The user who initiated the request"
      
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 mwe
Steps to reproduce
  • Generate as per above
  • cd out-mwe/
  • pip install -r requirements.txt
  • pip install -r test-requirements.txt
  • python -m unittest

Expected behavior:

All tests pass

Actual Behavior

http://localhost/v1/example validation error: 'user' is a required property
F
======================================================================
FAIL: test_sample_post (mwe.test.test_default_controller.TestDefaultController)
Test case for sample_post
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ec2-user/environment/dispatch_swagger/out-mwe/mwe/test/test_default_controller.py", line 32, in test_sample_post
    'Response body is : ' + response.data.decode('utf-8'))
  File "/home/ec2-user/environment/dispatch_swagger/venv/lib64/python3.6/dist-packages/flask_testing/utils.py", line 336, in assert200
    self.assertStatus(response, 200, message)
  File "/home/ec2-user/environment/dispatch_swagger/venv/lib64/python3.6/dist-packages/flask_testing/utils.py", line 324, in assertStatus
    self.assertEqual(response.status_code, status_code, message)
AssertionError: 400 != 200 : Response body is : {
  "detail": "'user' is a required property",
  "status": 400,
  "title": "Bad Request",
  "type": "about:blank"
}


----------------------------------------------------------------------
Ran 1 test in 0.034s

FAILED (failures=1)
Related issues/PRs

None

Suggest a fix

The test invokes the call using an empty dictionary.
It should include all required arguments.
If an example was in the spec, it should use that.

The only tricky thing is what to put as the value for a required argument when there's a non-trivial regex and no example. In that case I think it's best to include the argument, and have some placeholder value which will fail the regex matching.

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

Generate the sample with the provided OpenAPI document, then run python -m unittest from out-mwe/ to reproduce the failure in mwe/test/test_default_controller.py. Trace the generated python-flask test call and required request-body fields; done means the generated unit tests provide required arguments and pass.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.