def test_primitive_types_openapi_v3(self):
        for field, result in [
            (
                m.fields.Integer(allow_none=True),
                {
                    "anyOf": [{
                        "type": "null"
                    }, {
                        "type": "integer"
                    }]
                },
            ),
            (
                QueryParamList(m.fields.Integer()),
                {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    },
                    "explode": True
                },
            ),
            (
                CommaSeparatedList(m.fields.Integer()),
                {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    },
                    "style": "simple"
                },
            ),
        ]:

            class Foo(m.Schema):
                a = field

            schema = Foo()
            json_schema = self.registry.convert(schema, openapi_version=3)

            self.assertEqual(
                json_schema,
                {
                    "type": "object",
                    "title": "Foo",
                    "properties": {
                        "a": result
                    }
                },
            )
Exemple #2
0
    def test_primitive_types(self):
        for field, result in [
            (m.fields.Integer(), {
                'type': 'integer'
            }),
            (m.fields.String(), {
                'type': 'string'
            }),
            (m.fields.Number(), {
                'type': 'number'
            }),
            (m.fields.DateTime(), {
                'type': 'string',
                'format': 'date-time'
            }),
            (m.fields.Date(), {
                'type': 'string',
                'format': 'date'
            }),
            (m.fields.UUID(), {
                'type': 'string',
                'format': 'uuid'
            }),
            (m.fields.Boolean(), {
                'type': 'boolean'
            }),
            (m.fields.URL(), {
                'type': 'string'
            }),
            (m.fields.Email(), {
                'type': 'string'
            }),
            (m.fields.Constant('foo'), {
                'enum': ['foo'],
                'default': 'foo'
            }),
            (m.fields.Integer(missing=5), {
                'type': 'integer',
                'default': 5
            }),
            (m.fields.Integer(missing=lambda: 5), {
                'type': 'integer'
            }),
            (m.fields.Integer(allow_none=True), {
                'type': 'integer',
                'x-nullable': True
            }),
            (m.fields.List(m.fields.Integer()), {
                'type': 'array',
                'items': {
                    'type': 'integer'
                }
            }),
            (m.fields.List(m.fields.Integer), {
                'type': 'array',
                'items': {
                    'type': 'integer'
                }
            }),
            (m.fields.Integer(description='blam!'), {
                'type': 'integer',
                'description': 'blam!'
            }),
            (QueryParamList(m.fields.Integer()), {
                'type': 'array',
                'items': {
                    'type': 'integer'
                },
                'collectionFormat': 'multi'
            }),
            (CommaSeparatedList(m.fields.Integer()), {
                'type': 'array',
                'items': {
                    'type': 'integer'
                },
                'collectionFormat': 'csv'
            }),
            (m.fields.Integer(validate=v.Range(min=1)), {
                'type': 'integer',
                'minimum': 1
            }),
            (m.fields.Integer(validate=v.Range(max=9)), {
                'type': 'integer',
                'maximum': 9
            }),
            (m.fields.List(m.fields.Integer(), validate=v.Length(min=1)), {
                'type': 'array',
                'items': {
                    'type': 'integer'
                },
                'minItems': 1
            }),
            (m.fields.List(m.fields.Integer(), validate=v.Length(max=9)), {
                'type': 'array',
                'items': {
                    'type': 'integer'
                },
                'maxItems': 9
            }),
            (m.fields.String(validate=v.Length(min=1)), {
                'type': 'string',
                'minLength': 1
            }),
            (m.fields.String(validate=v.Length(max=9)), {
                'type': 'string',
                'maxLength': 9
            }),
            (m.fields.String(validate=v.OneOf(['a', 'b'])), {
                'type': 'string',
                'enum': ['a', 'b']
            }),
            (m.fields.Dict(), {
                'type': 'object'
            }),
            (m.fields.Method(serialize='x',
                             deserialize='y',
                             swagger_type='integer'), {
                                 'type': 'integer'
                             }),
            (m.fields.Function(serialize=lambda _: _,
                               deserialize=lambda _: _,
                               swagger_type='string'), {
                                   'type': 'string'
                               }),
            (m.fields.Integer(validate=lambda value: True), {
                'type': 'integer'
            }),
        ]:

            class Foo(m.Schema):
                a = field

            schema = Foo()
            json_schema = self.registry.convert(schema)

            self.assertEqual(json_schema, {
                'type': 'object',
                'title': 'Foo',
                'properties': {
                    'a': result
                }
            })
    def test_primitive_types(self):
        for field, result in [
            (m.fields.Integer(), {
                "type": "integer"
            }),
            (m.fields.String(), {
                "type": "string"
            }),
            (m.fields.Number(), {
                "type": "number"
            }),
            (m.fields.DateTime(), {
                "type": "string",
                "format": "date-time"
            }),
            (m.fields.Date(), {
                "type": "string",
                "format": "date"
            }),
            (m.fields.UUID(), {
                "type": "string",
                "format": "uuid"
            }),
            (m.fields.Boolean(), {
                "type": "boolean"
            }),
            (m.fields.URL(), {
                "type": "string"
            }),
            (m.fields.Email(), {
                "type": "string"
            }),
            (m.fields.Constant("foo"), {
                "enum": ["foo"],
                "default": "foo"
            }),
            (m.fields.Integer(missing=5), {
                "type": "integer",
                "default": 5
            }),
            (m.fields.Integer(dump_only=True), {
                "type": "integer",
                "readOnly": True
            }),
            (m.fields.Integer(missing=lambda: 5), {
                "type": "integer"
            }),
            (
                m.fields.Integer(allow_none=True),
                {
                    "type": "integer",
                    "x-nullable": True
                },
            ),
            (
                m.fields.List(m.fields.Integer()),
                {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    }
                },
            ),
            (
                m.fields.List(m.fields.Integer),
                {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    }
                },
            ),
            (
                m.fields.Integer(description="blam!"),
                {
                    "type": "integer",
                    "description": "blam!"
                },
            ),
            (
                QueryParamList(m.fields.Integer()),
                {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    },
                    "collectionFormat": "multi",
                },
            ),
            (
                CommaSeparatedList(m.fields.Integer()),
                {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    },
                    "collectionFormat": "csv",
                },
            ),
            (
                m.fields.Integer(validate=v.Range(min=1)),
                {
                    "type": "integer",
                    "minimum": 1
                },
            ),
            (
                m.fields.Integer(validate=v.Range(max=9)),
                {
                    "type": "integer",
                    "maximum": 9
                },
            ),
            (
                m.fields.List(m.fields.Integer(), validate=v.Length(min=1)),
                {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    },
                    "minItems": 1
                },
            ),
            (
                m.fields.List(m.fields.Integer(), validate=v.Length(max=9)),
                {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    },
                    "maxItems": 9
                },
            ),
            (
                m.fields.String(validate=v.Length(min=1)),
                {
                    "type": "string",
                    "minLength": 1
                },
            ),
            (
                m.fields.String(validate=v.Length(max=9)),
                {
                    "type": "string",
                    "maxLength": 9
                },
            ),
            (
                m.fields.String(validate=v.OneOf(["a", "b"])),
                {
                    "type": "string",
                    "enum": ["a", "b"]
                },
            ),
            (m.fields.Dict(), {
                "type": "object"
            }),
            (
                m.fields.Method(serialize="x",
                                deserialize="y",
                                swagger_type="integer"),
                {
                    "type": "integer"
                },
            ),
            (
                m.fields.Function(
                    serialize=lambda _: _,
                    deserialize=lambda _: _,
                    swagger_type="string",
                ),
                {
                    "type": "string"
                },
            ),
            (m.fields.Integer(validate=lambda value: True), {
                "type": "integer"
            }),
        ]:

            class Foo(m.Schema):
                a = field

            schema = Foo()
            json_schema = self.registry.convert(schema)

            self.assertEqual(
                json_schema,
                {
                    "type": "object",
                    "title": "Foo",
                    "properties": {
                        "a": result
                    }
                },
            )
class IntegerQuery(Schema):
    foos = QueryParamList(fields.Integer())
class StringQuery(Schema):
    foos = QueryParamList(fields.String())
class ExplodedQueryStringSchema(RequestSchema):
    foos = QueryParamList(marshmallow.fields.String(),
                          required=True,
                          description="foo string")