Esempio n. 1
0
    def get_response_object(self, response_serializer_class, description):

        fields = []
        serializer = response_serializer_class()
        nested_obj = {}

        for field in serializer.fields.values():
            # If field is a serializer, attempt to get its schema.
            if isinstance(field, serializers.Serializer):
                subfield_schema = self.get_response_object(
                    field.__class__, None)[0].get('schema')

                # If the schema exists, use it as the nested_obj
                if subfield_schema is not None:
                    nested_obj[field.field_name] = subfield_schema
                    nested_obj[
                        field.field_name]['description'] = field.help_text
                    continue

            # Otherwise, carry-on and use the field's schema.
            fallback_schema = self.fallback_schema_from_field(field)
            fields.append(
                Field(
                    name=field.field_name,
                    location='form',
                    required=field.required,
                    schema=fallback_schema
                    if fallback_schema else field_to_schema(field),
                ))

        res = _get_parameters(Link(fields=fields), None)

        if not res:
            if nested_obj:
                return {
                    'description': description,
                    'schema': {
                        'type': 'object',
                        'properties': nested_obj
                    }
                }, {}
            else:
                return {}, {}

        schema = res[0]['schema']
        schema['properties'].update(nested_obj)
        response_schema = {'description': description, 'schema': schema}

        error_status_codes = {}

        response_meta = getattr(response_serializer_class, 'Meta', None)

        for status_code, description in getattr(response_meta,
                                                'error_status_codes',
                                                {}).items():
            error_status_codes[status_code] = {'description': description}

        return response_schema, error_status_codes
Esempio n. 2
0
    def get_response_object(self, response_serializer_class, description):

        fields = []
        serializer = response_serializer_class()
        nested_obj = {}

        for field in serializer.fields.values():
            if isinstance(field, serializers.Serializer):
                nested_obj[field.field_name] = self.get_response_object(
                    field.__class__, None)[0]['schema']
                nested_obj[field.field_name]['description'] = field.help_text
                continue

            fields.append(
                Field(name=field.field_name,
                      location='form',
                      required=field.required,
                      schema=field_to_schema(field)))

        res = _get_parameters(Link(fields=fields), None)

        if not res:
            if nested_obj:
                return {
                    'description': description,
                    'schema': {
                        'type': 'object',
                        'properties': nested_obj
                    }
                }, {}
            else:
                return {}, {}

        schema = res[0]['schema']
        schema['properties'].update(nested_obj)
        response_schema = {'description': description, 'schema': schema}

        error_status_codes = {}

        response_meta = getattr(response_serializer_class, 'Meta', None)

        for status_code, description in getattr(response_meta,
                                                'error_status_codes',
                                                {}).items():
            error_status_codes[status_code] = {'description': description}

        return response_schema, error_status_codes