{"record":{"id":"9f28920dc12f9c5b","repo":"apache/beam","slug":"could-not-encode-provided-windows-with-the-provided-window","errorCode":null,"errorMessage":"Could not encode provided windows with the provided window coder","messagePattern":"Could not encode provided windows with the provided window coder","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/testing/WindowSupplier.java","lineNumber":55,"sourceCode":"// Spotbugs thinks synchronization is for the fields, but it is for the act of decoding\n@SuppressFBWarnings(\"IS2_INCONSISTENT_SYNC\")\n@SuppressWarnings({\n  \"nullness\" // TODO(https://github.com/apache/beam/issues/20497)\n})\nfinal class WindowSupplier implements Supplier<Collection<BoundedWindow>>, Serializable {\n  private final Coder<? extends BoundedWindow> coder;\n  private final Collection<byte[]> encodedWindows;\n\n  /** Access via {@link #get()}. */\n  private transient @Nullable Collection<BoundedWindow> windows;\n\n  public static <W extends BoundedWindow> WindowSupplier of(Coder<W> coder, Iterable<W> windows) {\n    ImmutableSet.Builder<byte[]> windowsBuilder = ImmutableSet.builder();\n    for (W window : windows) {\n      try {\n        windowsBuilder.add(CoderUtils.encodeToByteArray(coder, window));\n      } catch (CoderException e) {\n        throw new IllegalArgumentException(\n            \"Could not encode provided windows with the provided window coder\", e);\n      }\n    }\n    return new WindowSupplier(coder, windowsBuilder.build());\n  }\n\n  private WindowSupplier(Coder<? extends BoundedWindow> coder, Collection<byte[]> encodedWindows) {\n    this.coder = coder;\n    this.encodedWindows = encodedWindows;\n  }\n\n  @Override\n  public Collection<BoundedWindow> get() {\n    if (windows == null) {\n      decodeWindows();\n    }\n    return windows;\n  }","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/WindowSupplier.java#L37-L73","documentation":"WindowSupplier.of encodes each provided window with the supplied Coder; if any encoding fails (CoderException) it wraps it in this IllegalArgumentException. The provided coder cannot serialize one of the provided window instances.","triggerScenarios":"Calling WindowSupplier.of(coder, windows) with a coder incompatible with the window objects (wrong coder type, coder that throws CoderException for these values).","commonSituations":"Passing e.g. a GlobalWindow coder for IntervalWindow values, or a coder whose type parameters don't match the windows after a refactor.","solutions":["Pass the coder matching the window type, e.g. IntervalWindow.getCoder() or WindowCoder.of()","Verify the Iterable<W> windows actually match the Coder<W> type parameter","Catch at authoring time by unit-testing encode/decode round-trip of one window"],"exampleFix":"// before\nWindowSupplier.of(InstantCoder.of(), windows) // wrong coder\n// after\nWindowSupplier.of(IntervalWindow.getCoder(), windows)","handlingStrategy":"validation","validationCode":"for (W window : windows) {\n  CoderUtils.encodeToByteArray(coder, window); // throws CoderException early with better context\n}","typeGuard":null,"tryCatchPattern":"try { WindowSupplier.of(coder, windows); }\ncatch (IllegalArgumentException e) { /* coder does not match window type; fix coder */ }","preventionTips":["Always derive the coder from the window type (e.g. IntervalWindow.getCoder())","Round-trip encode/decode one window before constructing the supplier","Keep Coder<W> generics aligned with the Iterable<W> element type"],"tags":["java","windows","coder","encoding"],"backgroundTag":"incompatible-source-type","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"}