Skip to content

fix: use data view identifier provider for grid selection - #10393

Merged
web-padawan merged 1 commit into
mainfrom
fix/grid-use-data-view-identifier-provider
Oct 12, 2026
Merged

web-padawan merged 1 commit into
mainfrom
fix/grid-use-data-view-identifier-provider

Conversation

@sissbruecker

Copy link
Copy Markdown
Member

Description

Fixes #10389

The data providers that setItems(...) and setItemsPageable(...) create return the item itself from getId(). The selection models only asked the data provider for the ID, so selection compared items with equals() and ignored the identifier provider set through setIdentifierProvider() on the data view.

  • Added package-private Grid.getItemId(T), which uses the identifier provider of the data view and falls back to DataProvider.getId()
  • Changed the single and multi selection models and the PRESERVE_EXISTING selection handling to compare items with Grid.getItemId(T)
    • isSelected() now matches items by ID, selecting a new instance of a selected item fires no event and adds no second entry, and the selection is kept after refreshAll() returns new instances
  • Grid now updates the IDs of the selected items in multi select mode when the identifier provider changes, so a provider set after selecting items also works
  • Added unit tests in GridSelectionWithIdentifierProviderTest

Type of change

  • Bugfix

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sonarqubecloud

sonarqubecloud Bot commented Oct 9, 2026

Copy link
Copy Markdown

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the changes — collected 1 finding below that could not be attached to the diff.

Finding
⚠️ With a lazy data view the identifier provider never reaches the key mapper, so client-side selection updates still compare by data provider identity

⚠️ With a lazy data view the identifier provider never reaches the key mapper, so client-side selection updates still compare by data provider identity
GridDataView and GridListDataView override setIdentifierProvider to also call keyMapper.setIdentifierGetter(...); GridLazyDataView does not, and neither does Flow's AbstractLazyDataView. After this change the server side of a lazy grid matches items by the identifier provider, but the client sync path does not:

  • Grid.isInActiveRange is keyMapper.has(item), which still uses dataProvider::getId.
  • So grid.select(copyWithSameId) updates getSelectedItems() but sends nothing to the client, and the visible row stays unhighlighted until the next data refresh regenerates it.

The new singleSelect_lazyDataView_instanceWithSameId_isSelected test only asserts the server-side isSelected, so the gap is invisible. Overriding setIdentifierProvider in GridLazyDataView the same way the other two data views do would close it.
GridLazyDataView.java:34 · correctness

@sissbruecker

Copy link
Copy Markdown
Member Author

⚠️ With a lazy data view the identifier provider never reaches the key mapper, so client-side selection updates still compare by data provider identity

Mostly a pre-existing issue that had other consequences. Extracted this fix into #10394.

peholmst added a commit to vaadin/docs that referenced this pull request Oct 10, 2026
## Summary

Corrects the factual slips in Getting Started listed in #6294.

- **Quick Start, escaping note:** Vaadin escapes text when it displays
it; it doesn't sanitize input. The greeting is a `Paragraph`, not a
notification. The note now explains this, mentions XSS, and links to
*Write HTML* for content you have to sanitize yourself.
- **Quick Start, fourth screenshot:** the alt text described the QR code
view, one step early. It now describes what the image shows: extra space
between the empty text field and the button.
- **Tutorial, Set Up Project:**
- The H2 database is in-memory and created from `schema.sql` and
`data.sql` on every start, not an existing database file.
- A new note says that changes disappear on restart, and links to *Use a
Real Database* in Next Steps.
- "Setup" becomes "Set Up" in the title and heading. The link text in
Add Tests follows.
- **Tutorial index:** promises only the product catalog, which is what
the tutorial builds. It says "deep linking" instead of "navigation".
- **Generic run guide:**
- Recommends the vanilla JetBrains Runtime instead of the JCEF variant,
which adds an embedded browser for desktop apps. Only the JCEF variants
have a macOS `.pkg`, so the macOS instructions now say where the JVM is
in the extracted archive (`Contents/Home`, checked against an installed
JBR).
  - Uses `com.example.Application` in both the command and the script.
  - Fixes "to run build and run".
- **NetBeans run guide:** "JetBrain" becomes "JetBrains".

Vale shows no new alerts in the changed files.

## What's still open in #6294

