Skip to content

feat(api): add toBuilder() to DocumentRequest sealed base - #654

Merged
edeandrea merged 1 commit into
docling-project:mainfrom
edeandrea:feat/document-request-tobuilder
Sep 1, 2026
Merged

feat(api): add toBuilder() to DocumentRequest sealed base#654
edeandrea merged 1 commit into
docling-project:mainfrom
edeandrea:feat/document-request-tobuilder

Conversation

@edeandrea

Copy link
Copy Markdown
Contributor

What

Adds toBuilder() to the DocumentRequest sealed base (and the intermediate ChunkDocumentRequest) so a request can be cloned-and-modified through the base type.

Why

Lombok's @SuperBuilder(toBuilder = true) only emits a public toBuilder() on the concrete request subtypes (ConvertDocumentRequest, BatchConvertDocumentRequest, HierarchicalChunkDocumentRequest, HybridChunkDocumentRequest) — never on the abstract DocumentRequest or ChunkDocumentRequest.

As a result, given a DocumentRequest-typed reference, there was no way to inject a source/target without first pattern-matching on the concrete type inside a dispatch switch, e.g.:

case ConvertDocumentRequest r -> client.convertSource(r.toBuilder().source(source).build());

How

  • DocumentRequest declares public abstract DocumentRequest.Builder<?, ?> toBuilder();. The Lombok-generated concrete toBuilder() methods (returning ConvertDocumentRequest.Builder<?, ?> etc.) satisfy it as covariant overrides — no field moves or builder rewrites needed, since sources/target already live on the base.
  • ChunkDocumentRequest adds a narrowing @Override returning ChunkDocumentRequest.Builder<?, ?>.

Callers can now inject a source once, polymorphically, before dispatching:

DocumentRequest withSource = request.toBuilder().source(source).build();

Notes

  • Verified via delombok output that declaring the abstract method does not suppress Lombok's $fillValuesFromInstanceIntoBuilder field-copy helper; the existing toBuilderPreservesSourcesAndTarget test still passes.
  • docs/src/doc/docs/whats-new.md updated.

Tests

Added to DocumentRequestTests:

  • toBuilderReachableThroughBaseType — injects a source through a DocumentRequest reference and verifies sources/target.
  • toBuilderThroughBaseTypePreservesConcreteType — round-trips all four concrete subtypes through the base-type toBuilder() and asserts the concrete runtime type is preserved.

Lombok's @SuperBuilder(toBuilder = true) only emits toBuilder() on the
concrete request subtypes, so it was unreachable through the abstract
DocumentRequest (and intermediate ChunkDocumentRequest) supertype. This
forced callers to inject a source/target inside a concrete-type dispatch
switch.

Declare an abstract toBuilder() on DocumentRequest returning the generic
DocumentRequest.Builder<?, ?>, which the Lombok-generated concrete
toBuilder() methods satisfy as covariant overrides. ChunkDocumentRequest
adds a narrowing override returning its own builder. This lets callers
clone-and-modify polymorphically:

  DocumentRequest withSource = request.toBuilder().source(source).build();

Assisted-By: Claude Code <noreply@anthropic.com>
Signed-off-by: Eric Deandrea <eric.deandrea@ibm.com>
Copilot AI lite review requested due to automatic review settings September 1, 2026 17:47
@edeandrea
edeandrea enabled auto-merge (squash) September 1, 2026 17:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the docling-serve-api request model hierarchy by making toBuilder() reachable from the sealed base types (DocumentRequest, plus the intermediate ChunkDocumentRequest). This enables clone-and-modify workflows (e.g., injecting a source/target) without switching on the concrete request subtype, improving ergonomics for polymorphic dispatch code.

Changes:

  • Added an abstract toBuilder() contract to DocumentRequest returning DocumentRequest.Builder<?, ?>, relying on Lombok’s generated concrete toBuilder() methods to satisfy it via covariant overrides.
  • Added a narrowing abstract override on ChunkDocumentRequest returning ChunkDocumentRequest.Builder<?, ?>.
  • Added tests validating base-type toBuilder() reachability and that round-tripping preserves concrete runtime types; updated “What’s New” docs.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
docs/src/doc/docs/whats-new.md Documents the new ability to call toBuilder() via the DocumentRequest base type.
docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/request/DocumentRequest.java Declares base-type toBuilder() and documents polymorphic clone-and-modify usage.
docling-serve/docling-serve-api/src/main/java/ai/docling/serve/api/chunk/request/ChunkDocumentRequest.java Adds a narrowing toBuilder() override for chunk request subtypes.
docling-serve/docling-serve-api/src/test/java/ai/docling/serve/api/request/DocumentRequestTests.java Adds coverage for base-type toBuilder() usage and concrete-type preservation across all request subtypes.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@edeandrea
edeandrea merged commit 3a69860 into docling-project:main Sep 1, 2026
26 checks passed
@edeandrea
edeandrea deleted the feat/document-request-tobuilder branch September 1, 2026 17:57
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

:java_duke: JaCoCo coverage report

Overall Project 48.04% 🔴

There is no coverage information present for the Files changed

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
TestsPassed ✅SkippedFailed
Gradle Test Results (all modules & JDKs)1844 ran1844 passed0 skipped0 failed
TestResult
No test annotations available

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

HTML test reports are available as workflow artifacts (zipped HTML).

• Download: Artifacts for this run

@docling-java-ops docling-java-ops Bot added the released Issue has been released label Sep 1, 2026
@docling-java-ops

Copy link
Copy Markdown
Contributor

🎉 This issue has been resolved in v0.6.4 (Release Notes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Issue has been released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants