|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.lang.annotation.Annotation;
|
20 | 20 | import java.util.Collection;
|
21 |
| -import java.util.Collections; |
22 | 21 | import java.util.Map;
|
23 | 22 |
|
24 | 23 | import reactor.core.publisher.Mono;
|
|
29 | 28 | import org.springframework.core.ResolvableType;
|
30 | 29 | import org.springframework.lang.Nullable;
|
31 | 30 | import org.springframework.ui.Model;
|
| 31 | +import org.springframework.util.CollectionUtils; |
32 | 32 | import org.springframework.validation.BindingResult;
|
33 | 33 | import org.springframework.validation.DataBinder;
|
34 | 34 | import org.springframework.validation.SmartValidator;
|
@@ -209,10 +209,29 @@ public ExtendedWebExchangeDataBinder(@Nullable Object target, String objectName)
|
209 | 209 |
|
210 | 210 | @Override
|
211 | 211 | public Mono<Map<String, Object>> getValuesToBind(ServerWebExchange exchange) {
|
212 |
| - return super.getValuesToBind(exchange).doOnNext(map -> |
213 |
| - map.putAll(exchange.<Map<String, String>>getAttributeOrDefault( |
214 |
| - HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, Collections.emptyMap()))); |
| 212 | + return super.getValuesToBind(exchange).doOnNext(map -> { |
| 213 | + Map<String, String> vars = exchange.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); |
| 214 | + if (!CollectionUtils.isEmpty(vars)) { |
| 215 | + vars.forEach((key, value) -> addValueIfNotPresent(map, "URI variable", key, value)); |
| 216 | + } |
| 217 | + }); |
215 | 218 | }
|
| 219 | + |
| 220 | + private static void addValueIfNotPresent( |
| 221 | + Map<String, Object> map, String label, String name, @Nullable Object value) { |
| 222 | + |
| 223 | + if (value != null) { |
| 224 | + if (map.containsKey(name)) { |
| 225 | + if (logger.isDebugEnabled()) { |
| 226 | + logger.debug(label + " '" + name + "' overridden by request bind value."); |
| 227 | + } |
| 228 | + } |
| 229 | + else { |
| 230 | + map.put(name, value); |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + |
216 | 235 | }
|
217 | 236 |
|
218 | 237 |
|
|
0 commit comments