{"record":{"id":"fd7e5902b21a132f","repo":"apache/hadoop","slug":"unable-to-serialize-empty-enumset-with-no-element","errorCode":null,"errorMessage":"Unable to serialize empty EnumSet with no element type provided.","messagePattern":"Unable to serialize empty EnumSet with no element type provided\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/EnumSetWritable.java","lineNumber":146,"sourceCode":"      E first = (E) ObjectWritable.readObject(in, conf);\n      this.value = (EnumSet<E>) EnumSet.of(first);\n      for (int i = 1; i < length; i++)\n        this.value.add((E) ObjectWritable.readObject(in, conf));\n    }\n  }\n\n  @Override\n  public void write(DataOutput out) throws IOException {\n    if (this.value == null) {\n      out.writeInt(-1);\n      WritableUtils.writeString(out, this.elementType.getName());\n    } else {\n      Object[] array = this.value.toArray();\n      int length = array.length;\n      out.writeInt(length);\n      if (length == 0) {\n        if (this.elementType == null)\n          throw new UnsupportedOperationException(\n              \"Unable to serialize empty EnumSet with no element type provided.\");\n        WritableUtils.writeString(out, this.elementType.getName());\n      }\n      for (int i = 0; i < length; i++) {\n        ObjectWritable.writeObject(out, array[i], array[i].getClass(), conf);\n      }\n    }\n  }\n\n  /**\n   * Returns true if <code>o</code> is an EnumSetWritable with the same value,\n   * or both are null.\n   */\n  @Override\n  public boolean equals(Object o) {\n    if (o == null) {\n      throw new IllegalArgumentException(\"null argument passed in equal().\");\n    }","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/EnumSetWritable.java#L128-L164","documentation":"write() encodes an empty EnumSet as length 0 plus the element type name string, so the reader can rebuild an empty set of the right enum. If value is empty and elementType is null it throws UnsupportedOperationException — there is nothing to serialize the type from. set() normally prevents this state, so this fires when the invariant is bypassed (e.g. field mutation, subclasses, or a set(emptySet, type) followed by manual nulling).","triggerScenarios":"Constructing via a path that skips set()'s guard (reflection/field access setting value to an empty EnumSet with elementType null), then calling write(); subclass of EnumSetWritable that assigns fields directly.","commonSituations":"Custom serialization frameworks that restore object state field-by-field; test fixtures building writables without constructors; stale forks predating the elementType validation.","solutions":["Always populate via the constructor or set(value, elementType) so the type is captured.","Before writing, guard: if (w.get() == null || ((EnumSet<?>) w.get()).isEmpty()) ensure a type is set — or proactively set(emptySet, MyEnum.class).","Catch UnsupportedOperationException on write and fail the record with field context."],"exampleFix":"// before\nEnumSetWritable<Flag> w = ...; // value empty, elementType null\nout.write(w); // throws\n\n// after\nif (w.get() == null || ((EnumSet<?>) w.get()).isEmpty()) {\n  w.set(EnumSet.noneOf(Flag.class), Flag.class);\n}\nout.write(w);","handlingStrategy":"validation","validationCode":"EnumSet<Flag> v = (EnumSet<Flag>) w.get();\nif (v == null || v.isEmpty()) {\n  w.set(EnumSet.noneOf(Flag.class), Flag.class);\n}\nout.write(w);","typeGuard":null,"tryCatchPattern":"try {\n  w.write(out);\n} catch (UnsupportedOperationException e) {\n  throw new IllegalStateException(\"EnumSetWritable has empty value and no element type\", e);\n}","preventionTips":["Ensure every empty EnumSetWritable carries an element type before serialization.","Avoid restoring writable state via reflection; use constructors and set()."],"tags":["hadoop","serialization","writable","enumset","empty-collection"],"backgroundTag":"empty-collection-serialization","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}