Skip to content

Conversation

@nicknisi
Copy link
Member

@nicknisi nicknisi commented Jul 7, 2025

Summary

Fix multiple methods in the UserManagement module to use .compact when building request bodies, preventing unnecessary nil values from being sent to the API.

This addresses a customer-reported issue where update_user was unintentionally nullifying fields that weren't explicitly provided. The fix has been extended to all similar methods for consistency.

What was the problem?

When calling methods like update_user with only some parameters:

WorkOS::UserManagement.update_user(
  id: 'user_123',
  email_verified: true
)

The method was sending ALL parameters (including nil ones) to the API:

{
  email: nil,
  first_name: nil, 
  last_name: nil,
  email_verified: true,
  external_id: nil,
  # ... more nil values
}

This caused the API to explicitly nullify fields that users didn't intend to change.

What's the solution?

Add .compact to remove nil values before sending the request:

{
  email_verified: true  # Only non-nil values sent
}

Methods Fixed

  • update_user - Critical fix for user-reported issue
  • create_user - Consistency and best practice
  • send_invitation - Remove unnecessary nil invitation parameters
  • enroll_auth_factor - Remove unnecessary nil TOTP parameters
  • create_organization_membership - Remove unnecessary nil role_slug
  • create_magic_auth - Remove unnecessary nil invitation_token

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Fixed a critical bug in UserManagement module where API requests were unintentionally nullifying unspecified fields by sending nil values. This PR adds .compact to multiple methods' request body construction.

  • Fixed update_user method where customer reported unintended field nullification
  • Added .compact to 5 other methods (create_user, send_invitation, enroll_auth_factor, create_organization_membership, create_magic_auth) for consistency
  • Added test coverage in spec/lib/workos/user_management_spec.rb to verify only non-nil values are sent in requests
  • Follows existing pattern from authorization_url method implementation

2 files reviewed, no comments
Edit PR Review Bot Settings | Greptile

@nicknisi nicknisi requested a review from alisherry July 21, 2025 19:25
Copy link
Contributor

@ericroberts ericroberts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was curious about what if someone intentionally wanted to send a nil value, but it looks like our api for update doesn't accept any nulls

Could potentially be something in the future, but this seems good for what we have now.

@nicknisi nicknisi merged commit ffb578e into main Jul 22, 2025
2 checks passed
@nicknisi nicknisi deleted the nicknisi/update-method-fixes branch July 22, 2025 15:00
@nicknisi nicknisi mentioned this pull request Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants