def get_path_parameters(self, path: str, method: str) -> List[Dict]: parameters = [] model = getattr(getattr(self.view, 'queryset', None), 'model', None) # parse the path without depending on uritemplate for variable in self.variable_regex.findall(path): description = '' schema = {'type': 'string'} if variable == 'id': schema['type'] = 'integer' elif variable == 'slug': schema['pattern'] = '^[-a-zA-Z0-9_]+$' description = 'The slug of the series.' if model is not None: field = model._meta.get_field(variable) if field is not None: if field.primary_key: description = get_pk_description(model, field) elif field.help_text: description = force_str(field.help_text) parameters.append({ 'name': variable, 'in': 'path', 'required': True, 'description': description, 'schema': schema }) return parameters
def get_custom_path_parameters(self, path, method): """ Return a list of parameters from templated path variables. """ model = getattr(self.view.__class__, 'model_class', None) parameters = [] for variable in uritemplate.variables(path): description = '' if model is not None: # Attempt to infer a field description if possible. try: model_field = model._meta.get_field(variable) except Exception: model_field = None if model_field is not None and model_field.help_text: description = force_text(model_field.help_text) elif model_field is not None and model_field.primary_key: description = get_pk_description(model, model_field) parameter = { "name": variable, "in": "path", "required": True, "description": description, 'schema': { 'type': 'string' }, } parameters.append(parameter) return parameters
def get_custom_path_parameters(self, path, method): """ Return a list of parameters from templated path variables. """ model = getattr(self.view.__class__, 'model_class', None) parameters = [] for variable in uritemplate.variables(path): if '/stats' in path: parameter = { "name": "var", "in": "path", "required": False, "description": ("Timestamp URL. Example:\n- \"/timestamp_start:" "1603801749.315451\"\n- \"/timestamp_start:1603801749." "315451-timestamp_end:1603801850.463\""), 'schema': { 'type': 'string' }, } parameters.append(parameter) continue description = '' if model is not None: # Attempt to infer a field description if possible. try: model_field = model._meta.get_field(variable) except Exception: model_field = None if model_field is not None and model_field.help_text: description = force_text(model_field.help_text) elif model_field is not None and model_field.primary_key: description = get_pk_description(model, model_field) parameter = { "name": variable, "in": "path", "required": True, "description": description, 'schema': { 'type': 'string' }, } parameters.append(parameter) return parameters
def _resolve_path_parameters(self, variables): model = getattr(getattr(self.view, 'queryset', None), 'model', None) parameters = [] for variable in variables: schema = build_basic_type(OpenApiTypes.STR) description = '' resolved_parameter = resolve_regex_path_parameter( self.path_regex, variable, self.map_renderers('format'), ) if resolved_parameter: schema = resolved_parameter['schema'] elif not model: warn( f'could not derive type of path parameter "{variable}" because because it ' f'is untyped and {self.view.__class__} has no queryset. consider adding a ' f'type to the path (e.g. <int:{variable}>) or annotating the parameter ' f'type with @extend_schema. defaulting to "string".') else: try: model_field = model._meta.get_field(variable) schema = self._map_model_field(model_field, direction=None) # strip irrelevant meta data irrelevant_field_meta = [ 'readOnly', 'writeOnly', 'nullable', 'default' ] schema = { k: v for k, v in schema.items() if k not in irrelevant_field_meta } if 'description' not in schema and model_field.primary_key: description = get_pk_description(model, model_field) except django_exceptions.FieldDoesNotExist: warn( f'could not derive type of path parameter "{variable}" because ' f'model "{model}" did contain no such field. consider annotating ' f'parameter with @extend_schema. defaulting to "string".' ) parameters.append( build_parameter_type(name=variable, location=OpenApiParameter.PATH, description=description, schema=schema)) return parameters
def _resolve_path_parameters(self, variables): """ Resolve path parameters. Extended to omit undesired warns. """ model = getattr(getattr(self.view, "queryset", None), "model", None) parameters = [] for variable in variables: schema = build_basic_type(OpenApiTypes.STR) description = "" resolved_parameter = resolve_django_path_parameter( self.path_regex, variable, self.map_renderers("format"), ) if not resolved_parameter: resolved_parameter = resolve_regex_path_parameter( self.path_regex, variable) if resolved_parameter: schema = resolved_parameter["schema"] elif model: try: model_field = model._meta.get_field(variable) schema = self._map_model_field(model_field, direction=None) # strip irrelevant meta data irrelevant_field_meta = [ "readOnly", "writeOnly", "nullable", "default" ] schema = { k: v for k, v in schema.items() if k not in irrelevant_field_meta } if "description" not in schema and model_field.primary_key: description = get_pk_description(model, model_field) except FieldDoesNotExist: pass parameters.append( build_parameter_type( name=variable, location=OpenApiParameter.PATH, description=description, schema=schema, )) return parameters
def _resolve_path_parameters(self, variables): model = getattr(getattr(self.view, 'queryset', None), 'model', None) parameters = [] for idx, variable in enumerate(variables): schema = build_basic_type(OpenApiTypes.STR) description = '' required = True resolved_parameter = resolve_regex_path_parameter(self.path_regex, variable) if resolved_parameter: schema, required = resolved_parameter['schema'], resolved_parameter['required'] elif not model: warn( f'could not derive type of path parameter "{variable}" because ' f'{self.view.__class__} has no queryset. consider annotating the ' f'parameter type with @extend_schema. defaulting to "string".' ) else: try: model_field = model._meta.get_field(variable) schema = self._map_model_field(model_field, direction=None) # strip irrelevant meta data irrelevant_field_meta = ['readOnly', 'writeOnly', 'nullable', 'default'] schema = {k: v for k, v in schema.items() if k not in irrelevant_field_meta} if 'description' not in schema and model_field.primary_key: description = get_pk_description(model, model_field) except django_exceptions.FieldDoesNotExist: warn( f'could not derive type of path parameter "{variable}" because ' f'model "{model}" did contain no such field. consider annotating ' f'parameter with @extend_schema. defaulting to "string".' ) parameters.append({ "name": variable, "in": "path", "required": required, "description": description, 'schema': schema, }) return parameters
def _resolve_path_parameters(self, variables): parameters = [] for variable in variables: schema = build_basic_type(OpenApiTypes.STR) description = '' resolved_parameter = resolve_regex_path_parameter( self.path_regex, variable, self.map_renderers('format'), ) if resolved_parameter: schema = resolved_parameter['schema'] elif get_view_model(self.view) is None: warn( f'could not derive type of path parameter "{variable}" because because it ' f'is untyped and obtaining queryset from {self.view.__class__} failed. ' f'consider adding a type to the path (e.g. <int:{variable}>) or annotating ' f'the parameter type with @extend_schema. defaulting to "string".' ) else: try: model = get_view_model(self.view) model_field = model._meta.get_field(variable) schema = self._map_model_field(model_field, direction=None) if 'description' not in schema and model_field.primary_key: description = get_pk_description(model, model_field) except django_exceptions.FieldDoesNotExist: warn( f'could not derive type of path parameter "{variable}" because ' f'model "{model}" did contain no such field. consider annotating ' f'parameter with @extend_schema. defaulting to "string".' ) parameters.append( build_parameter_type(name=variable, location=OpenApiParameter.PATH, description=description, schema=schema)) return parameters
def _resolve_path_parameters(self, variables): model = getattr(getattr(self.view, 'queryset', None), 'model', None) parameters = [] for variable in variables: schema = build_basic_type(OpenApiTypes.STR) description = '' if not model: warn( f'could not derive type of path parameter "{variable}" because ' f'{self.view.__class__} has no queryset. consider annotating the ' f'parameter type with @extend_schema. defaulting to "string".' ) else: try: model_field = model._meta.get_field(variable) schema = self._map_model_field(model_field) if model_field.help_text: description = force_str(model_field.help_text) elif model_field.primary_key: description = get_pk_description(model, model_field) except django_exceptions.FieldDoesNotExist: warn( f'could not derive type of path parameter "{variable}" because ' f'model "{model}" did contain no such field. consider annotating ' f'parameter with @extend_schema. defaulting to "string".' ) parameters.append({ "name": variable, "in": "path", "required": True, "description": description, 'schema': schema, }) return parameters
def get_path_parameters(self, path, view_cls): """Return a list of Parameter instances corresponding to any templated path variables. :param str path: templated request path :param type view_cls: the view class associated with the path :return: path parameters :rtype: list[openapi.Parameter] """ parameters = [] queryset = get_queryset_from_view(view_cls) for variable in sorted(uritemplate.variables(path)): model, model_field = get_queryset_field(queryset, variable) attrs = get_basic_type_info(model_field) or { 'type': openapi.TYPE_STRING } if getattr( view_cls, 'lookup_field', None) == variable and attrs['type'] == openapi.TYPE_STRING: attrs['pattern'] = getattr(view_cls, 'lookup_value_regex', attrs.get('pattern', None)) if model_field and getattr(model_field, 'help_text', False): description = model_field.help_text elif model_field and getattr(model_field, 'primary_key', False): description = get_pk_description(model, model_field) else: description = None field = openapi.Parameter(name=variable, description=force_real_str(description), required=True, in_=openapi.IN_PATH, **attrs) parameters.append(field) return parameters
def _get_path_parameters(self, path): """ Return a list of parameters from templated path variables. """ assert ( uritemplate ), "`uritemplate` must be installed for OpenAPI schema support." model = getattr(getattr(self.view, "queryset", None), "model", None) parameters = [] for variable in uritemplate.variables(path): description = "" if model is not None: # TODO: test this. # Attempt to infer a field description if possible. try: model_field = model._meta.get_field(variable) except Exception: model_field = None if model_field is not None and model_field.help_text: description = force_str(model_field.help_text) elif model_field is not None and model_field.primary_key: description = get_pk_description(model, model_field) parameter = { "name": variable, "in": "path", "required": True, "description": description, "schema": { "type": "string", }, # TODO: integer, pattern, ... } parameters.append(parameter) return parameters