Skip to content

Cannot set user attributes to null when updating a user #735

@NamanhTran

Description

@NamanhTran

Checklist

  • I have looked into the Readme and Examples, and have not found a suitable solution or answer.
  • I have looked into the API documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • I agree to the terms within the Auth0 Code of Conduct.

Description

Hi everyone,

I'm encountering an issue when trying to update a user's profile attributes using the Java SDK. According to the Update a User documentation, it states that:

Supported attributes can be unset by supplying null as the value.

When using HTTP clients like cURL or Postman, I can successfully set these attributes (e.g., phone_number) to null. However, when using the Java SDK, I'm unable to achieve the same behavior.

I think the issue lies in the ObjectMapper used in the SDK. By default, Jackson's ObjectMapper does not serialize null values, which prevents them from being included in the request payload.

Here’s the relevant code from the BaseRequestclass:

protected HttpRequestBody createRequestBody() throws IOException {
    if (body == null && parameters.isEmpty()) {
        return null;
    }
    byte[] jsonBody = mapper.writeValueAsBytes(body != null ? body : parameters); // Null values are not serialized here
    return HttpRequestBody.create(CONTENT_TYPE_APPLICATION_JSON, jsonBody);
}

Since ObjectMapper is not configured to include null values (and I understand why it doesn't), attributes that are explicitly set to null are omitted from the request payload. This behavior contradicts the API's ability to unset attributes by passing null.

Reproduction

Can be reproduced easily.

  1. Create a user with a phone number.
  2. Try to remove the user's phone number to a null value.

Additional context

No response

auth0-java version

2.15.0

Java version

17

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis points to a verified bug in the code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions