Skip to content

Partial update of component field results in invalid data  #14743

@goodhoko

Description

@goodhoko

Bug report

Required System information

  • Node.js version: 16.16.0
  • Yarn version: 1.22.19
  • Strapi version: 4.3.9
  • Database: Postgres:12.0
  • Operating system: node:16 Docker image.

Describe the bug

Partial update of a "single" component field results in invalid data.

Steps to reproduce the behavior

  1. Create a component with two required fields of any type.
    // /src/components/category/component.json
    {
      "collectionName": "components_category_my-component",
      "info": {
        "displayName": "My Component",
      },
      "options": {},
      "attributes": {
        "field_1": {
          "type": "string",
          "required": true
        },
        "field_2": {
          "type": "string",
          "required": true,
        }
      }
    }
  2. Create a content-type with a non-repeatable component field that uses the component:
    // /src/api/my-type/contnent-types/my-type/shema.json
    {
      "kind": "collectionType",
      "collectionName": "my-type",
      "info": {
        "singularName": "my-type",
        "pluralName": "my-types",
        "displayName": "My Type"
      },
      "options": {
        "draftAndPublish": false
      },
      "pluginOptions": {},
      "attributes": {
        "component_field": {
          "type": "component",
          "repeatable": false,
          "component": "category.my-component",
          "required": true
        }
      }
    }
  3. Allow public role to crate and update the content type:
    image
  4. Create an entry of the content type:
    POST /api/my-types
    {
      data: {
        component_field: {
          field_1: "value",
          field_2: "value"
        }
      }
    }
    
5. Update one of the fields of the component using the REST API:
    ```
    PUT /api/my-types/1?populate=*
    {
      data: {
        component_field: {
          field_1: "new value",
        }
      }
    }
  1. Observe that field_2 gets nulled resulting in invalid entry:
    {
      data: {
        id: 1,
        attributes: {
          component_field: {
            field_1: "new value",
            field_2: null
          }
        },
        meta: {}
      },
      meta: {}
    }
    

Expected behavior

Only field_1 is updated to "new value" and field_2 keeps its value or HTTP 400 is returned stating that field_2 cannot be null.

Additional context

I posted about this on the Strapi forum: https://forum.strapi.io/t/partial-update-of-single-component-field-results-in-invalid-data/23195

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue: discussionA general discussion issueseverity: lowIf the issue only affects a very niche base of users and an easily implemented workaround can solvesource: core:strapiSource is core/strapi packagestalestale-before-2023status: confirmedConfirmed by a Strapi Team member or multiple community members

    Type

    No type

    Projects

    Status

    Fixed/Shipped

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions