0

I am working on a custom schema and got an error saying Object of type Boolean is not JSON serializable on the Swagger documentation page.

import coreapi
import coreschema
from rest_framework.schemas import AutoSchema


class CustomSchema(AutoSchema):
    def get_manual_fields(self, path, method):
        manual_fields = []
        if method == "POST":
            manual_fields.extend(
                [
                    coreapi.Field(
                        "custom_field",
                        required=False,
                        location="form",
                        description="custom field",
                        type=coreschema.Boolean()
                    )
                ]
            )
        return manual_fields

I noticed that using location="query" would not yield the error above, but I need it in the form for Swagger. Is there any workaround? Thanks in advance!

Sara
  • 69
  • 2
  • 9

1 Answers1

0

Not necessarily solving the error above, but here's a workaround that worked for the same purpose:

from rest_framework import serializers
from . import CustomModel


class CustomSerializer(serializers.ModelSerializer):
    custom_field = serializers.BooleanField(write_only=True)

    class Meta:
        model = CustomModel
        fields = "__all__"

Rather than calling schema = CustomSchema() from the views, Call this CustomSerializer and specify a write_only field in the serializer automatically populates the Swagger schema for the POST method.

Sara
  • 69
  • 2
  • 9