OpenAPITools / OpenAPITools/openapi-generator
[BUG] Python-Flask Models do not Validate on Initialization
Nobody has claimed this yet.
- 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)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Generated model classes in Python-Flask have an __init__() method that assigns constructor arguments directly to the ._attribute, instead of assigning to the .attribute (which would implicitly call the associated setter method), and this therefore bypasses validation. Bypassing validation at object initialization time is undesirable.
E.g.
If one has a schema like the following:
note:
type: object
description: A note
properties:
type:
description: The type of the note
type: string
enum:
- "to-do"
- "reminder"
text:
description: The content of the note
type: string
required:
- type
- text
example:
text: "Get milk at the store."
type: "to-do"
then the Note model generated from that (in python-flask mode) can be imported and used like this:
>>> from openapi_server.models.note import Note
>>> note = Note()
>>> # Notice the lack of exception despite not having all the required parameters set
which is undesired behavior. The Note() model should validate on initialization, and the above expression should result in an exception (due to missing required properties).
openapi-generator version
5.2.1
OpenAPI declaration file content or url
openapi: 3.0.3
info:
version: 1.0.0
title: Example note API
paths:
/note:
get:
summary: Get note
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/Note'
components:
schemas:
Note:
type: object
description: A clinical note
properties:
text:
description: The content of the note
type: string
type:
description: The note type
type: string
enum:
- "to-do"
- "reminder"
required:
- text
- type
Generation Details
openapi-generator-cli generate -g python-flask -o server -i openapi.yaml
(openapi.yaml is a file containing the above specification)
Steps to reproduce
openapi-generator-cli generate -g python-flask -o server -i openapi.yamlcd serverpython
>>> from openapi_server.models.note import Note
>>> Note() # This is an invalid note (no `text` or `type`) and this call *should* result in an exception
{'text': None, 'type': None}
The last output line above should be an error. Notice the desired behavior (validation) does happen when changing the value of an existing Note:
>>> note = Note()
>>> note.type = 'asdf' # Not an allowed value, see the enum in the spec
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/connor/Documents/tmp/server/openapi_server/models/note.py", line 97, in type
raise ValueError(
ValueError: Invalid value for `type` (asdf), must be one of ['to-do', 'reminder']
Related issues/PRs
Suggest a fix
This bug should be fixable by changing this line of the template.
self._{{name}} = {{name}}
should be
self.{{name}} = {{name}}
this will ensure that every assigned value gets passed through the setter for that attribute, now including at initialization time.
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 modules/openapi-generator/src/main/resources/python-flask/model.mustache, especially the constructor assignment described in the issue. Generate the provided OpenAPI example with the python-flask command and reproduce Note() and invalid enum assignment behavior. Done means initialization validates required and enum properties consistently with later attribute assignments.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100