- Item 1: the form binds to the persistence entity. This needs a design
decision.
- Item 2: `equals`/`hashCode` on `ProductCatalogItem`. This is waiting
for vaadin/flow-components#10393.

Part of #6294.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
peholmst added a commit to vaadin/docs that referenced this pull request Oct 10, 2026
)

## Summary

Responds to item 1 in #6294: the form binds to the persistence entity.
The tutorial keeps `ProductDetails` as both the Spring Data entity and
the form data object, because the form and the `PRODUCTS` table match
one-to-one and change together. A separate form object would only copy
the same values back and forth at this stage. The pages now explain this
choice instead of leaving it unexplained.

- **Show Details:**
- A "Why Is the FDO Also an Entity?" note where `ProductDetails` is
introduced. It explains the choice, says when to introduce a separate
form object (the form combines data from several tables, or the entity
contains business logic), and links to the *Entities* section of *Add a
Service*.
- The callout on `productId` says why the ID is a `Long` here and a
`long` in `ProductCatalogItem`: a new product has no ID until it's
saved. The review flagged this mismatch too.
- **Add Data:** one paragraph in "Work Around the Version Bug" names the
bug as a cost of using the entity as the form object. A separate form
object would avoid it too, but copying the entity before saving is
enough while the form and the table match.

No new Building Apps content was needed. *Add a Service* → *Input &
Output* already covers when to pass entities and when to use DTOs, and
*Fields and Binding* has an "Entities as Form Data Objects" note.

Vale shows no new alerts in the changed files.

After this and #6357, the only open item in #6294 is item 2, which is
waiting for vaadin/flow-components#10393.

Part of #6294.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@web-padawan
web-padawan added this pull request to the merge queue Oct 12, 2026
Merged via the queue into main with commit 542b61d Oct 12, 2026
25 checks passed
@web-padawan
web-padawan deleted the fix/grid-use-data-view-identifier-provider branch October 12, 2026 06:19
sissbruecker added a commit that referenced this pull request Oct 12, 2026
…P: 25.2) (#10398)

This PR cherry-picks changes from the original PR #10393 to branch 25.2.

---

> ## Description
> 
> Fixes #10389
> 
> The data providers that `setItems(...)` and `setItemsPageable(...)`
create return the item itself from `getId()`. The selection models only
asked the data provider for the ID, so selection compared items with
`equals()` and ignored the identifier provider set through
`setIdentifierProvider()` on the data view.
> 
> - Added package-private `Grid.getItemId(T)`, which uses the identifier
provider of the data view and falls back to `DataProvider.getId()`
> - Changed the single and multi selection models and the
`PRESERVE_EXISTING` selection handling to compare items with
`Grid.getItemId(T)`
> - `isSelected()` now matches items by ID, selecting a new instance of
a selected item fires no event and adds no second entry, and the
selection is kept after `refreshAll()` returns new instances
> - Grid now updates the IDs of the selected items in multi select mode
when the identifier provider changes, so a provider set after selecting
items also works
> - Added unit tests in `GridSelectionWithIdentifierProviderTest`
> 
> ## Type of change
> 
> - Bugfix
>

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
sissbruecker added a commit that referenced this pull request Oct 12, 2026
…P: 25.3) (#10399)

This PR cherry-picks changes from the original PR #10393 to branch 25.3.

---

> ## Description
> 
> Fixes #10389
> 
> The data providers that `setItems(...)` and `setItemsPageable(...)`
create return the item itself from `getId()`. The selection models only
asked the data provider for the ID, so selection compared items with
`equals()` and ignored the identifier provider set through
`setIdentifierProvider()` on the data view.
> 
> - Added package-private `Grid.getItemId(T)`, which uses the identifier
provider of the data view and falls back to `DataProvider.getId()`
> - Changed the single and multi selection models and the
`PRESERVE_EXISTING` selection handling to compare items with
`Grid.getItemId(T)`
> - `isSelected()` now matches items by ID, selecting a new instance of
a selected item fires no event and adds no second entry, and the
selection is kept after `refreshAll()` returns new instances
> - Grid now updates the IDs of the selected items in multi select mode
when the identifier provider changes, so a provider set after selecting
items also works
> - Added unit tests in `GridSelectionWithIdentifierProviderTest`
> 
> ## Type of change
> 
> - Bugfix
>

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
sissbruecker added a commit that referenced this pull request Oct 12, 2026
….2) (#10401)

This PR cherry-picks changes from the original PR #10394 to branch 25.2.

---

> ## Description
> 
> Related to #10389
> 
> `GridListDataView` and `GridDataView` pass the identifier provider to
the data communicator's `KeyMapper`, but `GridLazyDataView` did not. On
a lazy grid (`setItems(fetchCallback)`), the `KeyMapper` kept comparing
items with `equals()`. For example, `refreshItem(item)` with a new
instance of a loaded item did not update the row.
> 
> - Overrode `setIdentifierProvider` in `GridLazyDataView` so it sets
the identifier getter of the `KeyMapper`, the same way as
`GridListDataView` and `GridDataView`
> - Added unit tests in `GridLazyDataViewTest` for the `KeyMapper`
identifier and for `refreshItem()` with a new instance that has the same
ID
> 
> ## Type of change
> 
> - Bugfix
> 
> > [!NOTE]
> > Selection still compares items with `equals()` and ignores the
identifier provider. This is fixed in a follow-up PR: #10393
>

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
sissbruecker added a commit that referenced this pull request Oct 12, 2026
….3) (#10402)

This PR cherry-picks changes from the original PR #10394 to branch 25.3.

---

> ## Description
> 
> Related to #10389
> 
> `GridListDataView` and `GridDataView` pass the identifier provider to
the data communicator's `KeyMapper`, but `GridLazyDataView` did not. On
a lazy grid (`setItems(fetchCallback)`), the `KeyMapper` kept comparing
items with `equals()`. For example, `refreshItem(item)` with a new
instance of a loaded item did not update the row.
> 
> - Overrode `setIdentifierProvider` in `GridLazyDataView` so it sets
the identifier getter of the `KeyMapper`, the same way as
`GridListDataView` and `GridDataView`
> - Added unit tests in `GridLazyDataViewTest` for the `KeyMapper`
identifier and for `refreshItem()` with a new instance that has the same
ID
> 
> ## Type of change
> 
> - Bugfix
> 
> > [!NOTE]
> > Selection still compares items with `equals()` and ignores the
identifier provider. This is fixed in a follow-up PR: #10393
>

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
peholmst added a commit to vaadin/docs that referenced this pull request Oct 12, 2026
…6372)

