{"record":{"id":"0a798335d6f6095f","repo":"apache/beam","slug":"error-serializing-via-coder","errorCode":null,"errorMessage":"Error serializing via Coder","messagePattern":"Error serializing via Coder","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/testing/SerializableMatchers.java","lineNumber":787,"sourceCode":"   * is not likely to be a good encoding, so should be used only for tests, where data volume is\n   * small and minor costs are not critical.\n   */\n  private static class SerializableViaCoder<T> implements SerializableSupplier<T> {\n    /** Cached value that is not serialized. */\n    private transient @Nullable T value;\n\n    /** The bytes of {@link #value} when encoded via {@link #coder}. */\n    private byte[] encodedValue;\n\n    private Coder<T> coder;\n\n    public SerializableViaCoder(Coder<T> coder, T value) {\n      this.coder = coder;\n      this.value = value;\n      try {\n        this.encodedValue = CoderUtils.encodeToByteArray(coder, value);\n      } catch (CoderException exc) {\n        throw new RuntimeException(\"Error serializing via Coder\", exc);\n      }\n    }\n\n    @Override\n    public T get() {\n      if (value == null) {\n        try {\n          value = CoderUtils.decodeFromByteArray(coder, encodedValue);\n        } catch (CoderException exc) {\n          throw new RuntimeException(\"Error deserializing via Coder\", exc);\n        }\n      }\n      return value;\n    }\n  }\n\n  /**\n   * Wraps any array with values that can be encoded via a {@link Coder} to make it {@link","sourceCodeStart":769,"sourceCodeEnd":805,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/SerializableMatchers.java#L769-L805","documentation":"SerializableViaCoder serializes a value by encoding it with its Coder into a byte array so it can cross serialization boundaries (e.g. Hamcrest matcher distribution to workers). If CoderUtils.encodeToByteArray throws CoderException, it is wrapped in a RuntimeException with this message.","triggerScenarios":"Constructing a SerializableViaCoder (via SerializableMatchers.viaCoder) with a value that the given Coder cannot encode — mismatched value type vs coder, null encoding failures, or a coder requiring a context the value violates.","commonSituations":"Using a Coder for type X with a value of type Y after a refactor; custom coders that fail on particular values (e.g. Avro/Proto coders with invalid data); values that are null for coders without nullable support.","solutions":["Check that the Coder's encoded type actually matches the value's runtime type.","Encode a sample value in a test with CoderUtils.encodeToByteArray(coder, value) to surface the root CoderException cause.","Fix the custom coder or choose the correct coder (e.g. SerializableCoder if the value is Java-serializable)."],"exampleFix":"// before\nSerializableMatchers.viaCoder(InstantCoder.of()).value(someString);\n// after\nSerializableMatchers.viaCoder(StringUtf8Coder.of()).value(someString);","handlingStrategy":"validation","validationCode":"try { CoderUtils.encodeToByteArray(coder, value); } catch (CoderException e) { throw new IllegalStateException(\"Value not encodable by coder\", e); }","typeGuard":null,"tryCatchPattern":"try { viaCoderMatcher.matches(actual); } catch (RuntimeException e) { if (e.getCause() instanceof CoderException) { /* fix coder/value mismatch */ } }","preventionTips":["Verify coder type matches value type when using SerializableMatchers.viaCoder.","Unit-test encode/decode round trips for custom coders.","Prefer SerializableCoder for Java-serializable test values."],"tags":["java","serialization","coder","apache-beam"],"backgroundTag":"json-marshal-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}