Esempio n. 1
0
    def _get_examples(self,
                      serializer,
                      direction,
                      media_type,
                      status_code=None):
        examples = self.get_examples()

        if not examples:
            if is_list_serializer(serializer):
                examples = get_override(serializer.child, 'examples', [])
            elif is_serializer(serializer):
                examples = get_override(serializer, 'examples', [])

        filtered_examples = []
        for example in examples:
            if direction == 'request' and example.response_only:
                continue
            if direction == 'response' and example.request_only:
                continue
            if media_type and media_type != example.media_type:
                continue
            if status_code and status_code not in example.status_codes:
                continue
            filtered_examples.append(example)

        return build_examples_list(filtered_examples)
Esempio n. 2
0
    def _process_override_parameters(self):
        result = []
        exclusions = []
        for parameter in self.get_override_parameters():
            if isinstance(parameter, OpenApiParameter):
                if is_basic_type(parameter.type):
                    schema = build_basic_type(parameter.type)
                elif is_serializer(parameter.type):
                    schema = self.resolve_serializer(parameter.type,
                                                     'request').ref
                else:
                    schema = parameter.type

                if parameter.exclude:
                    exclusions.append((parameter.name, parameter.location))
                else:
                    result.append(
                        build_parameter_type(
                            name=parameter.name,
                            schema=schema,
                            location=parameter.location,
                            required=parameter.required,
                            description=parameter.description,
                            enum=parameter.enum,
                            deprecated=parameter.deprecated,
                            style=parameter.style,
                            explode=parameter.explode,
                            examples=build_examples_list(parameter.examples),
                        ))
            elif is_serializer(parameter):
                # explode serializer into separate parameters. defaults to QUERY location
                mapped = self._map_serializer(parameter, 'request')
                for property_name, property_schema in mapped[
                        'properties'].items():
                    result.append(
                        build_parameter_type(
                            name=property_name,
                            schema=property_schema,
                            location=OpenApiParameter.QUERY,
                            required=property_name
                            in mapped.get('required', []),
                        ))
            else:
                warn(
                    f'could not resolve parameter annotation {parameter}. skipping.'
                )
        return result, exclusions