How can I serialize Django REST framework Serializers?

Viewed 424

Maybe this isn't possible but given a Django Serializer class like this:

class ExampleSerializer(serializers.Serializer):
    name = serializers.CharField(min_length=1, max_length=200)

I'd like there to be a way for the client to get it like a JSON Schema like this (I don't particularly care about the format, OpenAPI would be okay too):

{
  "$id": "https://example.com/example.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "ExampleSerializer",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200
    }
  }
}

The advantage of this is that I don't have to duplicate the schema logic in the client too. I tried to use their CoreAPI which documents endpoints but it seems like in order to use it I'd need to convert all my serializers to use their AutoSchema format. I don't want to do because I already have it in the seralizer format. What's the recommended way to do this? If there isn't one (already asked on Reddit), it'd be relatively easy for me to create a Serializer-to-JSONSchema converter but I can't imagine I'm the first person to have wanted this.

0 Answers
Related