{"record":{"id":"aae4fcd089b26818","repo":"redis/jedis","slug":"hashimport-name-expects-size-values-but-got","errorCode":null,"errorMessage":"HashImport '<name>' expects <size> values but got <valueCount>","messagePattern":"HashImport '<name>' expects <size> values but got <valueCount>","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/redis/clients/jedis/HashImportSupport.java","lineNumber":22,"sourceCode":" * {@code HIMPORT SET} helpers: client-side argument validation, and the per-connection\n * prepare-before-use that a {@code himportSet} {@link CommandObject}'s\n * {@linkplain CommandObject#getPreProcessHooks() pre-process hook} runs. The hook is invoked by\n * {@link Connection#executeCommand(CommandObject)} once the {@code CommandExecutor} has picked a\n * connection &mdash; so retry / cluster redirection / failover stay in force &mdash; injecting a\n * {@code PREPARE} on that connection just before the {@code SET} when the fieldset is not yet\n * prepared there.\n */\nfinal class HashImportSupport {\n\n  private HashImportSupport() {\n  }\n\n  static void checkArgs(HashImport fieldset, int valueCount) {\n    if (fieldset.isDiscarded()) {\n      throw new IllegalStateException(\"HashImport '\" + fieldset.name() + \"' has been discarded\");\n    }\n    if (valueCount != fieldset.size()) {\n      throw new IllegalArgumentException(\"HashImport '\" + fieldset.name() + \"' expects \"\n          + fieldset.size() + \" values but got \" + valueCount);\n    }\n  }\n\n  /**\n   * Prepare-before-use: if {@code fieldset} is not yet prepared on {@code connection}, send\n   * {@code HIMPORT PREPARE} and record it in the connection's note. The PREPARE is built here, only\n   * when actually needed &mdash; the common already-prepared case allocates nothing. The caller\n   * must own the connection.\n   */\n  static void prepareBeforeUse(Connection connection, HashImport fieldset) {\n    if (!connection.himportState().isPrepared(fieldset.name())) {\n      connection.executeCommand(new CommandArguments(Protocol.Command.HIMPORT)\n          .add(Protocol.Keyword.PREPARE).add(fieldset.name()).addObjects(fieldset.fields()));\n      markPrepared(connection, fieldset);\n    }\n  }\n","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/HashImportSupport.java#L4-L40","documentation":"HashImportSupport.checkArgs enforces that the number of values passed to a hash-import call exactly matches the declared size of the HashImport fieldset. If the caller supplies fewer or more values than the fieldset was created with, IllegalArgumentException is thrown before anything is sent to Redis. This protects against desynchronized field/value pairing.","triggerScenarios":"Calling a hash-import API with a values array/collection whose length differs from fieldset.size() — e.g. the fieldset declares 5 hash fields but the caller passes 4 or 6 values in the corresponding varargs/Collection.","commonSituations":"Building the fieldset and the value list from different data sources that drifted out of sync; filtering nulls out of the values list after the fieldset was constructed; off-by-one bugs when appending a new field to one side only.","solutions":["Ensure the values collection length equals fieldset.size() before the call (values.size() == fieldset.size())","Rebuild the fieldset from the same source data as the values so they stay in sync","Log both fieldset.size() and values.size() at the call site to find where they diverge"],"exampleFix":"// before\nList<String> values = rawValues.stream().filter(Objects::nonNull).collect(toList());\njedis.himport(namespace, key, fieldset, values); // expects fieldset.size(), got fewer\n// after\nList<String> values = rawValues.stream().filter(Objects::nonNull).collect(toList());\nif (values.size() != fieldset.size()) {\n  throw new IllegalArgumentException(\"values/fieldset mismatch: \" + values.size() + \" vs \" + fieldset.size());\n}\njedis.himport(namespace, key, fieldset, values);","handlingStrategy":"validation","validationCode":"if (values.size() != fieldset.size()) {\n  throw new IllegalArgumentException(\"expected \" + fieldset.size() + \" values, got \" + values.size());\n}","typeGuard":null,"tryCatchPattern":"try {\n  hashImportCall(fieldset, values);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"expects\")) {\n    log.error(\"fieldset/values count mismatch: {}\", e.getMessage());\n  }\n  throw e;\n}","preventionTips":["Derive the values collection from the same data structure that built the fieldset","Filter/normalize both sides together, never one after the fieldset was sized","Assert equality of counts in tests covering the import path"],"tags":["argument-count","illegal-argument","redis","hash-import"],"backgroundTag":"invalid-argument-value","analyzedSha":"6dac31d4c224fb3257c216f3985340c6f500cdcb","analyzedAt":"2026-09-08T04:55:01.204Z","contentChangedAt":"2026-09-08T04:55:01.204Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}