Beispiel #1
0
 def test_marshal_list(self):
     fields = OrderedDict([('foo', restplus.fields.Raw),
                           ('fee',
                            restplus.fields.List(restplus.fields.String))])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', ['fye', 'fum'])])
     output = restplus.marshal(marshal_fields, fields)
     expected = OrderedDict([('foo', 'bar'), ('fee', (['fye', 'fum']))])
     self.assertEqual(output, expected)
 def test_marshal_list_of_nesteds(self):
     fields = OrderedDict([
         ('foo', restplus.fields.Raw),
         ('fee', restplus.fields.List(restplus.fields.Nested({
             'fye': restplus.fields.String
         })))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', {'fye': 'fum'})])
     output = restplus.marshal(marshal_fields, fields)
     expected = OrderedDict([('foo', 'bar'), ('fee', [OrderedDict([('fye', 'fum')])])])
     self.assertEqual(output, expected)
Beispiel #3
0
 def test_marshal_nested_with_null(self):
     fields = OrderedDict([('foo', restplus.fields.Raw),
                           ('fee',
                            restplus.fields.Nested(OrderedDict([
                                ('fye', restplus.fields.String),
                                ('blah', restplus.fields.String)
                            ]),
                                                   allow_null=True))])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', None)])
     output = restplus.marshal(marshal_fields, fields)
     expected = OrderedDict([('foo', 'bar'), ('fee', None)])
     self.assertEqual(output, expected)
 def test_marshal_nested_dict(self):
     fields = OrderedDict([
         ('foo', restplus.fields.Raw),
         ('bar', OrderedDict([
             ('a', restplus.fields.Raw),
             ('b', restplus.fields.Raw),
         ])),
     ])
     marshal_fields = OrderedDict([('foo', 'foo-val'), ('bar', 'bar-val'), ('bat', 'bat-val'),
                                   ('a', 1), ('b', 2), ('c', 3)])
     output = restplus.marshal(marshal_fields, fields)
     expected = OrderedDict([('foo', 'foo-val'), ('bar', OrderedDict([('a', 1), ('b', 2)]))])
     self.assertEqual(output, expected)
 def test_allow_null_presents_data(self):
     fields = OrderedDict([
         ('foo', restplus.fields.Raw),
         ('fee', restplus.fields.Nested(
             OrderedDict([
                 ('fye', restplus.fields.String),
                 ('blah', restplus.fields.String)
             ]), allow_null=True))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', {'blah': 'cool'})])
     output = restplus.marshal(marshal_fields, fields)
     expected = OrderedDict([('foo', 'bar'), ('fee', OrderedDict([('fye', None), ('blah', 'cool')]))])
     self.assertEqual(output, expected)
    def test_list_of_raw(self):
        field = fields.List(fields.Raw)

        data = [{'a': 1, 'b': 1}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}]
        expected = [
            OrderedDict([('a', 1), ('b', 1)]),
            OrderedDict([('a', 2), ('b', 1)]),
            OrderedDict([('a', 3), ('b', 1)])
        ]
        self.assert_field(field, data, expected)

        data = [1, 2, 'a']
        self.assert_field(field, data, data)
    def test_marshal_decorator(self):
        fields = OrderedDict([('foo', restplus.fields.Raw)])

        @restplus.marshal_with(fields)
        def try_me():
            return OrderedDict([('foo', 'bar'), ('bat', 'baz')])
        self.assertEqual(try_me(), {'foo': 'bar'})
    def test_marshal_decorator_tuple(self):
        fields = OrderedDict([('foo', restplus.fields.Raw)])

        @restplus.marshal_with(fields)
        def try_me():
            return OrderedDict([('foo', 'bar'), ('bat', 'baz')]), 200, {'X-test': 123}
        self.assertEqual(try_me(), ({'foo': 'bar'}, 200, {'X-test': 123}))
 def test_marshal_nested_property(self):
     class TestObject(object):
         @property
         def fee(self):
             return {'blah': 'cool'}
     fields = OrderedDict([
         ('foo', restplus.fields.Raw),
         ('fee', restplus.fields.Nested(
             OrderedDict([
                 ('fye', restplus.fields.String),
                 ('blah', restplus.fields.String)
             ]), allow_null=True))
     ])
     obj = TestObject()
     obj.foo = 'bar'
     obj.bat = 'baz'
     output = restplus.marshal([obj], fields)
     expected = [OrderedDict([('foo', 'bar'), ('fee', OrderedDict([('fye', None), ('blah', 'cool')]))])]
     self.assertEqual(output, expected)
    def test_with_nested_field(self):
        nested_fields = self.api.model('NestedModel', {'name': fields.String})
        field = fields.List(fields.Nested(nested_fields))
        assert_equal(field.__schema__, {
            'type': 'array',
            'items': {
                '$ref': '#/definitions/NestedModel'
            }
        })

        data = [{
            'name': 'John Doe',
            'age': 42
        }, {
            'name': 'Jane Doe',
            'age': 66
        }]
        expected = [
            OrderedDict([('name', 'John Doe')]),
            OrderedDict([('name', 'Jane Doe')])
        ]
        self.assert_field(field, data, expected)
Beispiel #11
0
 def test_marshal_with_envelope(self):
     fields = OrderedDict([('foo', restplus.fields.Raw)])
     marshal_dict = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = restplus.marshal(marshal_dict, fields, envelope='hey')
     self.assertEqual(output, {'hey': {'foo': 'bar'}})
Beispiel #12
0
 def test_marshal(self):
     fields = OrderedDict([('foo', restplus.fields.Raw)])
     marshal_dict = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = restplus.marshal(marshal_dict, fields)
     self.assertEqual(output, {'foo': 'bar'})
Beispiel #13
0
 def test_marshal_tuple_with_envelope(self):
     fields = OrderedDict({'foo': restplus.fields.Raw})
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = restplus.marshal((marshal_fields, ), fields, envelope='hey')
     self.assertEqual(output, {'hey': [{'foo': 'bar'}]})
Beispiel #14
0
 def test_marshal_tuple(self):
     fields = OrderedDict({'foo': restplus.fields.Raw})
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = restplus.marshal((marshal_fields, ), fields)
     self.assertEqual(output, [{'foo': 'bar'}])
Beispiel #15
0
 def try_me():
     return OrderedDict([('foo', 'bar'), ('bat', 'baz')])
Beispiel #16
0
 def try_me():
     return OrderedDict([('foo', 'bar'), ('bat', 'baz')]), 200, {
         'X-test': 123
     }
Beispiel #17
0
def marshal(data, fields, envelope=None, mask=None):
    """Takes raw data (in the form of a dict, list, object) and a dict of
    fields to output and filters the data based on those fields.
    :param data: the actual object(s) from which the fields are taken from
    :param fields: a dict of whose keys will make up the final serialized
                   response output
    :param envelope: optional key that will be used to envelop the serialized
                     response
    >>> from flask_restplus import fields, marshal
    >>> data = { 'a': 100, 'b': 'foo' }
    >>> mfields = { 'a': fields.Raw }
    >>> marshal(data, mfields)
    OrderedDict([('a', 100)])
    >>> marshal(data, mfields, envelope='data')
    OrderedDict([('data', OrderedDict([('a', 100)]))])
    """
    def make(cls):
        if isinstance(cls, type):
            return cls()
        return cls

    mask = mask or getattr(fields, '__mask__', None)
    fields = getattr(fields, 'resolved', fields)
    if mask:
        fields = apply_mask(fields, mask, skip=True)

    if isinstance(data, (list, tuple)):
        out = [marshal(d, fields) for d in data]
        if envelope:
            out = OrderedDict([(envelope, out)])
        return out

    items = []
    keys = []
    for dkey, val in fields.items():
        key = dkey
        if isinstance(val, dict):
            value = marshal(data, val)
        else:
            field = make(val)
            # exclude already parsed keys from the wildcard
            if isinstance(field, Wildcard):
                for tmp in keys:
                    if tmp not in field.exclude:
                        field.exclude.append(tmp)
                keys = []
            value = field.output(dkey, data)
            if isinstance(field, Wildcard):
                key = field.key or dkey
                items.append((key, value))
                while True:
                    value = field.output(dkey, data)
                    if value is None:
                        break
                    key = field.key
                    items.append((key, value))
                continue
        keys.append(key)
        items.append((key, value))
    items = tuple(items)

    out = OrderedDict(items)

    if envelope:
        out = OrderedDict([(envelope, out)])

    return out