Skip to content

[BUG][Python-Flask] Missing import of re when using string with pattern #1668

Open
@tomghyselinck

Description

@tomghyselinck
Description
  • We use a pattern in string schemas.
  • python-flask uses re.match for the pattern matching in the generated model code, but it misses the import of re.

Note: I noticed that there seems to be another point in the generated server code where the pattern validation is done. You can see this by calling the following command when the server runs:

curl -X PUT "http://localhost:8080/data" -H  "accept: */*" -H  "Content-Type: application/json" -d "{\"myItem\":\"Hello There\"}"

It will properly return an error:

{
  "detail": "'Hello There' does not match '^[a-zA-Z0-9]+$'",
  "status": 400,
  "title": "Bad Request",
  "type": "about:blank"
}

while the server log the message:

http://localhost:8080/data validation error: 'Hello There' does not match '^[a-zA-Z0-9]+$'
127.0.0.1 - - [13/Dec/2018 12:22:11] "PUT /data HTTP/1.1" 400 -
openapi-generator version

I used OpenAPI generator CLI version 4.0.0-SNAPSHOT:
https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/4.0.0-SNAPSHOT/openapi-generator-cli-4.0.0-20181210.103357-85.jar

OpenAPI declaration file content or url

See python-flask-string-pattern.yaml in the attached zip-file:
python-flask-string-pattern.zip

Command line used for generation
java -jar openapi-generator-cli-4.x.jar generate -i ./python-flask-string-pattern.yaml -g python-flask -o ./python-flask-string-pattern/
Steps to reproduce
  1. Generate the server code

    ./python-flask-string-pattern.sh
    
  2. Start the server

    (cd python-flask-string-pattern && python3 -m openapi_server)
    
  3. Perform a client request

    curl -X PUT "http://localhost:8080/data" -H  "accept: */*" -H  "Content-Type: application/json" -d "{\"myItem\":\"HelloThere\"}"
    
    {
      "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"
    }
    
  4. The server now logs an error message

    [2018-12-13 12:22:03,113] ERROR in app: Exception on /data [PUT]
    Traceback (most recent call last):
      File "/home/tom/.local/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
        response = self.full_dispatch_request()
    ...
      File "/home/tom/issues/python-flask-string-pattern/openapi_server/util.py", line 111, in deserialize_model
        setattr(instance, attr, _deserialize(value, attr_type))
      File "/home/tom/issues/python-flask-string-pattern/openapi_server/models/data.py", line 63, in my_item
        if my_item is not None and not re.search(r'^[a-zA-Z0-9]+$', my_item):  # noqa: E501
    NameError: name 're' is not defined
    127.0.0.1 - - [13/Dec/2018 12:22:03] "PUT /data HTTP/1.1" 500 -
    
Related issues/PRs
Suggest a fix

Two things to cover here:

  1. Add missing import of re
  2. Check whether duplicate check of the pattern is really required?
    Probably setting the field value in the model object is used somewhere else too? Not only when verifying (remote) user input?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions