Skip to content

Commit 2ad796b

Browse files
authored
feat: Router parts to jackson (#21100)
* feat: Router parts to jackson Use jackson for router and navigation. * remove import * Code and import cleanup
1 parent cf905bc commit 2ad796b

File tree

7 files changed

+196
-20
lines changed

7 files changed

+196
-20
lines changed

flow-server/src/main/java/com/vaadin/flow/component/EventData.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
import com.vaadin.flow.dom.DomListenerRegistration;
2525

26-
import elemental.json.JsonValue;
27-
2826
/**
2927
* Maps data from a DOM event to a {@link ComponentEvent}.
3028
* <p>
@@ -39,9 +37,10 @@
3937
* expression is passed back to the server and injected into the annotated
4038
* {@link ComponentEvent} constructor parameter.
4139
* <p>
42-
* Supported parameter types are {@link String}, {@link JsonValue},
43-
* {@link Integer}, {@link Double}, {@link Boolean} and their respective
44-
* primitive types.
40+
* Supported parameter types are {@link String},
41+
* {@link elemental.json.JsonValue},
42+
* {@link com.fasterxml.jackson.databind.JsonNode}, {@link Integer},
43+
* {@link Double}, {@link Boolean} and their respective primitive types.
4544
*
4645
* @see DomEvent
4746
* @see DomListenerRegistration#addEventData(String)

flow-server/src/main/java/com/vaadin/flow/component/internal/UIInternals.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.stream.Collectors;
3434
import java.util.stream.Stream;
3535

36+
import com.fasterxml.jackson.databind.node.BaseJsonNode;
3637
import org.slf4j.Logger;
3738
import org.slf4j.LoggerFactory;
3839

@@ -54,6 +55,7 @@
5455
import com.vaadin.flow.dom.impl.BasicElementStateProvider;
5556
import com.vaadin.flow.function.SerializableConsumer;
5657
import com.vaadin.flow.internal.ConstantPool;
58+
import com.vaadin.flow.internal.JacksonCodec;
5759
import com.vaadin.flow.internal.JsonCodec;
5860
import com.vaadin.flow.internal.StateNode;
5961
import com.vaadin.flow.internal.StateTree;
@@ -84,6 +86,8 @@
8486
import com.vaadin.flow.shared.Registration;
8587
import com.vaadin.flow.shared.communication.PushMode;
8688

89+
import elemental.json.JsonValue;
90+
8791
/**
8892
* Holds UI-specific methods and data which are intended for internal use by the
8993
* framework.
@@ -130,7 +134,11 @@ public JavaScriptInvocation(String expression,
130134
*/
131135
for (Object argument : parameters) {
132136
// Throws IAE for unsupported types
133-
JsonCodec.encodeWithTypeInfo(argument);
137+
if (argument instanceof JsonValue) {
138+
JsonCodec.encodeWithTypeInfo(argument);
139+
} else {
140+
JacksonCodec.encodeWithTypeInfo(argument);
141+
}
134142
}
135143

136144
this.expression = expression;
@@ -1131,7 +1139,7 @@ public void refreshCurrentRoute(boolean refreshRouteChain) {
11311139
+ "Unable to refresh the current route.");
11321140
} else {
11331141
getRouter().navigate(ui, locationForRefresh,
1134-
NavigationTrigger.REFRESH_ROUTE, null, true,
1142+
NavigationTrigger.REFRESH_ROUTE, (BaseJsonNode) null, true,
11351143
refreshRouteChain || hasModalComponent());
11361144
}
11371145
}

flow-server/src/main/java/com/vaadin/flow/router/ErrorNavigationEvent.java

+29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.vaadin.flow.router;
1717

18+
import com.fasterxml.jackson.databind.node.BaseJsonNode;
19+
1820
import com.vaadin.flow.component.UI;
1921

