question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] python-flask ignores missing required fields, and values that don't match enum

See original GitHub issue

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

python-flask does not raise errors when required fields are missing, or they don’t match a value in an enum.

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"
      responses:
        "201":
          description: "pending"
          schema:
            $ref: "#/definitions/example-post-response"
definitions:
  example-post-response:
    type: "object"
    required:
    - "user"
    properties:
      user:
        type: "string"
        example: "User A"
        description: "The user who initiated the request"
        enum:
        - "User B"
        - "User A"
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
  1. Generate the code
  2. Modify example/controllers/default_controller.py. Replace do some magic to instead return ExamplePostResponse()
  3. pip install -e output_dir
  4. Run python -m example
  5. in another terminal run curl http://localhost:8080/v1/example -d "param1=value1&param2=value2" -X POST -v
  6. Note what’s logged in both terminals
  7. Retry, but with ExamplePostResponse(user='value not in enum')

expected behavior

At step 5 the server code should fail to instantiate ExamplePostResponse(), because required field user is missing. It should catch this error and return 5XX to curl.

At step 7 the server should fail to instantiate ExamplePostResponse() because the field user has a value that’s not in the enum. It should catch this error and return 5XX to the curl.

actual behavior

No error thrown at step 5 or 7. The server returns a non-compliant payload. (Also the HTTP 200 status is not one allowed in the spec.)

Related issues/PRs
Suggest a fix

Something like:

for each field:
   if field is required and field is not specified:
      raise ValueError
   if field is an enum, and value not in enum:
      raise ValueError

Add that here:

https://github.com/OpenAPITools/openapi-generator/blob/850c958d83b32de9f2274349ef3a111631fd938a/modules/openapi-generator/src/main/resources/python-flask/model.mustache#L62

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

0reactions
aassaa110012commented, Oct 31, 2022

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

python-flask does not raise errors when required fields are missing, or they don’t match a value in an enum.

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"

      responses:

        "201":

          description: "pending"

          schema:

            $ref: "#/definitions/example-post-response"

definitions:

  example-post-response:

    type: "object"

    required:

    - "user"

    properties:

      user:

        type: "string"

        example: "User A"

        description: "The user who initiated the request"

        enum:

        - "User B"

        - "User A"

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
  1. Generate the code

  2. Modify example/controllers/default_controller.py. Replace do some magic to instead return ExamplePostResponse()

  3. pip install -e output_dir

  4. Run python -m example

  5. in another terminal run curl http://localhost:8080/v1/example -d "param1=value1&param2=value2" -X POST -v

  6. Note what’s logged in both terminals

  7. Retry, but with ExamplePostResponse(user='value not in enum')

expected behavior

At step 5 the server code should fail to instantiate ExamplePostResponse(), because required field user is missing.

It should catch this error and return 5XX to curl.

At step 7 the server should fail to instantiate ExamplePostResponse() because the field user has a value that’s not in the enum. It should catch this error and return 5XX to the curl.

actual behavior

No error thrown at step 5 or 7. The server returns a non-compliant payload. (Also the HTTP 200 status is not one allowed in the spec.)

Related issues/PRs
Suggest a fix

Something like:


for each field:

   if field is required and field is not specified:

      raise ValueError

   if field is an enum, and value not in enum:

      raise ValueError

Add that here:

https://github.com/OpenAPITools/openapi-generator/blob/850c958d83b32de9f2274349ef3a111631fd938a/modules/openapi-generator/src/main/resources/python-flask/model.mustache#L62

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Flask WTForm SelectField with Enum values 'Not a ...
1 Answer 1 · The choice value should be an Enum instance · The field value should be str(Enum.value) · The field text...
Read more >
API — Flask Documentation (2.2.x)
So it's important what you provide there. If you are using a single module, __name__ is always the correct value. If you however...
Read more >
Developing Python Web Applications with Flask
This article describe the Python's flask framework. ... It contains the field data, and also has properties, such as label , id ,...
Read more >
Changelog — Python 3.11.1 documentation
The errno modules exposes the new error number. getpath.py now ignores PermissionError when it cannot open landmark files pybuilddir.txt and ...
Read more >
apispec - Read the Docs
We can now use the marshmallow and Flask plugins. ... A callable passed to the field's missing parameter will be ignored. Parameters.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found