Skip to content

Commit

Permalink
Make URI template variables nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Jan 10, 2025
1 parent bce7d87 commit ec48c47
Show file tree
Hide file tree
Showing 33 changed files with 172 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -119,7 +119,7 @@ public <T> T getNativeRequest() {
* @param uriVars zero or more URI variables
* @return the created builder
*/
public static BaseBuilder<?> get(String urlTemplate, Object... uriVars) {
public static BaseBuilder<?> get(String urlTemplate, @Nullable Object... uriVars) {
return method(HttpMethod.GET, urlTemplate, uriVars);
}

Expand All @@ -129,7 +129,7 @@ public static BaseBuilder<?> get(String urlTemplate, Object... uriVars) {
* @param uriVars zero or more URI variables
* @return the created builder
*/
public static BaseBuilder<?> head(String urlTemplate, Object... uriVars) {
public static BaseBuilder<?> head(String urlTemplate, @Nullable Object... uriVars) {
return method(HttpMethod.HEAD, urlTemplate, uriVars);
}

Expand All @@ -139,7 +139,7 @@ public static BaseBuilder<?> head(String urlTemplate, Object... uriVars) {
* @param uriVars zero or more URI variables
* @return the created builder
*/
public static BodyBuilder post(String urlTemplate, Object... uriVars) {
public static BodyBuilder post(String urlTemplate, @Nullable Object... uriVars) {
return method(HttpMethod.POST, urlTemplate, uriVars);
}

Expand All @@ -150,7 +150,7 @@ public static BodyBuilder post(String urlTemplate, Object... uriVars) {
* @param uriVars zero or more URI variables
* @return the created builder
*/
public static BodyBuilder put(String urlTemplate, Object... uriVars) {
public static BodyBuilder put(String urlTemplate, @Nullable Object... uriVars) {
return method(HttpMethod.PUT, urlTemplate, uriVars);
}

Expand All @@ -160,7 +160,7 @@ public static BodyBuilder put(String urlTemplate, Object... uriVars) {
* @param uriVars zero or more URI variables
* @return the created builder
*/
public static BodyBuilder patch(String urlTemplate, Object... uriVars) {
public static BodyBuilder patch(String urlTemplate, @Nullable Object... uriVars) {
return method(HttpMethod.PATCH, urlTemplate, uriVars);
}

Expand All @@ -170,7 +170,7 @@ public static BodyBuilder patch(String urlTemplate, Object... uriVars) {
* @param uriVars zero or more URI variables
* @return the created builder
*/
public static BaseBuilder<?> delete(String urlTemplate, Object... uriVars) {
public static BaseBuilder<?> delete(String urlTemplate, @Nullable Object... uriVars) {
return method(HttpMethod.DELETE, urlTemplate, uriVars);
}

Expand All @@ -180,7 +180,7 @@ public static BaseBuilder<?> delete(String urlTemplate, Object... uriVars) {
* @param uriVars zero or more URI variables
* @return the created builder
*/
public static BaseBuilder<?> options(String urlTemplate, Object... uriVars) {
public static BaseBuilder<?> options(String urlTemplate, @Nullable Object... uriVars) {
return method(HttpMethod.OPTIONS, urlTemplate, uriVars);
}

Expand All @@ -205,7 +205,7 @@ public static BodyBuilder method(HttpMethod method, URI url) {
* @param vars variables to expand into the template
* @return the created builder
*/
public static BodyBuilder method(HttpMethod method, String uri, Object... vars) {
public static BodyBuilder method(HttpMethod method, String uri, @Nullable Object... vars) {
return method(method, toUri(uri, vars));
}

Expand All @@ -220,12 +220,12 @@ public static BodyBuilder method(HttpMethod method, String uri, Object... vars)
* @deprecated as of Spring Framework 6.0 in favor of {@link #method(HttpMethod, String, Object...)}
*/
@Deprecated(since = "6.0")
public static BodyBuilder method(String httpMethod, String uri, Object... vars) {
public static BodyBuilder method(String httpMethod, String uri, @Nullable Object... vars) {
Assert.hasText(httpMethod, "HTTP method is required.");
return new DefaultBodyBuilder(HttpMethod.valueOf(httpMethod), toUri(uri, vars));
}

private static URI toUri(String uri, Object[] vars) {
private static URI toUri(String uri, @Nullable Object[] vars) {
return UriComponentsBuilder.fromUriString(uri).buildAndExpand(vars).encode().toUri();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public UriAssert(@Nullable String actual, String displayName) {
* @param uriVars the values to replace the URI template variables
* @see UriComponentsBuilder#buildAndExpand(Object...)
*/
public UriAssert isEqualToTemplate(String uriTemplate, Object... uriVars) {
public UriAssert isEqualToTemplate(String uriTemplate, @Nullable Object... uriVars) {
String uri = buildUri(uriTemplate, uriVars);
return isEqualTo(uri);
}
Expand All @@ -81,7 +81,7 @@ public UriAssert matchesAntPattern(String uriPattern) {
}


private String buildUri(String uriTemplate, Object... uriVars) {
private String buildUri(String uriTemplate, @Nullable Object... uriVars) {
try {
return UriComponentsBuilder.fromUriString(uriTemplate)
.buildAndExpand(uriVars).encode().toUriString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 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.
Expand All @@ -23,6 +23,7 @@
import javax.xml.xpath.XPathExpressionException;

import org.hamcrest.Matcher;
import org.jspecify.annotations.Nullable;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -98,7 +99,7 @@ public static RequestMatcher requestTo(String expectedUri) {
* @param uriVars zero or more URI variables to populate the expected URI
* @return the request matcher
*/
public static RequestMatcher requestToUriTemplate(String expectedUri, Object... uriVars) {
public static RequestMatcher requestToUriTemplate(String expectedUri, @Nullable Object... uriVars) {
Assert.notNull(expectedUri, "'uri' must not be null");
URI uri = UriComponentsBuilder.fromUriString(expectedUri).buildAndExpand(uriVars).encode().toUri();
return requestTo(uri);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -202,13 +202,13 @@ private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec {
}

@Override
public RequestBodySpec uri(String uriTemplate, Object... uriVariables) {
public RequestBodySpec uri(String uriTemplate, @Nullable Object... uriVariables) {
this.uriTemplate = uriTemplate;
return uri(DefaultWebTestClient.this.uriBuilderFactory.expand(uriTemplate, uriVariables));
}

@Override
public RequestBodySpec uri(String uriTemplate, Map<String, ?> uriVariables) {
public RequestBodySpec uri(String uriTemplate, Map<String, ? extends @Nullable Object> uriVariables) {
this.uriTemplate = uriTemplate;
return uri(DefaultWebTestClient.this.uriBuilderFactory.expand(uriTemplate, uriVariables));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,15 +557,15 @@ interface UriSpec<S extends RequestHeadersSpec<?>> {
* with a base URI) it will be used to expand the URI template.
* @return spec to add headers or perform the exchange
*/
S uri(String uri, Object... uriVariables);
S uri(String uri, @Nullable Object... uriVariables);

/**
* Specify the URI for the request using a URI template and URI variables.
* <p>If a {@link UriBuilderFactory} was configured for the client (for example,
* with a base URI) it will be used to expand the URI template.
* @return spec to add headers or perform the exchange
*/
S uri(String uri, Map<String, ?> uriVariables);
S uri(String uri, Map<String, ? extends @Nullable Object> uriVariables);

/**
* Build the URI for the request with a {@link UriBuilder} obtained
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -151,7 +151,7 @@ public B uri(URI uri) {
/**
* Specify the URI for the request using a URI template and URI variables.
*/
public B uri(String uriTemplate, Object... uriVariables) {
public B uri(String uriTemplate, @Nullable Object... uriVariables) {
return updateUri(initUri(uriTemplate, uriVariables), uriTemplate);
}

Expand All @@ -161,7 +161,7 @@ private B updateUri(URI uri, @Nullable String uriTemplate) {
return self();
}

private static URI initUri(String uri, Object[] vars) {
private static URI initUri(String uri, @Nullable Object[] vars) {
Assert.notNull(uri, "'uri' must not be null");
Assert.isTrue(uri.isEmpty() || uri.startsWith("/") || uri.startsWith("http://") || uri.startsWith("https://"),
() -> "'uri' should start with a path or be a complete HTTP URI: " + uri);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -75,7 +75,7 @@ public MockHttpServletRequestBuilder uri(URI uri) {
}

@Override
public MockHttpServletRequestBuilder uri(String uriTemplate, Object... uriVariables) {
public MockHttpServletRequestBuilder uri(String uriTemplate, @Nullable Object... uriVariables) {
return super.uri(uriTemplate, uriVariables);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand All @@ -19,6 +19,7 @@
import java.net.URI;

import jakarta.servlet.DispatcherType;
import org.jspecify.annotations.Nullable;

import org.springframework.http.HttpMethod;
import org.springframework.mock.web.MockHttpServletRequest;
Expand Down Expand Up @@ -52,7 +53,7 @@ public abstract class MockMvcRequestBuilders {
* @param uriTemplate a URI template; the resulting URI will be encoded
* @param uriVariables zero or more URI variables
*/
public static MockHttpServletRequestBuilder get(String uriTemplate, Object... uriVariables) {
public static MockHttpServletRequestBuilder get(String uriTemplate, @Nullable Object... uriVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.GET).uri(uriTemplate, uriVariables);
}

Expand All @@ -70,7 +71,7 @@ public static MockHttpServletRequestBuilder get(URI uri) {
* @param uriTemplate a URI template; the resulting URI will be encoded
* @param uriVariables zero or more URI variables
*/
public static MockHttpServletRequestBuilder post(String uriTemplate, Object... uriVariables) {
public static MockHttpServletRequestBuilder post(String uriTemplate, @Nullable Object... uriVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.POST).uri(uriTemplate, uriVariables);
}

Expand All @@ -88,7 +89,7 @@ public static MockHttpServletRequestBuilder post(URI uri) {
* @param uriTemplate a URI template; the resulting URI will be encoded
* @param uriVariables zero or more URI variables
*/
public static MockHttpServletRequestBuilder put(String uriTemplate, Object... uriVariables) {
public static MockHttpServletRequestBuilder put(String uriTemplate, @Nullable Object... uriVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.PUT).uri(uriTemplate, uriVariables);
}

Expand All @@ -106,7 +107,7 @@ public static MockHttpServletRequestBuilder put(URI uri) {
* @param uriTemplate a URI template; the resulting URI will be encoded
* @param uriVariables zero or more URI variables
*/
public static MockHttpServletRequestBuilder patch(String uriTemplate, Object... uriVariables) {
public static MockHttpServletRequestBuilder patch(String uriTemplate,@Nullable Object... uriVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.PATCH).uri(uriTemplate, uriVariables);
}

Expand All @@ -124,7 +125,7 @@ public static MockHttpServletRequestBuilder patch(URI uri) {
* @param uriTemplate a URI template; the resulting URI will be encoded
* @param uriVariables zero or more URI variables
*/
public static MockHttpServletRequestBuilder delete(String uriTemplate, Object... uriVariables) {
public static MockHttpServletRequestBuilder delete(String uriTemplate, @Nullable Object... uriVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.DELETE).uri(uriTemplate, uriVariables);
}

Expand All @@ -142,7 +143,7 @@ public static MockHttpServletRequestBuilder delete(URI uri) {
* @param uriTemplate a URI template; the resulting URI will be encoded
* @param uriVariables zero or more URI variables
*/
public static MockHttpServletRequestBuilder options(String uriTemplate, Object... uriVariables) {
public static MockHttpServletRequestBuilder options(String uriTemplate, @Nullable Object... uriVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.OPTIONS).uri(uriTemplate, uriVariables);
}

Expand All @@ -161,7 +162,7 @@ public static MockHttpServletRequestBuilder options(URI uri) {
* @param uriVariables zero or more URI variables
* @since 4.1
*/
public static MockHttpServletRequestBuilder head(String uriTemplate, Object... uriVariables) {
public static MockHttpServletRequestBuilder head(String uriTemplate, @Nullable Object... uriVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.HEAD).uri(uriTemplate, uriVariables);
}

Expand All @@ -180,7 +181,7 @@ public static MockHttpServletRequestBuilder head(URI uri) {
* @param uriTemplate a URI template; the resulting URI will be encoded
* @param uriVariables zero or more URI variables
*/
public static MockHttpServletRequestBuilder request(HttpMethod method, String uriTemplate, Object... uriVariables) {
public static MockHttpServletRequestBuilder request(HttpMethod method, String uriTemplate, @Nullable Object... uriVariables) {
return new MockHttpServletRequestBuilder(method).uri(uriTemplate, uriVariables);
}

Expand Down Expand Up @@ -213,7 +214,7 @@ public static MockHttpServletRequestBuilder request(String httpMethod, URI uri)
* @param uriVariables zero or more URI variables
* @since 5.0
*/
public static MockMultipartHttpServletRequestBuilder multipart(String uriTemplate, Object... uriVariables) {
public static MockMultipartHttpServletRequestBuilder multipart(String uriTemplate, @Nullable Object... uriVariables) {
MockMultipartHttpServletRequestBuilder builder = new MockMultipartHttpServletRequestBuilder();
builder.uri(uriTemplate, uriVariables);
return builder;
Expand All @@ -227,7 +228,7 @@ public static MockMultipartHttpServletRequestBuilder multipart(String uriTemplat
* @param uriVariables zero or more URI variables
* @since 5.3.22
*/
public static MockMultipartHttpServletRequestBuilder multipart(HttpMethod httpMethod, String uriTemplate, Object... uriVariables) {
public static MockMultipartHttpServletRequestBuilder multipart(HttpMethod httpMethod, String uriTemplate, @Nullable Object... uriVariables) {
MockMultipartHttpServletRequestBuilder builder = new MockMultipartHttpServletRequestBuilder(httpMethod);
builder.uri(uriTemplate, uriVariables);
return builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -98,7 +98,7 @@ public static ResultMatcher forwardedUrl(@Nullable String expectedUrl) {
* @param uriVars zero or more URI variables to populate the template
* @see UriComponentsBuilder#fromUriString(String)
*/
public static ResultMatcher forwardedUrlTemplate(String urlTemplate, Object... uriVars) {
public static ResultMatcher forwardedUrlTemplate(String urlTemplate, @Nullable Object... uriVars) {
String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString();
return forwardedUrl(uri);
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public static ResultMatcher redirectedUrl(String expectedUrl) {
* @param uriVars zero or more URI variables to populate the template
* @see UriComponentsBuilder#fromUriString(String)
*/
public static ResultMatcher redirectedUrlTemplate(String urlTemplate, Object... uriVars) {
public static ResultMatcher redirectedUrlTemplate(String urlTemplate, @Nullable Object... uriVars) {
String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString();
return redirectedUrl(uri);
}
Expand Down
Loading

0 comments on commit ec48c47

Please sign in to comment.