redis/jedis · error · IllegalArgumentException

HashImport fields must not contain duplicates

Error message

HashImport fields must not contain duplicates

What it means

HashImport.build throws IllegalArgumentException when the same field appears more than once in the input. Duplicates are detected content-wise via a HashSet of ByteBuffers, ensuring each hash field in the import template is unique.

Solutions

  1. Deduplicate caller-side: Arrays.stream(fields).distinct().toArray(String[]::new) for strings, or a LinkedHashSet<byte[]> wrapped appropriately for binary content.
  2. If merge semantics are needed, build a Map first so later values override earlier ones, then pass unique field names.
  3. Decide on one representative entry per duplicated field before constructing the template.

Example fix

// before
HashImport t = HashImport.of(fields); // may contain duplicates
// after
String[] unique = Arrays.stream(fields).distinct().toArray(String[]::new);
HashImport t = HashImport.of(unique);
Defensive patterns

Strategy: validation

Validate before calling

long distinct = Arrays.stream(fields).distinct().count();
if (distinct != fields.length) { /* deduplicate before HashImport.of */ }

Type guard

boolean allDistinct(String[] a) { return a == null || Arrays.stream(a).distinct().count() == a.length; }

Try / catch

try {
  HashImport t = HashImport.of(fields);
} catch (IllegalArgumentException e) {
  log.warn("Duplicate hash field rejected: {}", e.getMessage());
}

Prevention

When it happens

Trigger: Calling HashImport.of("field", "field"); passing a byte[][] containing content-identical arrays (duplicate detection compares bytes, not references); fields collected from a source that can repeat keys (e.g. concatenated config sections).

Common situations: Merging field lists from multiple sources without deduplication; HGETALL-style dumps re-fed as import fields in edge cases; users assuming later entries override earlier ones, but the API forbids duplicates outright.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of redis/jedis@6dac31d4c2 (2026-09-08). Data as JSON: /api/errors/4801b583202d80c8. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/redis/clients/jedis/HashImport.java:114

    }
    List<byte[]> copy = new ArrayList<>(fields.length);
    for (byte[] field : fields) {
      if (field == null) {
        throw new IllegalArgumentException("HashImport fields must not contain null");
      }
      copy.add(field.clone()); // clone so later caller mutation can't alter the template
    }
    return build(copy);
  }

  private static HashImport build(List<byte[]> fields) {
    Set<ByteBuffer> seen = new HashSet<>();
    for (byte[] field : fields) {
      if (field.length == 0) {
        throw new IllegalArgumentException("HashImport fields must not contain empty names");
      }
      if (!seen.add(ByteBuffer.wrap(field))) {
        throw new IllegalArgumentException("HashImport fields must not contain duplicates");
      }
    }
    return new HashImport(nextName(), Collections.unmodifiableList(fields));
  }

  /**
   * @return {@code true} once {@link #close()} has been called
   * @since 8.0
   */
  public boolean isDiscarded() {
    return discarded;
  }

  /**
   * Discards this template; it must not be used again afterwards. Server-side cleanup is deferred
   * and handled by the client. Idempotent.
   * @since 8.0
   */

View on GitHub (pinned to 6dac31d4c2)