mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-28 17:09:59 +03:00
improve decision making logic
This commit is contained in:
parent
6eb16481fa
commit
0a6322e7d5
|
@ -212,20 +212,21 @@ class AutoSchema(ViewInspector):
|
||||||
return paginator.get_schema_operation_parameters(view)
|
return paginator.get_schema_operation_parameters(view)
|
||||||
|
|
||||||
def _map_choicefield(self, field):
|
def _map_choicefield(self, field):
|
||||||
type = None
|
|
||||||
choices = list(field.choices)
|
choices = list(field.choices)
|
||||||
if all(isinstance(choice, int) for choice in choices):
|
|
||||||
type = 'integer'
|
|
||||||
if all(isinstance(choice, float) or isinstance(choice, Decimal) for choice in choices):
|
|
||||||
type = 'number'
|
|
||||||
if all(isinstance(choice, str) for choice in choices):
|
|
||||||
type = 'string'
|
|
||||||
if all(isinstance(choice, bool) for choice in choices):
|
if all(isinstance(choice, bool) for choice in choices):
|
||||||
# Here we can not use `boolean` as type because choicefield expects `True` and `False` which is different
|
# Here we can not use `boolean` as type because choicefield expects `True` and `False` which is different
|
||||||
# from the `true` and `false` of OpenAPI Specification.
|
# from the `true` and `false` of OpenAPI Specification.
|
||||||
# Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#format
|
# Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#format
|
||||||
type = 'string'
|
type = 'string'
|
||||||
choices = list(map(lambda choice: str(choice), choices))
|
choices = list(map(lambda choice: str(choice), choices))
|
||||||
|
elif all(isinstance(choice, int) for choice in choices):
|
||||||
|
type = 'integer'
|
||||||
|
elif all(isinstance(choice, (int, float, Decimal)) for choice in choices): # `number` includes `integer`
|
||||||
|
type = 'number'
|
||||||
|
elif all(isinstance(choice, str) for choice in choices):
|
||||||
|
type = 'string'
|
||||||
|
else:
|
||||||
|
type = None
|
||||||
|
|
||||||
mapping = {
|
mapping = {
|
||||||
# The value of `enum` keyword MUST be an array and SHOULD be unique.
|
# The value of `enum` keyword MUST be an array and SHOULD be unique.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user