Skip to content

feat: Add TypeReference support for generic types in JSON deserialization - #22442

Merged
Artur- merged 6 commits into
mainfrom
typereference-return-values
Oct 7, 2025
Merged

Artur- merged 6 commits into
mainfrom
typereference-return-values

Conversation

@Artur-

@Artur- Artur- commented Oct 7, 2025

Copy link
Copy Markdown
Member

Add TypeReference overloads to enable List and Map<String, Bean> deserialization across all JSON APIs:

  • JacksonCodec.decodeAs(JsonNode, TypeReference) for core deserialization
  • PendingJavaScriptResult.then/toCompletableFuture with TypeReference
  • Element.getProperty(String, Class/TypeReference) for typed property access
  • DomEvent.getEventData(Class/TypeReference) for typed event data

This enables usage like:

TypeReference<List<MyDto>> ref = new TypeReference<List<MyDto>>() {}; 
element.executeJs("return [...]").then(ref, list -> ...);

…tion

Add TypeReference overloads to enable List<Bean> and Map<String, Bean> deserialization across all JSON APIs:
- JacksonCodec.decodeAs(JsonNode, TypeReference<T>) for core deserialization
- PendingJavaScriptResult.then/toCompletableFuture with TypeReference
- Element.getProperty(String, Class/TypeReference) for typed property access
- DomEvent.getEventData(Class/TypeReference) for typed event data

This enables usage like:
  TypeReference<List<MyDto>> ref = new TypeReference<List<MyDto>>() {};
  element.executeJs("return [...]").then(ref, list -> ...);

All 37 JacksonCodecTest tests pass including 5 new TypeReference tests.

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

Co-Authored-By: Claude <noreply@anthropic.com>
@Artur-
Artur- requested a review from caalador October 7, 2025 07:52
@Artur-

Artur- commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

The Element API getProperty(String, Class) is incompatible with current uses of getProperty("somethingDefaultNull", null) which now would need an explicit cast for null to String. Do you think that is an issue? It would feel weird to add a suffix for only this case and not all the other types. @Legioth @caalador

@Artur-

Artur- commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

Likely either setPropertyBean should become setProperty or then getProperty(String, Class/TypeReference) should become getPropertyBean

Artur- and others added 2 commits October 7, 2025 11:49
…vent data capture

Add overloaded addEventData methods to DomListenerRegistration that automatically
introspect bean/record structures and add event data expressions for all properties.
This eliminates tedious manual addEventData() calls when working with structured
event data.

Key changes:
- Add BeanUtil.getBeanPropertyPaths() utility for extracting property paths from
  bean/record structures using Java Bean introspection
- Add DomListenerRegistration.addEventData(Class<?>) overload that auto-discovers
  all properties and nested properties
- Add DomListenerRegistration.addEventData(TypeReference<?>) overload for generic
  type support
- Add tests for nested records and simple beans in ElementListenersTest
- Add comprehensive tests for record deserialization in JacksonCodecTest
- Clean separation of concerns: introspection logic in BeanUtil, registration
  just iterates over paths

Usage example:
  record MouseEventData(EventDetails event, String type) {
      record EventDetails(int button, int clientX, int clientY) {}
  }

  element.addEventListener("click", e -> {
      MouseEventData data = e.getEventData(MouseEventData.class);
      System.out.println("Button: " + data.event().button());
  }).addEventData(MouseEventData.class);
  // Automatically adds: event.button, event.clientX, event.clientY, type

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

Co-Authored-By: Claude <noreply@anthropic.com>
@caalador

caalador commented Oct 7, 2025

Copy link
Copy Markdown
Contributor

getProperty("somethingDefaultNull", null) feels silly in that it should just be used from getProperty("somethingDefaultNull") even if that doesn't remove the explicit cast in internal code.

The @eventdata annotation already supported bean types via JacksonCodec
deserialization in ComponentEventBus.createEventDataObjects(), but the
javadoc incorrectly stated that only primitives and JsonNode were supported.

Changes:
- Updated EventData javadoc to document full support for:
  - Beans and DTOs
  - Java records
  - Collections (List, Map)
  - All types Jackson can deserialize
- Added comprehensive example showing bean parameter usage
- Added test EventWithBeanData demonstrating bean parameters
- Added ComponentEventBusTest.eventWithBeanParameter_beanDeserialized() test
- Updated json.md to mark all 10/10 features as implemented (100% complete!)

This completes JSON/DTO support across all Flow APIs.

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

Co-Authored-By: Claude <noreply@anthropic.com>
@Artur- Artur- changed the title feat!: Add TypeReference support for generic types in JSON deserialization feat: Add TypeReference support for generic types in JSON deserialization Oct 7, 2025
@Artur-

Artur- commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

The bean getter is now getPropertyBean which makes this a non-breaking change

@github-actions

github-actions Bot commented Oct 7, 2025 •

Copy link
Copy Markdown
Contributor

Test Results

1 270 files  ± 0  1 270 suites  ±0   1h 16m 9s ⏱️ -6s
8 765 tests +10  8 698 ✅ +10  67 💤 ±0  0 ❌ ±0 
9 217 runs  + 7  9 140 ✅ + 7  77 💤 ±0  0 ❌ ±0 

Results for commit b46cd0a. ± Comparison against base commit 121d791.

♻️ This comment has been updated with latest results.

@Artur-
Artur- enabled auto-merge (squash) October 7, 2025 13:54
@sonarqubecloud

sonarqubecloud Bot commented Oct 7, 2025

Copy link
Copy Markdown

@Artur-
Artur- merged commit d0b0bb1 into main Oct 7, 2025
6 of 8 checks passed
@Artur-
Artur- deleted the typereference-return-values branch October 7, 2025 14:10
@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with Vaadin 25.0.0-beta2.

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.

3 participants