-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Closed
Description
Consider this useless serializer:
class MySerializer(serializers.Serializer):
random_number = serializers.SerializerMethodField()
def get_random_number(self):
# Chosen by fair dice roll.
# Guaranteed to be random.
return 4
Assuming you added this serializer in an endpoint and generated the OpenAPI schema, the serializer would look like this:
properties:
random_number:
type: string
required:
- random_number
The issue here is there is no way to tell DRF to use type: number
here. I thought of a few possibilities:
-
An
output_field
attribute, likeserializers.SerializerMethodField(output_field=serializers.IntegerField())
, similarly to Django's Query Expressions -
A decorator, like
openapi_type(serializers.SerializerMethodField(), serializers.IntegerField())
-
An attribute, similarly to setting descriptions on admin actions:
random_number = serializers.SerializerMethodField() def get_random_number(self): return 4 get_random_number.output_field = serializers.IntegerField()
-
Simply something DRF does not support, which probably should be documented somewhere as a possible caveat.
sigbjornlo, Dawidpol, piec, tevariou, charanjit-singh and 2 moresigbjornlo and daxaxelrod