bhch / django-jsonform

A better, user-friendly JSON editing form field for Django admin. Also supports Postgres ArrayField.
https://django-jsonform.rtfd.io
BSD 3-Clause "New" or "Revised" License
352 stars 34 forks source link

When the form has initial data and the schema is updated, the form throws an error #131

Closed bhch closed 12 months ago

bhch commented 1 year ago

Steps to reproduce:

Step 1. Original schema:

{
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "title": "Example",
      "oneOf": [
        {
          "title": "Table",
          "properties": {
            "type": {
              "type": "string",
              "default": "table",
              "widget": "hidden"
            },
            "columns": {
              "title": "Columns",
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "title": {
                    "type": "string",
                    "title": "Header"
                  },
                  "type": {
                    "type": "string",
                    "title": "Type",
                    "choices": [
                      "string",
                      "number",
                      "integer",
                      "boolean"
                    ],
                    "default": "string",
                    "required": true
                  },
                  "default": {
                    "type": "array",
                    "title": "Prepopulate rows (EN)",
                    "items": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

Step 2: Add some data and save the object.

Step 3: Update the schema to this:

{
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "title": "Example",
      "oneOf": [
        {
          "title": "Table",
          "properties": {
            "type": {
              "type": "string",
              "default": "table",
              "widget": "hidden"
            },
            "columns": {
              "title": "Columns",
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "title": {
                    "type": "string",
                    "title": "Header"
                  },
                  "type": {
                    "type": "string",
                    "title": "Type",
                    "choices": [
                      "string",
                      "number",
                      "integer",
                      "boolean"
                    ],
                    "default": "string",
                    "required": true
                  },
                  "default": {
                    "type": "array",
                    "title": "Prepopulate rows (EN)",
                    "items": {
                      "type": "string"
                    }
                  },
                  "default_de": {
                    "type": "array",
                    "title": "Prepopulate rows (DE)",
                    "items": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

Step 4: Refresh page

The admin widget doesn't render with the following message:

Screenshot 2023-11-28 at 10 02 20 AM

I was using 2.17.0 earlier but upgraded to 2.21.0 but the problem still persists.

Originally posted by @sgocg in https://github.com/bhch/django-jsonform/issues/101#issuecomment-1829378697

bhch commented 12 months ago

@sgocg This has been fixed in v2.21.1. Please upgrade and test it out.

Thank you for reporting the issue.


UPDATE: Just released a new version v.2.21.2 (previous release had a small mistake left in the logic).

sgocg commented 11 months ago

Amazing, thanks for the fix! I tested it locally and it works quite well.