Skip to content

Commit f1bc790

Browse files
gregturnodrotbohm
authored andcommitted
#1172 - Provide static helpers to build TemplateVariable instance.
We now expose static factory methods to make code setting up a UriTemplate with TemplateVariable instances. Original pull request: #1192.
1 parent f297bb2 commit f1bc790

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/main/java/org/springframework/hateoas/TemplateVariable.java

+61-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,66 @@ public TemplateVariable(String name, TemplateVariable.VariableType type, String
7171
this.description = description;
7272
}
7373

74+
/**
75+
* Static helper to fashion {@link VariableType#PATH_VARIABLE} variables.
76+
*
77+
* @param pathVariable
78+
* @return
79+
*/
80+
public static TemplateVariable pathVariable(String pathVariable) {
81+
return new TemplateVariable(pathVariable, VariableType.PATH_VARIABLE);
82+
}
83+
84+
/**
85+
* Static helper to fashion {@link VariableType#REQUEST_PARAM} variables.
86+
*
87+
* @param requestParam
88+
* @return
89+
*/
90+
public static TemplateVariable requestParam(String requestParam) {
91+
return new TemplateVariable(requestParam, VariableType.REQUEST_PARAM);
92+
}
93+
94+
/**
95+
* Static helper to fashion {@link VariableType#REQUEST_PARAM_CONTINUED} variables.
96+
*
97+
* @param requestParam
98+
* @return
99+
*/
100+
public static TemplateVariable requestParamContinued(String requestParam) {
101+
return new TemplateVariable(requestParam, VariableType.REQUEST_PARAM_CONTINUED);
102+
}
103+
104+
/**
105+
* Static helper to fashion {@link VariableType#SEGMENT} variables.
106+
*
107+
* @param segment
108+
* @return
109+
*/
110+
public static TemplateVariable segment(String segment) {
111+
return new TemplateVariable(segment, VariableType.SEGMENT);
112+
}
113+
114+
/**
115+
* Static helper to fashion {@link VariableType#FRAGMENT} variables.
116+
*
117+
* @param fragment
118+
* @return
119+
*/
120+
public static TemplateVariable fragment(String fragment) {
121+
return new TemplateVariable(fragment, VariableType.FRAGMENT);
122+
}
123+
124+
/**
125+
* Static helper to fashion {@link VariableType#COMPOSITE_PARAM} variables.
126+
*
127+
* @param compositeParam
128+
* @return
129+
*/
130+
public static TemplateVariable compositeParam(String compositeParam) {
131+
return new TemplateVariable(compositeParam, VariableType.COMPOSITE_PARAM);
132+
}
133+
74134
/**
75135
* Returns whether the variable has a description.
76136
*
@@ -192,7 +252,7 @@ public static TemplateVariable.VariableType from(String key) {
192252
.orElseThrow(() -> new IllegalArgumentException("Unsupported variable type " + key + "!"));
193253
}
194254

195-
/*
255+
/*
196256
* (non-Javadoc)
197257
* @see java.lang.Enum#toString()
198258
*/

src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.hateoas;
1717

1818
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.hateoas.TemplateVariable.*;
1920
import static org.springframework.hateoas.UriTemplateUnitTest.EncodingFixture.*;
2021

2122
import java.io.ByteArrayInputStream;
@@ -38,7 +39,7 @@
3839
import org.junit.jupiter.api.Test;
3940
import org.junit.jupiter.params.ParameterizedTest;
4041
import org.junit.jupiter.params.provider.MethodSource;
41-
import org.springframework.hateoas.TemplateVariable.VariableType;
42+
import org.springframework.hateoas.TemplateVariable.*;
4243

4344
/**
4445
* Unit tests for {@link UriTemplate}.
@@ -300,6 +301,25 @@ void expandsTemplateWithAddedVariable() {
300301
assertThat(template.expand("value").toString()).isEqualTo("/foo?bar=value");
301302
}
302303

304+
@Test // 1172
305+
void useHelperMethodsToBuildUriTemplates() {
306+
307+
assertThat(UriTemplate.of("/foo").with(pathVariable("var")).getVariableNames()).containsExactly("var");
308+
309+
assertThat(UriTemplate.of("/foo").with(requestParam("var")).with(requestParamContinued("var2")).toString())
310+
.isEqualTo("/foo{?var,var2}");
311+
assertThat(UriTemplate.of("/foo").with(requestParam("var")).with(requestParam("var2")).toString())
312+
.isEqualTo("/foo{?var,var2}");
313+
314+
assertThat(UriTemplate.of("/foo").with(requestParamContinued("var2")).toString()).isEqualTo("/foo{&var2}");
315+
316+
assertThat(UriTemplate.of("/foo").with(segment("var")).toString()).isEqualTo("/foo{/var}");
317+
318+
assertThat(UriTemplate.of("/foo").with(fragment("var")).toString()).isEqualTo("/foo{#var}");
319+
320+
assertThat(UriTemplate.of("/foo").with(compositeParam("var")).toString()).isEqualTo("/foo{*var}");
321+
}
322+
303323
private static void assertVariables(UriTemplate template, TemplateVariable... variables) {
304324
assertVariables(template, Arrays.asList(variables));
305325
}

0 commit comments

Comments
 (0)