'drf-spectacular extend_schema_serializer can't do empty value example

I'm using the extend_schema_serializer decorator to document examples of json data that can be sent in a POST request. The problem is, one of the variations/examples requires no data, but the generated swagger doc insists on showing what looks like an auto-generated default with all possible params. This isn't ideal when I'm trying to demonstrate the most simple examples.

I have this for the serializer.

@extend_schema_serializer(
    examples=[
        OpenApiExample(
            'Simple example',
            value={},
            request_only=True,
            response_only=False,
        ),
        OpenApiExample(
            'Single param example',
            value={
                'mult': 3,
            },
            request_only=True,
            response_only=False,
        ),
    ],
)
class RollChartSerializer(serializers.Serializer):
    mult = serializers.IntegerField(required=False, write_only=True)
    input_vars = serializers.DictField(required=False, write_only=True)
    foo_name = serializers.CharField(source="foo", allow_null=True, read_only=True)
    foo_url = ChartAbsoluteUrlField(source="foo.id", allow_null=True, read_only=True)
    results = serializers.StringRelatedField(many=True, read_only=True)
...

Where I have value={}, I've also tried value=None and have tried removing it altogether. In all cases, the 'example value' in swagger showing.

{
  "mult": 0,
  "input_vars": {
    "additionalProp1": "string",
    "additionalProp2": "string",
    "additionalProp3": "string"
  }
}

Is there something I'm missing?

I did notice that the generated redoc docs show null.

For the second example, swagger correctly shows

{
    "mult": 3
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source