OpenAPITools / OpenAPITools/openapi-generator
[BUG][Python-Flask] Missing import of `re` when using `string` with `pattern`
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
- We use a
patterninstringschemas. python-flaskusesre.matchfor the pattern matching in the generatedmodelcode, 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
- #1755: It is not a model object, but an implementation requirement though.
- #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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the generated openapi_server/models/data.py and the python-flask-string-pattern.yaml input, then reproduce the failure with python-flask-string-pattern.sh and the documented curl request. Trace the generated model validation and compare it with openapi_server/util.py. Done means pattern validation no longer raises NameError, the request returns the expected validation response, and duplicate validation is assessed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- flask, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100