Skip to content

Fix request body construction to only send non-nil values #384

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

nicknisi
Copy link
Contributor

@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

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.

1 participant