def _map_response_type_hint(self, method): hint = get_override(method, 'field') or typing.get_type_hints(method).get('return') if is_serializer(hint) or is_field(hint): return self._map_serializer_field(force_instance(hint), 'response') try: return resolve_type_hint(hint) except UnableToProceedError: warn( f'unable to resolve type hint for function "{method.__name__}". consider ' f'using a type hint or @extend_schema_field. defaulting to string.' ) return build_basic_type(OpenApiTypes.STR)
def test_is_field(): assert is_field(serializers.SlugField) assert is_field(serializers.SlugField()) assert not is_field(models.CharField) assert not is_field(models.CharField()) assert not is_field(serializers.Serializer) assert not is_field(serializers.Serializer())
def _map_type_hint(self, method): hint = getattr(method, '_spectacular_annotation', None) or typing.get_type_hints(method).get('return') if is_serializer(hint) or is_field(hint): return self._map_serializer_field(force_instance(hint)) elif is_basic_type(hint): return build_basic_type(hint) elif getattr(hint, '__origin__', None) is typing.Union: if type(None) == hint.__args__[1] and len(hint.__args__) == 2: schema = build_basic_type(hint.__args__[0]) schema['nullable'] = True return schema else: warn(f'type hint {hint} not supported yet. defaulting to "string"') return build_basic_type(OpenApiTypes.STR) else: warn(f'type hint for function "{method.__name__}" is unknown. defaulting to string.') return build_basic_type(OpenApiTypes.STR)
def _map_response_type_hint(self, method): hint = get_override(method, 'field') or typing.get_type_hints(method).get('return') if is_serializer(hint) or is_field(hint): return self._map_serializer_field(force_instance(hint), 'response') elif is_basic_type(hint, allow_none=False): return build_basic_type(hint) elif getattr(hint, '__origin__', None) is typing.Union: if type(None) == hint.__args__[1] and len(hint.__args__) == 2: schema = build_basic_type(hint.__args__[0]) schema['nullable'] = True return schema else: warn(f'type hint {hint} not supported yet. defaulting to "string"') return build_basic_type(OpenApiTypes.STR) else: warn( f'type hint for function "{method.__name__}" is unknown. consider using ' f'a type hint or @extend_schema_field. defaulting to string.' ) return build_basic_type(OpenApiTypes.STR)