## Summary

Fixes #6294 (item 2, the last open one).

The tutorial's Edit Details step kept the grid selection after a save by
overriding `equals()` and `hashCode()` in the `ProductCatalogItem`
record. This replaces the override with an identifier provider on the
grid's data view:

```java
grid.setItemsPageable(pageable -> service
    .findItems(searchField.getValue(), pageable)
).setIdentifierProvider(ProductCatalogItem::productId);
```

- **Edit Details → Preserve Grid Selection**: rewritten around the
identifier provider, with a short paragraph on why it's preferable to
overriding `equals()` on the record, and a note that it requires Vaadin
25.3.2 or later.
- **Edit Details, Add Data, Deep Link**: every later
`ProductCatalogView` listing chains the identifier provider, and the
`ProductCatalogItem` listings are back to a plain record.

## Why this is a draft

The grid's selection only honors the identifier provider since
vaadin/flow-components#10393 (selection) and
vaadin/flow-components#10394 (item keys of lazy data views, which
`setItemsPageable()` returns). Both are in flow-components 25.3.2, but
the Vaadin platform 25.3.2 BOM isn't released yet. Mark ready when it
is, then backport to `v25.3`.

## Verification

I ran a server-side check of the tutorial's exact pattern:
`setItemsPageable(...).setIdentifierProvider(...)`, select an item, call
`refreshAll()` so the callback returns new instances, then select a
newly fetched instance, as the Deep Link step does.

| | `vaadin-grid-flow` 25.3.1 | 25.3.2 |
|---|---|---|
| `isSelected()` on the reloaded instance | false | true |
| Row rendered with the `selected` flag | false | true |
| Same item key after reload | false | true |
| Selecting a fresh instance of the selected item fires an event | yes |
no |

## Notes for review

- The minimum version is a `[NOTE]` admonition rather than a `since`
badge. No page in the docs uses a patch-level badge, and the note can
also tell readers how to upgrade. Vale flags the version number
(`Vaadin.Versions`) on purpose.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
peholmst added a commit to vaadin/docs that referenced this pull request Oct 12, 2026
Addresses finding 5 of #6288: give the Getting Started tutorial a
bounded outcome that matches its promise, and link a runnable reference
application.

## What changed

**New step: [Add
Navigation](articles/getting-started/tutorial/add-navigation.adoc)**
(`/getting-started/tutorial/add-navigation`, order 35, between Add Data
and Deep Link).
- `MainLayout` in `com.example.base.ui`, following the layout of
projects from start.vaadin.com: an `@Layout` App Layout with the drawer
as the primary section, holding an avatar logo, the application name,
and a Side Navigation menu in a scroller. The menu is built from
`MenuConfiguration.getMenuEntries()`, so the layout never refers to a
view class. That matters here because the tutorial's views are
package-private.
- `ViewTitle` in `com.example.base.ui` (public): a drawer toggle and an
H1 that each view puts at the top, since the layout has no navigation
bar. The product catalog puts it at the start of its existing toolbar,
which becomes its title bar, next to the search field and the Add
Product button, and wraps on narrow screens. The styles from the starter
go in the skeleton's `styles.css`. The Aura properties they use are
defined in the `aura.css` that 25.3.2 ships.
- `OrdersView` in `com.example.order`: a deliberately trivial
placeholder at `orders`.
- `ProductCatalogView` gets `@Menu` and moves from `""` to `products`,
with `@RouteAlias("")` so that http://localhost:8080 still opens it.
Otherwise, the Deep Link URLs would be `/12`, and the menu item couldn't
stay highlighted on them: `setMatchNested` can't match `/12` against
`/`.
- A note that the layout needs its own access annotation once the
application is secured. This matches `AnnotatedViewAccessChecker` in
Flow 25.3 and 25.4.
- The complete-code section lists the three changed or new classes and
points to Add Data for the rest, like Add Tests does for Deep Link.

**Edit Details: new *Delete Products* section.** It adds
`ProductCatalogService.delete()`, a red **Delete** button with a
`DeleteCallback` in `ProductFormDrawer`, a `ConfirmDialog` (error-themed
confirm button, cancelable), and the view's callback, which deletes,
clears the selection, and refreshes the grid. The drawer now keeps the
product it shows, so that delete gets the current version. Spring Data
deletes with a version check, so a stale delete raises the
`OptimisticLockingFailureException` that the view already handles. The
section says how to try that with two windows.

**Propagated through the later steps.**
- Add Data: every `ProductCatalogView` and `ProductCatalogService`
listing and the complete code carry the delete callback and method. The
"Next" button now leads to Add Navigation.
- Deep Link (order 35 → 40): every view listing carries
`@Route("products")`, `@RouteAlias("")`, `@Menu`, and the delete
callback. The prose uses `/products/12` and mentions that the menu item
stays highlighted. Every view listing also has the view title in the
toolbar. The complete code now also lists `MainLayout`, `ViewTitle`, and
`OrdersView`, so it shows every class in the application again. The
sentence that said "other views" can call `showProductDetails()` now
says "other classes in the `com.example.product` package", since the
class is package-private and there now is a view in another package.
- Add Tests (order 40 → 45): the recap mentions delete and the layout.
The tests are unchanged.
- Index: mentions the layout and navigation, uses the new screenshot of
the finished application, and says 15 to 45 minutes per step, since the
new step is short.

**Screenshot.** `product-catalog-with-navigation.png` is used by the
index and the new step. It's 1600×1200 like the others, but captured at
1200×900 CSS pixels at 4/3×. At 800 px width, App Layout collapses the
drawer into an overlay, and up to about 1000 px the product drawer of
Master-Detail Layout covers the grid. The dev-tools overlay is hidden so
that it doesn't cover the Delete button. The old
`product-drawer-with-save.png` is still used in Edit Details.

**Also in the changed pages:** four pre-existing Vale warnings (simply,
will, just, and an exclamation mark in the index title), so that these
pages are Vale-clean.

**Step order:** The task mentioned that Add Data's `order: 30` clashes
with the index. It doesn't: the index's 30 is the tutorial's position
among the Getting Started pages, not among its steps. I only renumbered
what the new step displaces.

## How it was verified

