How to include the JSON Schema definition of a JSONField inside the OpenAPI schema generated by drf-spectacular?
Asked Answered
A

0

6

By default, when using drf-spectacular to generate the OpenAPI schema, the JSONField will be typed as object or array. However, I have a JSON Field which is validated against a JSON Schema and I'd like this JSON Schema to be included on the OpenAPI schema.

I couldn't find this specific scenario described on the docs of drf-spectacular, but however, I tried modifying it using @extend_schema but with no success.

The model looks like this:

Car(models.Model):
    name = models.CharField()
    data = models.JSONField("Car Data", validators=[JSONSchemaValidator(limit_value=my_schema)])

On the generated OpenAPI schema, this is represented as:

data:
    type: object
    additionalProperties: {}
    nullable: true
    title: Car Data

I'm not including a sample on how it should look because I'm seeking answers where this is possible in some pre-defined way.

Acanthopterygian answered 29/11, 2022 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.