Open
Description
Description
- We use a
pattern
instring
schemas. python-flask
usesre.match
for the pattern matching in the generatedmodel
code, but it misses the import ofre
.
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
-
Generate the server code
./python-flask-string-pattern.sh
-
Start the server
(cd python-flask-string-pattern && python3 -m openapi_server)
-
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" }
-
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
- [BUG][python-flask] Generator fails to create required imports for models #1755: It is not a model object, but an implementation requirement though.
- Fix openapi_types generation error in Python Flask's models #1256
Suggest a fix
Two things to cover here:
- Add missing import of
re
- 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?