Skip to content

Unable to use objects with only additionalProperties (no pre-defined fields) within schema #64

@pirate

Description

@pirate

I have a pydantic model that has a field like so:

class MySchemaModel(BaseModel):
    my_mapping: Dict[str, str] = Field(default={})

DEFAULT = MySchemaModel()

class Model(models.Model):
    test_field: MySchemaModel = SchemaField(default=DEFAULT)

This field contains a mapping of str to str and I Want users to be able to add their own entries , but the default contains no pre-defined keys/properties, so it throws an error right now:

Error: Error while creating EditorState: Invalid schema: Schema of type 'object' must have at least one of these keys: ['properties' or 'keys' or 'oneOf' or 'anyOf' or 'allOf']

I think the issue can be fixed by adding an empty properties: {} entry to the schema generation /conversion code before it gets passed to django-jsonform:
MySchemaModel().model_json_schema()

{
    "properties": {
        "my_mapping": {
            "additionalProperties": {
                "type": "string"
            },
            "default": {},
            "title": "My Mapping",
+           "properties": {},
            "type": "object"
        }
    },
    "title": "MySchemaModel",
    "type": "object"
}

I think django-jsonform should also natively support objects with no properties/keys defined if they have additionalProperties set. I commented on a related an issue on their side here: bhch/django-jsonform#144.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions