Unleash / unleash

Open-source feature management solution built for developers.
https://getunleash.io
Apache License 2.0
11.41k stars 720 forks source link

Update a Segment via API call #7987

Open theomeuh opened 2 months ago

theomeuh commented 2 months ago

Discussed in https://github.com/orgs/Unleash/discussions/7980

Originally posted by **theomeuh** August 26, 2024 I'm trying to migrate some data we have in Unleash and I need to automatically update Segment via API. I'm referring to this endpoint: https://docs.getunleash.io/reference/api/unleash/update-segment ### Context It says: "Any fields not specified will be left untouched", but if I try to PUT an empty body, I got a 400 Bad Request `{"id":"1dd37e9f-2b75-458b-b7ca-1424e0561479","name":"BadDataError","message":"Request validation failed: your request body or params contain invalid data. Refer to the `details` list for more information.","details":[{"message":"\"name\" is required."}]}` Then let's add a "dummy name".. I got the same message but this time, it is the field "constraints" which is apparently missing. My question: is the doc up-to-date ? It really looks like a PUT (like the endpoint verb says) and not a PATCH (like the description says), do you confirm ? PS: what I really want is to update "constraints" but unfortunately, I have the "name is required" error when I try to just push a payload with only "constraints"

Current behavior

A PUT requests on api/admin/segments/:id with in body a json containing only the key constraints returns a 400, saying the field name is required

Expected behavior

A PUT requests on api/admin/segments/:id with in body a json containing only the key constraints must return a 200 (and update the constraints field)

gastonfournier commented 2 months ago

Thanks @theomeuh we'll look into it, we have somewhat related work we're planning to take on soon, and I included this as part of that backlog, we'll keep you posted on this ticket.

nunogois commented 2 months ago

Hi @theomeuh,

Thank you for bringing up this issue. We’ve reviewed your concerns and I’d like to provide some clarification and updates.

You’re correct in pointing out that the behavior of the endpoint can be confusing, especially regarding how it handles PUT requests. While the API documentation indicates that unspecified fields will remain untouched (which would align more with PATCH behavior), the endpoint currently requires name and constraints to be present. Other fields should be left untouched, however there's also an edge case for the project property, where it is reset to null if not present.

https://github.com/Unleash/unleash/pull/8085 doesn’t really fix the issue, but at least it includes the request body schema, which is the same as the POST endpoint.

In the meantime, for your specific use case of updating only the constraints, a potential workaround would be to include the name to avoid the 400 error, even if you’re not intending to change it. Also keep in mind that you'll need to provide the project in case they are project-specific segments, as mentioned in the edge case above.

We acknowledge that the current behavior is far from great, and we'll take this into consideration when we discuss the future of our API and respective OpenAPI spec.