Partial update of single-component field results in invalid data

Hi!
I’m developing an app with Strapi V4. I have a content-type that has a “single” component that in turn has two fields. When I’m updating individual fields of the component via REST API all its other fields get nulled. Even those marked as required. Here’s an example using the familiar Restaurant model:

// A valid Restaurant instance might look something like this:
{
  id: 2,
  name: "Kimchi Kitchen",
  type: "asian",
  // This is a single component.
  address: {
    id: 5,
    street: "5 Ave",
    city: "Haarlem"
  }
}

When I update this Restaurant with

{
  data: {
    type: "italian"
  }
}

Only the type is updated and all other fields keep their value. That’s expected.

However, when I update it with

{
  data: {
    address: {
      street "10 Ave",
    }
  }
}

I get a Restaurant, where address.city === null. Also, even if the city field is marked as required no errors are thrown and the update gets through resulting in invalid data.

Is there a way to update components partially? If not can I at least prevent API clients from creating invalid data? Shall I report that as a bug?

So I submitted this as bug on GitHub. Partial update of component field results in invalid data · Issue #14743 · strapi/strapi · GitHub

Turns out, partial update works, but the client needs to pass the id of the component just as when updating the multi-component fields. Eg.

{
  data: {
    address: {
      id: 1,
      street "10 Ave",
    }
  }
}

Note however, that it’s still possible for clients to create invalid data in your DB. See the issue for discussion about how/if this will be fixed.

1 Like

Did you find any solution for this