Skip to content

Commit

Permalink
add format query parameter #110
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Jul 17, 2020
1 parent a92acc2 commit 67d638d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
40 changes: 21 additions & 19 deletions drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ def _process_override_parameters(self):
warn(f'could not resolve parameter annotation {parameter}. skipping.')
return result

def _get_format_parameters(self):
parameters = []
formats = self.map_renderers('format')
if api_settings.URL_FORMAT_OVERRIDE and len(formats) > 1:
parameters.append(build_parameter_type(
name=api_settings.URL_FORMAT_OVERRIDE,
schema=build_basic_type(OpenApiTypes.STR),
location=OpenApiParameter.QUERY,
enum=formats
))
return parameters

def _get_parameters(self):
def dict_helper(parameters):
return {(p['name'], p['in']): p for p in parameters}
Expand All @@ -162,6 +174,7 @@ def dict_helper(parameters):
**dict_helper(self._resolve_path_parameters(path_variables)),
**dict_helper(self._get_filter_parameters()),
**dict_helper(self._get_pagination_parameters()),
**dict_helper(self._get_format_parameters()),
}
# override/add @extend_schema parameters
for key, parameter in override_parameters.items():
Expand Down Expand Up @@ -272,7 +285,7 @@ def _resolve_path_parameters(self, variables):
description = ''

resolved_parameter = resolve_regex_path_parameter(
self.path_regex, variable, self.map_formats(),
self.path_regex, variable, self.map_renderers('format'),
)

if resolved_parameter:
Expand Down Expand Up @@ -738,23 +751,12 @@ def _get_paginator(self):
def map_parsers(self):
return list(map(attrgetter('media_type'), self.view.parser_classes))

def map_renderers(self):
media_types = []
for renderer in self.view.renderer_classes:
# BrowsableAPIRenderer not relevant to OpenAPI spec
if renderer == renderers.BrowsableAPIRenderer:
continue
media_types.append(renderer.media_type)
return media_types

def map_formats(self):
formats = set()
for renderer in self.view.renderer_classes:
# BrowsableAPIRenderer not relevant to OpenAPI spec
if renderer == renderers.BrowsableAPIRenderer:
continue
formats.add(renderer.format)
return list(formats)
def map_renderers(self, attribute):
assert attribute in ['media_type', 'format']
return list(dict.fromkeys([
getattr(r, attribute) for r in self.view.renderer_classes
if r != renderers.BrowsableAPIRenderer
]))

def _get_serializer(self):
view = self.view
Expand Down Expand Up @@ -900,7 +902,7 @@ def _get_response_for_code(self, serializer):

return {
'content': {
mt: {'schema': schema} for mt in self.map_renderers()
mt: {'schema': schema} for mt in self.map_renderers('media_type')
},
# Description is required by spec, but descriptions for each response code don't really
# fit into our model. Description is therefore put into the higher level slots.
Expand Down
4 changes: 2 additions & 2 deletions drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ def build_parameter_type(
schema['explode'] = explode
if style is not None:
schema['style'] = style
if enum is not None:
schema['schema']['enum'] = enum
if enum:
schema['schema']['enum'] = sorted(enum)
return schema


Expand Down
2 changes: 1 addition & 1 deletion tests/test_extend_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ paths:
schema:
type: boolean
enum:
- true
- false
- true
description: creation will be in the sandbox
tags:
- custom_tag
Expand Down
7 changes: 7 additions & 0 deletions tests/test_i18n.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ paths:
- YAML: application/vnd.oai.openapi
- JSON: application/vnd.oai.openapi+json
parameters:
- in: query
name: format
schema:
type: string
enum:
- json
- yaml
- in: query
name: lang
schema:
Expand Down

0 comments on commit 67d638d

Please sign in to comment.