def test_deserialize_dict(self): req = self.json_request("{}") expected = dict() results = helpers.deserialize(req) self.assertEquals(expected, results) expected = dict(this="that") req = self.json_request('{"this": "that"}') results = helpers.deserialize(req) self.assertEquals(expected, results)
def from_http_req(obj_type, req, **overrides): """ Given a supplied dict or list coming from an HTTP API request body that has been deserialized, validate that properties of the list or dict match the JSONSchema for the HTTP method of the object, and return an appropriate object. :param obj_type: The class of object to return. :param req: The `falcon.request.Request` object. :param **overrides: Any attributes that should be overridden in the serialized body. Useful for when we are dealing with a collection resource's subcollection. For example, when creating a new public key for a user, the HTTP request looks like: POST /users/{user_id}/keys We override the value of the body's user_id field (if set) with the value of the {user_id} from the URI itself. :returns An object of the appropriate subclass. :raises `exc.BadInput` if the object model's schema does not match the supplied instance structure or if the request could not be deserialized. """ api_version = version.tuple_from_request(req) values = helpers.deserialize(req) if overrides: values.update(overrides) schema = _JSONSCHEMA_CATALOG.schema_for_version(req.method, obj_type, api_version) try: jsonschema.validate(values, schema) except jsonschema.ValidationError as e: raise exc.BadInput(e) return obj_type(**values)
def from_http_req(cls, req, **overrides): """ Given a supplied dict or list coming from an HTTP API request body that has been deserialized), validate that properties of the list or dict match the JSONSchema for the HTTP method of the object, and return an appropriate object. :param req: The `falcon.request.Request` object. :param **overrides: Any attributes that should be overridden in the serialized body. Useful for when we are dealing with a collection resource's subcollection. For example, when creating a new public key for a user, the HTTP request looks like: POST /users/{user_id}/keys We override the value of the body's userId field (if set) with the value of the {user_id} from the URI itself. :returns An object of the appropriate subclass. :raises `exc.BadInput` if the object model's schema does not match the supplied instance structure or if the request could not be deserialized. """ api_version = version.tuple_from_request(req) ctx = rest_context.from_http_req(req) deser_body = rest_helpers.deserialize(req) if overrides: deser_body.update(overrides) schema = JSONSCHEMA_CATALOG.schema_for_version(req.method, cls._SINGULAR_NAME, api_version) try: jsonschema.validate(deser_body, schema) except jsonschema.ValidationError as e: raise exc.BadInput(e) values = cls.field_names_to_capnp(deser_body) return cls(cls._CAPNP_OBJECT.new_message(**values), ctx=ctx)
def test_deserialize_bad_mime_type(self): req = mock.MagicMock() req = mock.MagicMock(content_type="application/xml") with testtools.ExpectedException(fexc.HTTPNotAcceptable): helpers.deserialize(req)
def test_deserialize_list(self): req = self.json_request("[]") expected = list() results = helpers.deserialize(req) self.assertEquals(expected, results)