Beispiel #1
0
    def responses(self):
        """
        Collects method responses

        :return: swagger responses object
        :rtype: OrderedDict
        """
        responses = OrderedDict()
        responses.update(self.introspector.responses)
        responses.update(super(BaseMethodIntrospector, self).responses)

        serializer = self._get_serializer()

        if serializer and not responses.get(200, None):
            si = SerializerIntrospector(serializer)
            if 'list' in self.method.lower():
                pagination_introspector = get_pagination_introspector(
                    self.view, si=si)
                responses.update(pagination_introspector.responses)
            else:
                response = OrderedDict([
                    ('description', 'Default response'),
                    ('schema', si.build_response_object()['schema']),
                ])
                responses[200] = response

        status_code = self.STATUS_CODES.get(self.method,
                                            self.DEFAULT_STATUS_CODE)
        response = responses.pop(200, None)
        # TODO this code wants to be rewritten
        if response:
            if status_code == status.HTTP_204_NO_CONTENT:
                response.pop('schema', None)
            if not responses.get(status_code, None):
                responses[status_code] = response

        if status_code not in responses:
            response = OrderedDict([
                ('description', 'Empty response'),
            ])
            responses[status_code] = response

        return responses
Beispiel #2
0
    def _get_field_object(self, request=False):
        """
        Creates swagger object for field for request or response

        :param request: is this object for request?
        :return: swagger object
        :rtype: OrderedDict
        """
        if isinstance(self._field, BaseSerializer):
            if getattr(self._field, 'many', None):
                result = {
                    'type':
                    'array',
                    'items':
                    self._serializer_inrospector_class(
                        self._field).build_response_object(),
                }
            else:
                result = self._serializer_inrospector_class(
                    self._field).build_response_object()
        else:
            field_type, data_type, data_format = self._get_field_type(
                self._field)
            if data_type == 'file' and not request:
                data_type = 'string'
            result = OrderedDict(type=data_type)

            # Retrieve Field metadata
            max_val = getattr(self._field, 'max_value', None)
            min_val = getattr(self._field, 'min_value', None)
            max_length = getattr(self._field, 'max_length', None)
            default = self._get_default_value()
            description = getattr(self._field, 'help_text', '')

            if data_format:
                result['format'] = data_format

            if max_val is not None and data_type in ('integer', 'number'):
                result['minimum'] = min_val

            if max_val is not None and data_type in ('integer', 'number'):
                result['maximum'] = max_val

            if max_length is not None and data_type == 'string':
                result['maxLength'] = max_length

            if description:
                result['description'] = description

            if default is not None:
                result['default'] = default

            if field_type in ['multiple choice', 'choice']:
                if isinstance(self._field.choices, dict):
                    result['enum'] = [k for k in self._field.choices]

                if all(
                        isinstance(item, int)
                        for item in result.get('enum', ['1'])):
                    result['type'] = 'integer'
        return result