Improve expanding in MvcUriComponentsBuilder

Before this change MvcUriComponentsBuilder could not create a
UriComponentsBuilder for methods where the mapping has a URI variable
and no matching method argument for it.

For example a URI variable may be in the type-level mapping but not
all methods may have an @PathVariable argument for it.

This fix addresses the shortcoming such that MvcUriComponentsBuilder
expands the method argument values available to it and leaves remaining
URI variables to be further expanded via UriComponents.expand().

Issue: SPR-11391
This commit is contained in:
Rossen Stoyanchev
2014-02-06 16:46:28 -05:00
parent bdb742b8db
commit 42d0470d94
4 changed files with 57 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -218,6 +218,9 @@ public abstract class UriComponents implements Serializable {
String match = matcher.group(1);
String variableName = getVariableName(match);
Object variableValue = uriVariables.getValue(variableName);
if (UriTemplateVariables.SKIP_VALUE.equals(variableValue)) {
continue;
}
String variableValueString = getVariableValueAsString(variableValue);
String replacement = Matcher.quoteReplacement(variableValueString);
matcher.appendReplacement(sb, replacement);
@@ -242,6 +245,16 @@ public abstract class UriComponents implements Serializable {
*/
public interface UriTemplateVariables {
public static final Object SKIP_VALUE = UriTemplateVariables.class;
/**
* Get the value for the given URI variable name.
* If the value is {@code null}, an empty String is expanded.
* If the value is {@link #SKIP_VALUE}, the URI variable is not expanded.
*
* @param name the variable name
* @return the variable value, possibly {@code null} or {@link #SKIP_VALUE}
*/
Object getValue(String name);
}

View File

@@ -363,7 +363,7 @@ public class UriComponentsBuilder {
}
/**
* Initializes all components of this URI builder from the given {@link UriComponents}.
* Set all components of this URI builder from the given {@link UriComponents}.
* @param uriComponents the UriComponents instance
* @return this UriComponentsBuilder
*/
@@ -385,7 +385,14 @@ public class UriComponentsBuilder {
this.port = uriComponents.getPort();
}
if (StringUtils.hasLength(uriComponents.getPath())) {
this.pathBuilder = new CompositePathComponentBuilder(uriComponents.getPath());
List<String> segments = uriComponents.getPathSegments();
if (segments.isEmpty()) {
// Perhaps "/"
this.pathBuilder.addPath(uriComponents.getPath());
}
else {
this.pathBuilder.addPathSegments(segments.toArray(new String[segments.size()]));
}
}
if (!uriComponents.getQueryParams().isEmpty()) {
this.queryParams.clear();