Hello,
One of the field of my serializer class is a JSONField and I am wondering if I could specify the schema that the data of this JSONField are expected to follow?
I am already having a validation code ensuring that the data meet the json schema (using https://json-schema.org/understanding-json-schema and the jsonschema schema library), but I would also like to have it documented in my swagger view.
Is there a workaround to provide this information to drf-yasg
thanks in advance,
Loic
You could use a serializer for your JSONField, then the documentation would be automatically generated.
Hum.... Either I don't understand your answer, or you don't get my question.... ;-)
Let say I have a JSonField in my model that is call "extra".
I create a serializer for my model, and I defined the associated serializer for my extra object in it:
extra= serializers.JSONField(read_only=False)
(this is what you are suggesting if I am not wrong)
Then, indeed, I can see in the swagger schema that "extra" is documented.

Great.... BUT it is documented as string, while I would like to enforce the expected structure of the json element within the exta field itself. Typically, say that extra should at least have two required fields of type number named extra1 and extra2. How can I specify this to drf-yasg.
An example would be nice.
Thanks in advance,
Loic
PS: Besides that this lib is awesome, thanks for developing it!
I think you could provide the expected structure to drf_yasg by subclassing JSONField and using Meta.swagger_schema_fields .
Like this (may not work as-is but you get the idea):
from rest_framework import serializers
from drf_yasg import openapi
class ExtraJSONField(serializers.JSONField):
class Meta:
swagger_schema_fields = {
"type": openapi.TYPE_OBJECT,
"required": ["extra1", "extra2"],
"properties": {
"extra1": {
"title": "Extra 1",
"description": "First extra field",
"type": openapi.TYPE_INTEGER,
},
"extra2": {
"title": "Extra 2",
"description": "Second extra field",
"type": openapi.TYPE_INTEGER,
},
}
}
# Then in the parent serializer:
class HasExtra(serializers.Serializer):
extra = ExtraJSONField()
Which would give the following representation in openapi format:
"HasExtra": {
"type": "object",
"properties": {
"extra": {
"title": "extra",
"type": "object",
"required": [
"extra1",
"extra2"
],
"properties": {
"extra1": {
"title": "Extra 1",
"description": "First extra field",
"type": "integer"
},
"extra2": {
"title": "Extra 2",
"description": "Second extra field",
"type": "integer"
}
}
}
}
}
Is that what you wanted to do ?
Hi @etene
This is exactly the example I was looking for,
This does the job, thank you!
You're welcome !
Most helpful comment
Hum.... Either I don't understand your answer, or you don't get my question.... ;-)
Let say I have a JSonField in my model that is call "extra".
I create a serializer for my model, and I defined the associated serializer for my extra object in it:
extra= serializers.JSONField(read_only=False)(this is what you are suggesting if I am not wrong)
Then, indeed, I can see in the swagger schema that "extra" is documented.

Great.... BUT it is documented as string, while I would like to enforce the expected structure of the json element within the exta field itself. Typically, say that extra should at least have two required fields of type number named extra1 and extra2. How can I specify this to drf-yasg.
An example would be nice.
Thanks in advance,
Loic
PS: Besides that this lib is awesome, thanks for developing it!