A decorator that apply marshalling to the return values of your methods.

        :param schema: The schema class to be used to serialize the values.
        :param envelope: The key used to envelope the data.
        :return: A function.

def marshal_with(self, schema, envelope=None):
        """
        A decorator that apply marshalling to the return values of your methods.

        :param schema: The schema class to be used to serialize the values.
        :param envelope: The key used to envelope the data.
        :return: A function.
        """

        # schema is pre instantiated to avoid instantiate it
        # on every request
        schema_is_class = isclass(schema)
        schema_cache = schema() if schema_is_class else schema

        def decorator(func):
            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                data = func(*args, **kwargs)
                if isinstance(data, self.__app.response_class):
                    return data

                schema_instance = schema_cache

                # if there is the parameter 'fields' in the url
                # we cannot use the cached schema instance
                # in this case we have to instantiate the schema
                # on every request.
                if schema_is_class:
                    only = get_fields_from_request(schema=schema)
                    if only:
                        schema_instance = schema(only=only)

                return marshal(data, schema_instance, envelope)
            return wrapper
        return decorator