swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.98k stars 6.03k forks source link

Python-flask generated model class skips validation when parameters passed via constructor #5815

Open mdear opened 7 years ago

mdear commented 7 years ago
Description

I noticed that when constructing an object from an autogenerated class that validations were not done by default.

Swagger-codegen version

java -jar swagger-codegen-cli.jar version 2.2.2

Swagger declaration file content or url
Command line used for generation

java -jar ../swagger-codegen-cli.jar generate -l python-flask -i /ws/mdear-ott/Projects/S3/qmgr/qmgr_api.yaml -c ../generate_options_server.json

pip list amqp (1.4.6) amqplib (1.0.2) AMQPQueue (0.4.2) anyjson (0.3.3) APScheduler (3.0.1) astroid (1.3.2) backports.ssl-match-hostname (3.4.0.2) billiard (3.3.0.19) Candygram (1.0) celery (3.1.17) certifi (14.05.14) CoronaAPI (1.3.0) crontab (0.20.2) cx-Oracle (5.1.2) decorator (3.4.0) desktop (0.4.2) distribute (0.7.3) Django (1.7.1) ecdsa (0.11) enum34 (1.0.4) flower (0.7.3) FormEncode (1.3.0a1) frosted (1.4.1) futures (2.2.0) fysom (2.0.1) http-parser (0.8.3) httplib2 (0.9) ipaddress (1.0.7) ipython (2.3.1) jenkins (1.0.2) jenkinsapi (0.2.25) Jinja2 (2.7.3) kombu (3.0.24) lockfile (0.10.2) locknix (1.0.3) logilab-common (0.63.2) lxml (3.4.1) MarkupSafe (0.23) MySQL-python (1.2.5) MySQLObject (1.3.1) ndg-httpsclient (0.4.2) netaddr (0.7.12) oauth2 (1.5.211) oauthlib (0.7.2) ordereddict (1.1) paramiko (1.15.1) pexpect (3.3) pies (2.6.1) pies2overrides (2.6.1) pip (1.5.6) ply (3.4) progressbar (2.2) protobuf (2.6.1) psutil (2.1.3) pyasn1 (0.1.9) pycrypto (2.6.1) pylint (1.4.0) pymongo (2.7.2) PyOpenGL (3.1.0b3) pyOpenSSL (16.2.0) pyotp (1.4.1) pysqlite (2.6.3) python-daemon (1.6.1) python-dateutil (2.3) python-jenkins (0.2) python-ldap (2.3.13) pytz (2014.10) PyYAML (3.11) requests (2.5.0) requests-oauth2 (0.2.0) restkit (4.2.2) setuptools (7.0) simplejson (3.6.5) six (1.8.0) socketpool (0.5.3) South (1.0.1) SQLAlchemy (0.9.8) sqlite3dbm (0.1.4) SQLObject (1.7.0) sqlsoup (0.9.0) suds-jurko (0.6) tornado (4.0.2) Twisted (14.0.2) tzlocal (1.1.2) validictory (1.0.0a2) virtualenv (1.11.6) web.py (0.37) wsgiref (0.1.2) xmlrunner (1.7.7) zope.interface (4.1.1)

Generated code

CommonReturnModel(Model): """ NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. """ def init(self, message: str=None, code: str=None): """ CommonReturnModel - a model defined in Swagger

    :param message: The message of this CommonReturnModel.
    :type message: str
    :param code: The code of this CommonReturnModel.
    :type code: str
    """
    self.swagger_types = {
        'message': str,
        'code': str
    }

    self.attribute_map = {
        'message': 'message',
        'code': 'code'
    }

    self._message = message
    self._code = code
Steps to reproduce

from qmgr_svr.models.common_failure_return_model import CommonFailureReturnModel

The following should fail validation but does not raise an exception (silently fails): obj = CommonReturnModel(code="wrong", message="Something bad happened")

In contrast, the following raises an exception: obj = CommonReturnModel() obj.code="wrong" *** ValueError: Invalid value for code (wrong), must be one of ['SUCCESS', 'FAILURE']

Related issues
Suggest a Fix
wing328 commented 7 years ago

@mdear thanks for reporting the issue. If I remember correctly, I suggest using the Python model.mustache for Python flask generator (as Python client generator is more mature): https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/python/model.mustache

Would that work for you?

mdear commented 7 years ago

Thanks for getting back to me, wing328. Sadly, due to time-to-market reasons I had to implement my own server classes manually, as I'm working in Django Rest Framework and there wasn't a generator available. Following good design practice, my specs come first, and then the code, but it appears there is only a DRF->Swagger converter, but no code generator support going the other direction.

wing328 commented 7 years ago

@mdear no problem. If you've time to contribute a Django REST Framework generator, let me know and we can work together to create one. It should not be difficult as we can use the Python Flask generator as a starting point.