{"action":"created","issue":{"url":"https://api.github.com/repos/noirbizarre/flask-restplus/issues/179","repository_url":"https://api.github.com/repos/noirbizarre/flask-restplus","labels_url":"https://api.github.com/repos/noirbizarre/flask-restplus/issues/179/labels{/name}","comments_url":"https://api.github.com/repos/noirbizarre/flask-restplus/issues/179/comments","events_url":"https://api.github.com/repos/noirbizarre/flask-restplus/issues/179/events","html_url":"https://github.com/noirbizarre/flask-restplus/issues/179","id":158938439,"number":179,"title":"Input payload validation fails on fields with required=False","user":{"login":"postrational","id":4430709,"avatar_url":"https://avatars.githubusercontent.com/u/4430709?v=3","gravatar_id":"","url":"https://api.github.com/users/postrational","html_url":"https://github.com/postrational","followers_url":"https://api.github.com/users/postrational/followers","following_url":"https://api.github.com/users/postrational/following{/other_user}","gists_url":"https://api.github.com/users/postrational/gists{/gist_id}","starred_url":"https://api.github.com/users/postrational/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/postrational/subscriptions","organizations_url":"https://api.github.com/users/postrational/orgs","repos_url":"https://api.github.com/users/postrational/repos","events_url":"https://api.github.com/users/postrational/events{/privacy}","received_events_url":"https://api.github.com/users/postrational/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-06-07T14:32:09Z","updated_at":"2016-06-08T09:58:54Z","closed_at":null,"body":"Hi there,\r\n\r\nwe're using Flask-Restplus 0.9.0 and we're big fans.\r\n\r\nHowever we keep running into an issue when trying to use optional fields.\r\n\r\nOur code is similar to the following:\r\n\r\n```python\r\nmodel = api.model('Example model', {\r\n    'optional_field': fields.String(required=False),\r\n})\r\n\r\n@ns.route('/test/<int:item_id>/')\r\nclass Item(Resource):\r\n\r\n    @api.marshal_with(model)\r\n    def get(self, item_id):\r\n        \"\"\"\r\n        Returns an Item.\r\n        \"\"\"\r\n        return get_item(item_id)\r\n\r\n    @api.expect(model)\r\n    def put(self, item_id):\r\n        \"\"\"\r\n        Updates an Item.\r\n        \"\"\"\r\n        item_data = request.json\r\n        return update_item(item_id, item_data)\r\n```\r\n\r\nNow when we do a GET to the API endpint, we will receive an Item with the optional field containing a null value.\r\n\r\n    {\r\n        \"optional_field\": null\r\n    }\r\n\r\nUnfortunately, when we try to later send the same item to the API, we get an error message:\r\n\r\n\r\n    {\r\n        \"message\": \"Input payload validation failed\", \r\n        \"errors\": {\"optional_field\": \"None is not of type 'string'\"}\r\n    }\r\n\r\n\r\nThis behavior seems inconsistent. I think it would be better\r\n\r\n* not to serialize the `optional_field` at all in the `marshal_with` method \r\n* or alternatively to accept `null` as a valid value of a field which has `required=False` regardless of its type.\r\n\r\nIs there a workaround for this which allows the `optional_field` to stay undefined?\r\n\r\nThanks for all your great work!"},"comment":{"url":"https://api.github.com/repos/noirbizarre/flask-restplus/issues/comments/224544238","html_url":"https://github.com/noirbizarre/flask-restplus/issues/179#issuecomment-224544238","issue_url":"https://api.github.com/repos/noirbizarre/flask-restplus/issues/179","id":224544238,"user":{"login":"petroslamb","id":1930167,"avatar_url":"https://avatars.githubusercontent.com/u/1930167?v=3","gravatar_id":"","url":"https://api.github.com/users/petroslamb","html_url":"https://github.com/petroslamb","followers_url":"https://api.github.com/users/petroslamb/followers","following_url":"https://api.github.com/users/petroslamb/following{/other_user}","gists_url":"https://api.github.com/users/petroslamb/gists{/gist_id}","starred_url":"https://api.github.com/users/petroslamb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petroslamb/subscriptions","organizations_url":"https://api.github.com/users/petroslamb/orgs","repos_url":"https://api.github.com/users/petroslamb/repos","events_url":"https://api.github.com/users/petroslamb/events{/privacy}","received_events_url":"https://api.github.com/users/petroslamb/received_events","type":"User","site_admin":false},"created_at":"2016-06-08T09:58:54Z","updated_at":"2016-06-08T09:58:54Z","body":"Hi @postrational, \r\n\r\nWe had the same issue and solved it by extending the json schema type of the field `String` to two types, like so:\r\n\r\n```python\r\nclass NullableString(fields.String):\r\n    __schema_type__ = ['string', 'null']\r\n    __schema_example__ = 'nullable string'\r\n```\r\n\r\nHope this helps."}}
{"actor":{"display_login":"petroslamb"}}