Skip to content

Commit

Permalink
bugfix properties with $ref component. closes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Mar 27, 2020
1 parent 9daf0ad commit e3c2edb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
7 changes: 6 additions & 1 deletion drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ def _map_basic_serializer(self, method, serializer):
required.append(field.field_name)

schema = self._map_serializer_field(method, field)

if field.read_only:
schema['readOnly'] = True
if field.write_only:
Expand All @@ -583,9 +584,13 @@ def _map_basic_serializer(self, method, serializer):
schema['default'] = field.default
if field.help_text:
schema['description'] = str(field.help_text)

self._map_field_validators(field, schema)

# sibling entries to $ref will be ignored as it replaces itself and its context with
# the referenced object. Wrap it in a separate context.
if '$ref' in schema and len(schema) > 1:
schema = {'allOf': [{'$ref': schema.pop('$ref')}], **schema}

properties[field.field_name] = schema

result = {
Expand Down
6 changes: 4 additions & 2 deletions tests/test_basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ components:
$ref: '#/components/schemas/Song'
readOnly: true
single:
$ref: '#/components/schemas/Song'
allOf:
- $ref: '#/components/schemas/Song'
readOnly: true
title:
type: string
Expand Down Expand Up @@ -254,7 +255,8 @@ components:
$ref: '#/components/schemas/Song'
readOnly: true
single:
$ref: '#/components/schemas/Song'
allOf:
- $ref: '#/components/schemas/Song'
readOnly: true
title:
type: string
Expand Down
3 changes: 2 additions & 1 deletion tests/test_extend_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ components:
format: date-time
readOnly: true
field_j:
$ref: '#/components/schemas/Inline'
allOf:
- $ref: '#/components/schemas/Inline'
readOnly: true
field_k:
type: array
Expand Down

0 comments on commit e3c2edb

Please sign in to comment.