[BUG] Python-Flask Models do not Validate on Initialization
Created by: boyleconnor
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.yaml
cd server
python
>>> 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.