跳转至

Schemas

EmptySchema (Schema) marshmallow-model

An empty schema used to generate a 204 response.

Examples:

@app.delete('/foo')
@app.output(EmptySchema)
def delete_foo():
    return ''

It equals to:

@app.delete('/foo')
@app.output({}, status_code=204)
def delete_foo():
    return ''
Source code in apiflask/schemas.py
class EmptySchema(Schema):
    """An empty schema used to generate a 204 response.

    Example:

    ```python
    @app.delete('/foo')
    @app.output(EmptySchema)
    def delete_foo():
        return ''
    ```

    It equals to:

    ```python
    @app.delete('/foo')
    @app.output({}, status_code=204)
    def delete_foo():
        return ''
    ```
    """

    pass

PaginationSchema (Schema) marshmallow-model

A schema for common pagination information.

Source code in apiflask/schemas.py
class PaginationSchema(Schema):
    """A schema for common pagination information."""
    page = Integer()
    per_page = Integer()
    pages = Integer()
    total = Integer()
    current = URL()
    next = URL()
    prev = URL()
    first = URL()
    last = URL()

Schema (Schema) marshmallow-model

A base schema for all schemas.

The different between marshmallow's Schema and APIFlask's Schema is that the latter sets set_class to OrderedSet by default.

Version Added: 1.2.0

Source code in apiflask/schemas.py
class Schema(BaseSchema):
    """A base schema for all schemas.

    The different between marshmallow's `Schema` and APIFlask's `Schema` is that the latter
    sets `set_class` to `OrderedSet` by default.

    *Version Added: 1.2.0*
    """
    # use ordered set to keep the order of fields
    # can be removed when https://github.com/marshmallow-code/marshmallow/pull/1896 is merged
    set_class = OrderedSet

set_class (MutableSet)

Source code in apiflask/schemas.py
class OrderedSet(MutableSet):
    def __init__(self, iterable=None):
        self.end = end = []
        end += [None, end, end]  # sentinel node for doubly linked list
        self.map = {}  # key --> [key, prev, next]
        if iterable is not None:
            self |= iterable

    def __len__(self):
        return len(self.map)

    def __contains__(self, key):
        return key in self.map

    def add(self, key):
        if key not in self.map:
            end = self.end
            curr = end[1]
            curr[2] = end[1] = self.map[key] = [key, curr, end]

    def discard(self, key):
        if key in self.map:
            key, prev, next = self.map.pop(key)
            prev[2] = next
            next[1] = prev

    def __iter__(self):
        end = self.end
        curr = end[2]
        while curr is not end:
            yield curr[0]
            curr = curr[2]

    def __reversed__(self):
        end = self.end
        curr = end[1]
        while curr is not end:
            yield curr[0]
            curr = curr[1]

    def pop(self, last=True):
        if not self:
            raise KeyError("set is empty")
        key = self.end[1][0] if last else self.end[2][0]
        self.discard(key)
        return key

    def __repr__(self):
        if not self:
            return f"{self.__class__.__name__}()"
        return f"{self.__class__.__name__}({list(self)!r})"

    def __eq__(self, other):
        if isinstance(other, OrderedSet):
            return len(self) == len(other) and list(self) == list(other)
        return set(self) == set(other)

add(self, key)

Add an element.

Source code in apiflask/schemas.py
def add(self, key):
    if key not in self.map:
        end = self.end
        curr = end[1]
        curr[2] = end[1] = self.map[key] = [key, curr, end]

discard(self, key)

Remove an element. Do not raise an exception if absent.

Source code in apiflask/schemas.py
def discard(self, key):
    if key in self.map:
        key, prev, next = self.map.pop(key)
        prev[2] = next
        next[1] = prev

pop(self, last=True)

Return the popped value. Raise KeyError if empty.

Source code in apiflask/schemas.py
def pop(self, last=True):
    if not self:
        raise KeyError("set is empty")
    key = self.end[1][0] if last else self.end[2][0]
    self.discard(key)
    return key

External documentation

Check out the API docs for Schema class in the marshmallow docs: