Skip to content

Commit

Permalink
Avoid invalid usage of readOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
axnsan12 committed Aug 9, 2018
1 parent 608abaf commit 9b56e1a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/drf_yasg/inspectors/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def make_schema_definition():

definitions = self.components.with_scope(openapi.SCHEMA_DEFINITIONS)
actual_schema = definitions.setdefault(ref_name, make_schema_definition)
actual_schema._remove_read_only()

actual_serializer = get_serializer_class(getattr(actual_schema, '_serializer', None))
this_serializer = get_serializer_class(field)
if actual_serializer and actual_serializer != this_serializer: # pragma: no cover
Expand Down
19 changes: 14 additions & 5 deletions src/drf_yasg/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,6 @@ def __init__(self, name, in_, description=None, required=None, schema=None,
:param default: default value if the parameter is not provided; must conform to parameter type
"""
super(Parameter, self).__init__(**extra)
if (not schema and not type) or (schema and type):
raise AssertionError("either schema or type are required for Parameter object!")
self.name = name
self.in_ = in_
self.description = description
Expand All @@ -401,6 +399,10 @@ def __init__(self, name, in_, description=None, required=None, schema=None,
self.items = items
self.default = default
self._insert_extras__()
if (not schema and not type) or (schema and type):
raise AssertionError("either schema or type are required for Parameter object (not both)!")
if schema and isinstance(schema, Schema):
schema._remove_read_only()
if self['in'] == IN_PATH:
# path parameters must always be required
assert required is not False, "path parameter cannot be optional"
Expand Down Expand Up @@ -465,6 +467,11 @@ def __init__(self, title=None, description=None, type=None, format=None, enum=No
if pattern and type != TYPE_STRING:
raise AssertionError("pattern can only be used when type is string")

def _remove_read_only(self):
# readOnly is only valid for Schemas inside another Schema's properties;
# when placing Schema elsewhere we must take care to remove the readOnly flag
self.pop('readOnly', '')


class _Ref(SwaggerDict):
ref_name_re = re.compile(r"#/(?P<scope>.+)/(?P<name>[^/]+)$")
Expand Down Expand Up @@ -497,12 +504,12 @@ def resolve(self, resolver):
ref_match = self.ref_name_re.match(self.ref)
return resolver.get(ref_match.group('name'), scope=ref_match.group('scope'))

def __setitem__(self, key, value, **kwargs):
def __setitem__(self, key, value):
if key == "$ref":
return super(_Ref, self).__setitem__(key, value, **kwargs)
return super(_Ref, self).__setitem__(key, value)
raise NotImplementedError("only $ref can be set on Reference objects (not %s)" % key)

def __delitem__(self, key, **kwargs):
def __delitem__(self, key):
raise NotImplementedError("cannot delete property of Reference object")


Expand Down Expand Up @@ -560,6 +567,8 @@ def __init__(self, description, schema=None, examples=None, **extra):
self.schema = schema
self.examples = examples
self._insert_extras__()
if schema and isinstance(schema, Schema):
schema._remove_read_only()


class ReferenceResolver(object):
Expand Down
1 change: 0 additions & 1 deletion tests/reference.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,6 @@ definitions:
title: Foo
type: string
minLength: 1
readOnly: true
MethodFieldExample:
title: Hint example
type: object
Expand Down

0 comments on commit 9b56e1a

Please sign in to comment.