2022
import elemental.json.JsonValue;
@@ -69,6 +71,7 @@ public ErrorNavigationEvent(Router router, Location location, UI ui,
6971
* includes navigation state info including for example the
7072
* scroll position and the complete href of the RouterLink
7173
*/
74+
@Deprecated
7275
public ErrorNavigationEvent(Router router, Location location, UI ui,
7376
NavigationTrigger trigger, ErrorParameter<?> errorParameter,
7477
JsonValue state) {
@@ -77,6 +80,32 @@ public ErrorNavigationEvent(Router router, Location location, UI ui,
7780
this.errorParameter = errorParameter;
7881
}
7982

83+
/**
84+
* Creates a new navigation event.
85+
*
86+
* @param router
87+
* the router handling the navigation, not {@code null}
88+
* @param location
89+
* the new location, not {@code null}
90+
* @param ui
91+
* the UI in which the navigation occurs, not {@code null}
92+
* @param trigger
93+
* the type of user action that triggered this navigation event,
94+
* not {@code null}
95+
* @param errorParameter
96+
* parameter containing navigation error information
97+
* @param state
98+
* includes navigation state info including for example the
99+
* scroll position and the complete href of the RouterLink
100+
*/
101+
public ErrorNavigationEvent(Router router, Location location, UI ui,
102+
NavigationTrigger trigger, ErrorParameter<?> errorParameter,
103+
BaseJsonNode state) {
104+
super(router, location, ui, trigger, state, false);
105+
106+
this.errorParameter = errorParameter;
107+
}
108+
80109
/**
81110
* Gets the ErrorParameter if set.
82111
*

flow-server/src/main/java/com/vaadin/flow/router/NavigationEvent.java

+72-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
import java.util.EventObject;
1919
import java.util.Optional;
2020

21+
import com.fasterxml.jackson.databind.node.BaseJsonNode;
22+
2123
import com.vaadin.flow.component.UI;
24+
import com.vaadin.flow.internal.JacksonUtils;
2225

2326
import elemental.json.JsonValue;
2427

@@ -33,7 +36,7 @@ public class NavigationEvent extends EventObject {
3336
private final UI ui;
3437
private final NavigationTrigger trigger;
3538
private boolean forwardTo = false;
36-
private JsonValue state = null;
39+
private BaseJsonNode state = null;
3740
private boolean forceInstantiation = false;
3841
private boolean recreateLayoutChain = false;
3942

@@ -82,11 +85,12 @@ public NavigationEvent(Router router, Location location, UI ui,
8285
* indicates if this event is created as a result of
8386
* {@link BeforeEvent#forwardTo} or not
8487
*/
88+
@Deprecated
8589
public NavigationEvent(Router router, Location location, UI ui,
8690
NavigationTrigger trigger, JsonValue state, boolean forwardTo) {
8791
this(router, location, ui, trigger);
8892

89-
this.state = state;
93+
this.state = JacksonUtils.mapElemental(state);
9094
this.forwardTo = forwardTo;
9195
}
9296

@@ -116,11 +120,76 @@ public NavigationEvent(Router router, Location location, UI ui,
116120
* navigation target will be re-instantiated. Requires
117121
* {@code forceInstantiation} to be true to have an effect.
118122
*/
123+
@Deprecated
119124
public NavigationEvent(Router router, Location location, UI ui,
120125
NavigationTrigger trigger, JsonValue state, boolean forwardTo,
121126
boolean forceInstantiation, boolean recreateLayoutChain) {
122127
this(router, location, ui, trigger);
123128

129+
this.state = JacksonUtils.mapElemental(state);
130+
this.forwardTo = forwardTo;
131+
this.forceInstantiation = forceInstantiation;
132+
this.recreateLayoutChain = recreateLayoutChain;
133+
}
134+
135+
/**
136+
* Creates a new navigation event.
137+
*
138+
* @param router
139+
* the router handling the navigation, not {@code null}
140+
* @param location
141+
* the new location, not {@code null}
142+
* @param ui
143+
* the UI in which the navigation occurs, not {@code null}
144+
* @param trigger
145+
* the type of user action that triggered this navigation event,
146+
* not {@code null}
147+
* @param state
148+
* includes navigation state info including for example the
149+
* scroll position and the complete href of the RouterLink
150+
* @param forwardTo
151+
* indicates if this event is created as a result of
152+
* {@link BeforeEvent#forwardTo} or not
153+
*/
154+
public NavigationEvent(Router router, Location location, UI ui,
155+
NavigationTrigger trigger, BaseJsonNode state, boolean forwardTo) {
156+
this(router, location, ui, trigger);
157+
158+
this.state = state;
159+
this.forwardTo = forwardTo;
160+
}
161+
162+
/**
163+
* Creates a new navigation event.
164+
*
165+
* @param router
166+
* the router handling the navigation, not {@code null}
167+
* @param location
168+
* the new location, not {@code null}
169+
* @param ui
170+
* the UI in which the navigation occurs, not {@code null}
171+
* @param trigger
172+
* the type of user action that triggered this navigation event,
173+
* not {@code null}
174+
* @param state
175+
* includes navigation state info including for example the
176+
* scroll position and the complete href of the RouterLink
177+
* @param forwardTo
178+
* indicates if this event is created as a result of
179+
* {@link BeforeEvent#forwardTo} or not
180+
* @param forceInstantiation
181+
* if set to {@code true}, the navigation target will always be
182+
* instantiated
183+
* @param recreateLayoutChain
184+
* if set to {@code true}, the complete layout chain up to the
185+
* navigation target will be re-instantiated. Requires
186+
* {@code forceInstantiation} to be true to have an effect.
187+
*/
188+
public NavigationEvent(Router router, Location location, UI ui,
189+
NavigationTrigger trigger, BaseJsonNode state, boolean forwardTo,
190+
boolean forceInstantiation, boolean recreateLayoutChain) {
191+
this(router, location, ui, trigger);
192+
124193
this.state = state;
125194
this.forwardTo = forwardTo;
126195
this.forceInstantiation = forceInstantiation;
@@ -166,7 +235,7 @@ public NavigationTrigger getTrigger() {
166235
*
167236
* @return the navigation state
168237
*/
169-
public Optional<JsonValue> getState() {
238+
public Optional<BaseJsonNode> getState() {
170239
return state == null ? Optional.empty() : Optional.of(state);
171240
}
172241

flow-server/src/main/java/com/vaadin/flow/router/Router.java

+75-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Optional;
2121

2222
import com.vaadin.flow.component.UI;
23+
import com.vaadin.flow.internal.JacksonUtils;
2324
import com.vaadin.flow.router.internal.DefaultRouteResolver;
2425
import com.vaadin.flow.router.internal.ErrorStateRenderer;
2526
import com.vaadin.flow.router.internal.ErrorTargetEntry;
@@ -34,6 +35,7 @@
3435
import com.vaadin.flow.server.VaadinService;
3536
import com.vaadin.flow.server.VaadinSession;
3637

38+
import com.fasterxml.jackson.databind.node.BaseJsonNode;
3739
import org.slf4j.LoggerFactory;
3840

3941
import elemental.json.JsonValue;
@@ -76,8 +78,10 @@ public Router(RouteRegistry registry) {
7678
* the location object of the route
7779
*/
7880
public void initializeUI(UI ui, Location location) {
79-
ui.getPage().getHistory().setHistoryStateChangeHandler(e -> navigate(ui,
80-
e.getLocation(), e.getTrigger(), e.getState().orElse(null)));
81+
ui.getPage().getHistory()
82+
.setHistoryStateChangeHandler(e -> navigate(ui, e.getLocation(),
83+
e.getTrigger(),
84+
JacksonUtils.mapElemental(e.getState().orElse(null))));
8185

8286
int statusCode = navigate(ui, location, NavigationTrigger.PAGE_LOAD);
8387

@@ -164,7 +168,7 @@ public Optional<NavigationState> resolveRouteNotFoundNavigationTarget() {
164168
* @see UI#navigate(String, QueryParameters)
165169
*/
166170
public int navigate(UI ui, Location location, NavigationTrigger trigger) {
167-
return navigate(ui, location, trigger, null);
171+
return navigate(ui, location, trigger, (BaseJsonNode) null);
168172
}
169173

170174
/**
@@ -189,7 +193,7 @@ public int navigate(UI ui, Location location, NavigationTrigger trigger) {
189193
* @see UI#navigate(String, QueryParameters)
190194
*/
191195
public int navigate(UI ui, Location location, NavigationTrigger trigger,
192-
JsonValue state) {
196+
BaseJsonNode state) {
193197
return navigate(ui, location, trigger, state, false, false);
194198
}
195199

@@ -222,7 +226,7 @@ public int navigate(UI ui, Location location, NavigationTrigger trigger,
222226
* @see UI#navigate(String, QueryParameters)
223227
*/
224228
public int navigate(UI ui, Location location, NavigationTrigger trigger,
225-
JsonValue state, boolean forceInstantiation,
229+
BaseJsonNode state, boolean forceInstantiation,
226230
boolean recreateLayoutChain) {
227231
assert ui != null;
228232
assert location != null;
@@ -243,6 +247,69 @@ public int navigate(UI ui, Location location, NavigationTrigger trigger,
243247
return HttpStatusCode.NOT_MODIFIED.getCode();
244248
}
245249

250+
/**
251+
* Navigates the given UI to the given location. For internal use only.
252+
* <p>
253+
* This method pushes to the browser history if the <code>trigger</code> is
254+
* {@link NavigationTrigger#ROUTER_LINK} or
255+
* {@link NavigationTrigger#UI_NAVIGATE}.
256+
*
257+
* @param ui
258+
* the UI to update, not <code>null</code>
259+
* @param location
260+
* the location to navigate to, not <code>null</code>
261+
* @param trigger
262+
* the type of user action that triggered this navigation, not
263+
* <code>null</code>
264+
* @param state
265+
* includes navigation state info including for example the
266+
* scroll position and the complete href of the RouterLink
267+
* @return the HTTP status code resulting from the navigation
268+
* @see UI#navigate(String)
269+
* @see UI#navigate(String, QueryParameters)
270+
*/
271+
@Deprecated
272+
public int navigate(UI ui, Location location, NavigationTrigger trigger,
273+
JsonValue state) {
274+
return navigate(ui, location, trigger, state, false, false);
275+
}
276+
277+
/**
278+
* Navigates the given UI to the given location. For internal use only.
279+
* <p>
280+
* This method pushes to the browser history if the <code>trigger</code> is
281+
* {@link NavigationTrigger#ROUTER_LINK} or
282+
* {@link NavigationTrigger#UI_NAVIGATE}.
283+
*
284+
* @param ui
285+
* the UI to update, not <code>null</code>
286+
* @param location
287+
* the location to navigate to, not <code>null</code>
288+
* @param trigger
289+
* the type of user action that triggered this navigation, not
290+
* <code>null</code>
291+
* @param state
292+
* includes navigation state info including for example the
293+
* scroll position and the complete href of the RouterLink
294+
* @param forceInstantiation
295+
* if set to {@code true}, the navigation target will always be
296+
* instantiated
297+
* @param recreateLayoutChain
298+
* if set to {@code true}, the complete layout chain up to the
299+
* navigation target will be re-instantiated. Requires
300+
* {@code forceInstantiation} to be true to have an effect.
301+
* @return the HTTP status code resulting from the navigation
302+
* @see UI#navigate(String)
303+
* @see UI#navigate(String, QueryParameters)
304+
*/
305+
@Deprecated
306+
public int navigate(UI ui, Location location, NavigationTrigger trigger,
307+
JsonValue state, boolean forceInstantiation,
308+
boolean recreateLayoutChain) {
309+
return navigate(ui, location, trigger, JacksonUtils.mapElemental(state),
310+
forceInstantiation, recreateLayoutChain);
311+
}
312+
246313
private boolean handleNavigationForLocation(UI ui, Location location) {
247314
if (ui.getInternals().hasLastHandledLocation()) {
248315
return !location.getPathWithQueryParameters()
@@ -253,7 +320,7 @@ private boolean handleNavigationForLocation(UI ui, Location location) {
253320
}
254321

255322
private int handleNavigation(UI ui, Location location,
256-
NavigationTrigger trigger, JsonValue state,
323+
NavigationTrigger trigger, BaseJsonNode state,
257324
boolean forceInstantiation, boolean recreateLayoutChain) {
258325
NavigationState newState = getRouteResolver()
259326
.resolve(new ResolveRequest(this, location));
@@ -284,7 +351,8 @@ private int handleNavigation(UI ui, Location location,
284351
}
285352

286353
private int handleExceptionNavigation(UI ui, Location location,
287-
Exception exception, NavigationTrigger trigger, JsonValue state) {
354+
Exception exception, NavigationTrigger trigger,
355+
BaseJsonNode state) {
288356
Optional<ErrorTargetEntry> maybeLookupResult = getErrorNavigationTarget(
289357
exception);
290358

0 commit comments

Comments
 (0)