I built the tutorial from the AsciiDoc rather than reading it.
- **Project.** I downloaded the skeleton from the Setup step's link
(`platformVersion=v25.4`, Vaadin 25.4.0-alpha1) and added the
dependencies and SQL files as Setup says.
- **Every listing.** A script followed every step in order. It applied
each whole-class Java listing as written (snippet tags and callouts
stripped), compiled after each one, and then applied the step's complete
code and compiled again. It also added the validation dependency where
Edit Details says to.
- **Results.** Every listing compiles except four intermediate states,
where a constructor changes one listing before its caller does. Two
existed before and are already announced in the text. The new Delete
section now says so too, and the fourth is the existing Add Product
dialog change.
- **Tests.** After Add Tests, `./mvnw verify` passes: 6 tests, BUILD
SUCCESS, jar built.
- **25.3.** I repeated the same walk with the `v25.3` skeleton (Vaadin
25.3.1) for the cherry-pick, with the same results. The tutorial pages
on `v25.3` are identical to `main`.
- **Main layout** (start.vaadin.com style): after the change, I repeated
the full walk on Vaadin 25.3.2 with the same results, and the
complete-code check passes. In a browser, I checked the drawer toggle on
a wide screen (it hides and shows the drawer) and at phone width (it
opens the drawer as an overlay). At phone width, the toolbar wraps to
two rows. The menu is empty right after Create the Main Layout, as the
text says.
- **After merging #6372** (the identifier provider that replaces the
`equals()` override): the new Add Navigation listing chains
`.setIdentifierProvider(ProductCatalogItem::productId)` like every other
later listing. I repeated the walk on Vaadin 25.3.2 with the same
results. A throwaway browserless test confirmed that the reloaded item
stays selected after saving, and that deleting right after saving works.
On 25.4.0-alpha1, which `main`'s Setup link downloads, the selection is
still lost after saving, because that build predates
vaadin/flow-components#10393. #6372's note asks for 25.3.2 or later, so
that should resolve itself with the next 25.4 pre-release.
- **In a browser** (the finished app, built from the docs only):
  - Menu highlighting on `/`, `/products/N`, and `/orders`.
  - Back button.
  - Drawer toggle at mobile width.
- Delete confirm and cancel. After a delete, the drawer closes and the
URL returns to `/products`.
- The view instance is reused when moving from the `/` alias to
`/products/N`: the search text is kept.
- **Extra browserless tests** (throwaway, not in the tutorial):
- Confirmed delete, canceled delete, stale-version delete (shows the
notification and keeps the product), and delete right after save.
  - The menu has two entries.
- The intermediate state with the layout but no `@Menu` renders with an
empty menu.
- **Docs checks:**
  - `vale --minAlertLevel=warning` is clean on all changed pages.
- The front matter parses. The new page's page-title is 55 characters,
and its meta-description is 158.
  - `npm run check-redirects` passes. No page moved.
- Rendered with `asciidoctor -d book -a idprefix= -a idseparator=-`. The
anchors this PR links to exist: `complete-code-for-this-step` on Add
Data, `automatic-layouts` on Add a Router Layout, and `vaadin-icons` on
Icons.

## Still open

- **The finished-application repository doesn't exist.**
- **What I found:** I searched the vaadin org and found nothing for this
tutorial. [vaadin/tutorial](https://github.com/vaadin/tutorial) holds
the Vaadin 24.4 chat tutorial, and `flow-quickstart-tutorial` is the
quick start. The only copy of the product catalog code on GitHub is a
personal repository. So this PR links no repository, and the issue's
"runnable reference application" acceptance criterion stays unmet.
- **Proposal:** Reuse `vaadin/tutorial`. Its README already says it
holds the tutorial application and links these docs. Put the finished
application on `main`, with one tag or branch per step
(`step-add-navigation`, …), so that readers can start or compare at any
step. A CI job could rebuild it from the docs listings, the way this PR
was verified, and run `mvn verify` against each release, which would
also catch listings that drift.
- **Tracked in** vaadin/tutorial#13. **Once it exists:** link it from
the tutorial index and from the end of Add Tests.
- **Trimming the listings** (from the [second
comment](#6288 (comment)))
is not done. This PR adds about 500 lines, mostly whole-class listings
that had to carry the delete callback and the new annotations.
- **Unchanged pages:** List Data and Sort & Filter still have three
pre-existing Vale warnings, which this PR doesn't touch.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with Vaadin 25.4.0-alpha3.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grid selection ignores the identifier provider set on its data view

4 participants