Exemplo n.º 1
0
    def test_fields2parameters_does_not_modify_metadata(self):
        field_dict = {'field': fields.Str(location='querystring')}
        res = swagger.fields2parameters(field_dict, default_in='headers')
        assert res[0]['in'] == 'query'

        res = swagger.fields2parameters(field_dict, default_in='headers')
        assert res[0]['in'] == 'query'
Exemplo n.º 2
0
    def test_fields_with_dump_only(self):
        class UserSchema(Schema):
            name = fields.Str(dump_only=True)

        res = swagger.fields2parameters(UserSchema._declared_fields,
                                        default_in='query')
        assert len(res) == 0
        res = swagger.fields2parameters(UserSchema().fields,
                                        default_in='query')
        assert len(res) == 0

        class UserSchema(Schema):
            name = fields.Str()

            class Meta:
                dump_only = ('name', )

        res = swagger.fields2parameters(UserSchema._declared_fields,
                                        schema=UserSchema,
                                        default_in='query')
        assert len(res) == 0
        res = swagger.fields2parameters(UserSchema().fields,
                                        schema=UserSchema,
                                        default_in='query')
        assert len(res) == 0
Exemplo n.º 3
0
    def _fields_to_swagger(self, fields):
        if not fields:
            return []
        if isinstance(fields, list):
            return fields
        if isinstance(fields, dict) and all(
                isinstance(field, dict) for field in fields.values()):
            return list(fields.values())

        if all(
                field.metadata.get('location') == 'json'
                for field_name, field in iteritems(fields)):
            default_in = 'body'
        else:
            default_in = 'query'

        parameters = fields2parameters(fields,
                                       default_in=default_in,
                                       required=True,
                                       dump=False)
        # XXX: We use virtual 'body' argument due to the limitations of webargs:
        # https://github.com/sloria/webargs/issues/81
        # Here we drop 'body' argument, so Swagger spec will use a JSON list as
        # a top-level structure instead of unnecessary 'body' wrapper.
        if parameters and 'schema' in parameters[0] and 'body' in parameters[
                0]['schema']['properties']:
            parameters[0]['schema'] = parameters[0]['schema']['properties'][
                'body']
        return parameters
Exemplo n.º 4
0
 def test_fields_default_in_body(self):
     field_dict = {
         'name': fields.Str(),
         'email': fields.Email(),
     }
     res = swagger.fields2parameters(field_dict)
     assert len(res) == 1
     assert set(res[0]['schema']['properties'].keys()) == {'name', 'email'}
Exemplo n.º 5
0
    def test_fields_default_location_mapping_if_schema_many(self):

        class ExampleSchema(Schema):
            id = fields.Int()

        schema = ExampleSchema(many=True)
        res = swagger.fields2parameters(schema.fields, schema=schema, default_in='json')
        assert res[0]['in'] == 'body'
Exemplo n.º 6
0
 def test_params(self, app, path):
     params = path['get']['parameters']
     rule = app.url_map._rules_by_endpoint['get_band'][0]
     expected = (
         swagger.fields2parameters({'name': fields.Str()}, default_in='query') +
         rule_to_params(rule)
     )
     assert params == expected
Exemplo n.º 7
0
 def test_fields_query(self):
     field_dict = {
         'name': fields.Str(),
         'email': fields.Email(),
     }
     res = swagger.fields2parameters(field_dict, default_in='query')
     assert len(res) == 2
     res.sort(key=lambda param: param['name'])
     assert res[0]['name'] == 'email'
     assert res[0]['in'] == 'query'
     assert res[1]['name'] == 'name'
     assert res[1]['in'] == 'query'
Exemplo n.º 8
0
 def test_fields_with_multiple_json_locations(self):
     field_dict = {'field1': fields.Str(location='json', required=True),
                   'field2': fields.Str(location='json', required=True),
                   'field3': fields.Str(location='json')}
     res = swagger.fields2parameters(field_dict, default_in=None)
     assert len(res) == 1
     assert res[0]['in'] == 'body'
     assert res[0]['required'] is False
     assert 'field1' in res[0]['schema']['properties']
     assert 'field2' in res[0]['schema']['properties']
     assert 'field3' in res[0]['schema']['properties']
     assert 'required' in res[0]['schema']
     assert len(res[0]['schema']['required']) == 2
     assert 'field1' in res[0]['schema']['required']
     assert 'field2' in res[0]['schema']['required']
    def _fields_to_swagger(self, fields):
        if not fields:
            return []
        if isinstance(fields, list):
            return fields
        if isinstance(fields, dict) and all(isinstance(field, dict) for field in fields.values()):
            return list(fields.values())

        if all(field.metadata.get('location') == 'json' for field_name, field in iteritems(fields)):
            default_in = 'body'
        else:
            default_in = 'query'

        parameters = fields2parameters(fields, default_in=default_in, required=True, dump=False)
        # XXX: We use virtual 'body' argument due to the limitations of webargs:
        # https://github.com/sloria/webargs/issues/81
        # Here we drop 'body' argument, so Swagger spec will use a JSON list as
        # a top-level structure instead of unnecessary 'body' wrapper.
        if parameters and 'schema' in parameters[0] and 'body' in parameters[0]['schema']['properties']:
            parameters[0]['schema'] = parameters[0]['schema']['properties']['body']
        return parameters
Exemplo n.º 10
0
 def test_fields_with_location(self):
     field_dict = {'field': fields.Str(location='querystring')}
     res = swagger.fields2parameters(field_dict, default_in='headers')
     assert res[0]['in'] == 'query'
Exemplo n.º 11
0
 def test_fields_with_default_load(self):
     field_dict = {'field': fields.Str(default='foo', missing='bar')}
     res = swagger.fields2parameters(field_dict, default_in='query')
     assert res[0]['default'] == 'bar'
Exemplo n.º 12
0
 def test_fields_default_location_mapping(self):
     field_dict = {'field': fields.Str()}
     res = swagger.fields2parameters(field_dict, default_in='headers')
     assert res[0]['in'] == 'header'
Exemplo n.º 13
0
 def test_params(self, app, path):
     params = path['get']['parameters']
     rule = app.url_map._rules_by_endpoint['get_band'][0]
     expected = (swagger.fields2parameters(
         {'name': fields.Str()}, default_in='query') + rule_to_params(rule))
     assert params == expected
Exemplo n.º 14
0
 def test_fields_with_dump_only(self):
     field_dict = {'field': fields.Str(dump_only=True)}
     res = swagger.fields2parameters(field_dict, default_in='query', dump=False)
     assert len(res) == 0