Skip to content

Commit

Permalink
Improve schema generation for DictField and HStoreField
Browse files Browse the repository at this point in the history
Make `serializers.DictField` and `serializers.HStoreField` generate objects with `additionalProperties` defined by their `.child`.
  • Loading branch information
beruic authored Jan 16, 2025
1 parent 4a1d773 commit d9dc01c
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions rest_framework/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,17 @@ def map_field(self, field):
'format': 'binary'
}

# Also handles serializers.HStoreField,
if isinstance(field, serializers.DictField):
return {
"type": "object",
"additionalProperties": self.map_field(field.child),
}

# Simplest cases, default to 'string' type:
FIELD_CLASS_SCHEMA_TYPE = {
serializers.BooleanField: 'boolean',
serializers.JSONField: 'object',
serializers.DictField: 'object',
serializers.HStoreField: 'object',
}
return {'type': FIELD_CLASS_SCHEMA_TYPE.get(field.__class__, 'string')}

Expand Down

0 comments on commit d9dc01c

Please sign in to comment.