Skip to content

Commit

Permalink
non-blank string enforcement for parameters #282
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Oct 15, 2021
1 parent 7222466 commit bdea0c2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,23 @@ def _process_override_parameters(self):
style=parameter.style,
explode=parameter.explode,
default=parameter.default,
allow_blank=parameter.allow_blank,
examples=build_examples_list(parameter.examples),
extensions=parameter.extensions,
)
elif is_basic_serializer(parameter):
# explode serializer into separate parameters. defaults to QUERY location
parameter = force_instance(parameter)
mapped = self._map_serializer(parameter, 'request')
for property_name, property_schema in mapped['properties'].items():
field = parameter.fields.get(property_name)
result[property_name, OpenApiParameter.QUERY] = build_parameter_type(
name=property_name,
schema=property_schema,
description=property_schema.pop('description', None),
location=OpenApiParameter.QUERY,
required=property_name in mapped.get('required', []),
allow_blank=getattr(field, 'allow_blank', True),
required=field.required,
)
else:
warn(f'could not resolve parameter annotation {parameter}. Skipping.')
Expand Down Expand Up @@ -1272,6 +1276,7 @@ def _get_response_headers_for_code(self, status_code) -> dict:
style=parameter.style,
explode=parameter.explode,
default=parameter.default,
allow_blank=parameter.allow_blank,
examples=build_examples_list(parameter.examples),
extensions=parameter.extensions,
)
Expand Down
3 changes: 3 additions & 0 deletions drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def build_parameter_type(
explode=None,
style=None,
default=None,
allow_blank=True,
examples=None,
extensions=None,
):
Expand All @@ -325,6 +326,8 @@ def build_parameter_type(
schema['schema']['enum'] = sorted(enum)
if default is not None and 'default' not in irrelevant_field_meta:
schema['schema']['default'] = default
if not allow_blank and schema['schema'].get('type') == 'string':
schema['schema']['minLength'] = schema['schema'].get('minLength', 1)
if examples:
schema['examples'] = examples
if extensions:
Expand Down
2 changes: 2 additions & 0 deletions drf_spectacular/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def __init__(
style: Optional[str] = None,
explode: Optional[bool] = None,
default: Any = None,
allow_blank: bool = True,
examples: Optional[List[OpenApiExample]] = None,
extensions: Optional[Dict[str, Any]] = None,
exclude: bool = False,
Expand All @@ -170,6 +171,7 @@ def __init__(
self.style = style
self.explode = explode
self.default = default
self.allow_blank = allow_blank
self.examples = examples or []
self.extensions = extensions
self.exclude = exclude
Expand Down
1 change: 1 addition & 0 deletions tests/test_extend_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class QuerySerializer(serializers.Serializer):
choices=['a', 'b', 'c'],
default=['a'],
)
tag = serializers.CharField(required=False)


class ErrorDetailSerializer(serializers.Serializer):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_extend_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ paths:
minimum: 1
description: filter by rating stars
required: true
- in: query
name: tag
schema:
type: string
minLength: 1
tags:
- doesitall
security:
Expand Down Expand Up @@ -408,6 +413,8 @@ components:
$ref: '#/components/schemas/OrderByEnum'
default:
- a
tag:
type: string
required:
- stars
securitySchemes:
Expand Down

0 comments on commit bdea0c2

Please sign in to comment.