Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update a Segment via API call #7987

Open
theomeuh opened this issue Aug 27, 2024 Discussed in #7980 · 2 comments
Open

Update a Segment via API call #7987

theomeuh opened this issue Aug 27, 2024 Discussed in #7980 · 2 comments
Assignees

Comments

@theomeuh
Copy link

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
Copy link
Contributor

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
Copy link
Member

nunogois commented Sep 4, 2024

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.

#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.

nunogois added a commit that referenced this issue Sep 5, 2024
https://linear.app/unleash/issue/2-2592/updateimprove-a-segment-via-api-call

Related to #7987

This does not make the endpoint necessarily better - It's still a PUT
that acts as a PUT in some ways (expects specific required fields to be
present, resets the project to `null` if it's not included in the body)
and a PATCH in others (ignores most fields if they're not included in
the body). We need to have a more in-depth discussion about developing
long-term strategies for our API and respective OpenAPI spec.

However this at least includes the proper schema for the request body,
which is slightly better than before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

3 participants