Skip to content

Commit

Permalink
Fix compatibility problems with Spring Framework 6.2
Browse files Browse the repository at this point in the history
Fixes gh-934
  • Loading branch information
wilkinsona committed Jul 18, 2024
1 parent 9743b45 commit cf451c9
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 29 deletions.
2 changes: 1 addition & 1 deletion spring-restdocs-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ components.java.withVariantsFromConfiguration(configurations.testFixturesRuntime
compatibilityTest {
dependency("Spring Framework") { springFramework ->
springFramework.groupId = "org.springframework"
springFramework.versions = ["6.1.+"]
springFramework.versions = ["6.1.+", "6.2.+"]
}
}
2 changes: 1 addition & 1 deletion spring-restdocs-mockmvc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ dependencies {
compatibilityTest {
dependency("Spring Framework") { springFramework ->
springFramework.groupId = "org.springframework"
springFramework.versions = ["6.1.+"]
springFramework.versions = ["6.1.+", "6.2.+"]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 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 @@ -51,8 +51,8 @@ private RestDocumentationRequestBuilders() {
* @return the builder for the GET request
*/
public static MockHttpServletRequestBuilder get(String urlTemplate, Object... urlVariables) {
return MockMvcRequestBuilders.get(urlTemplate, urlVariables)
.requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate);
return (MockHttpServletRequestBuilder) configureUrlTemplateRequestAttribute(
MockMvcRequestBuilders.get(urlTemplate, urlVariables), urlTemplate);
}

/**
Expand All @@ -72,8 +72,8 @@ public static MockHttpServletRequestBuilder get(URI uri) {
* @return the builder for the POST request
*/
public static MockHttpServletRequestBuilder post(String urlTemplate, Object... urlVariables) {
return MockMvcRequestBuilders.post(urlTemplate, urlVariables)
.requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate);
return (MockHttpServletRequestBuilder) configureUrlTemplateRequestAttribute(
MockMvcRequestBuilders.post(urlTemplate, urlVariables), urlTemplate);
}

/**
Expand All @@ -93,8 +93,8 @@ public static MockHttpServletRequestBuilder post(URI uri) {
* @return the builder for the PUT request
*/
public static MockHttpServletRequestBuilder put(String urlTemplate, Object... urlVariables) {
return MockMvcRequestBuilders.put(urlTemplate, urlVariables)
.requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate);
return (MockHttpServletRequestBuilder) configureUrlTemplateRequestAttribute(
MockMvcRequestBuilders.put(urlTemplate, urlVariables), urlTemplate);
}

/**
Expand All @@ -114,8 +114,8 @@ public static MockHttpServletRequestBuilder put(URI uri) {
* @return the builder for the PATCH request
*/
public static MockHttpServletRequestBuilder patch(String urlTemplate, Object... urlVariables) {
return MockMvcRequestBuilders.patch(urlTemplate, urlVariables)
.requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate);
return (MockHttpServletRequestBuilder) configureUrlTemplateRequestAttribute(
MockMvcRequestBuilders.patch(urlTemplate, urlVariables), urlTemplate);
}

/**
Expand All @@ -135,8 +135,8 @@ public static MockHttpServletRequestBuilder patch(URI uri) {
* @return the builder for the DELETE request
*/
public static MockHttpServletRequestBuilder delete(String urlTemplate, Object... urlVariables) {
return MockMvcRequestBuilders.delete(urlTemplate, urlVariables)
.requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate);
return (MockHttpServletRequestBuilder) configureUrlTemplateRequestAttribute(
MockMvcRequestBuilders.delete(urlTemplate, urlVariables), urlTemplate);
}

/**
Expand All @@ -156,8 +156,8 @@ public static MockHttpServletRequestBuilder delete(URI uri) {
* @return the builder for the OPTIONS request
*/
public static MockHttpServletRequestBuilder options(String urlTemplate, Object... urlVariables) {
return MockMvcRequestBuilders.options(urlTemplate, urlVariables)
.requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate);
return (MockHttpServletRequestBuilder) configureUrlTemplateRequestAttribute(
MockMvcRequestBuilders.options(urlTemplate, urlVariables), urlTemplate);
}

/**
Expand All @@ -177,8 +177,8 @@ public static MockHttpServletRequestBuilder options(URI uri) {
* @return the builder for the HEAD request
*/
public static MockHttpServletRequestBuilder head(String urlTemplate, Object... urlVariables) {
return MockMvcRequestBuilders.head(urlTemplate, urlVariables)
.requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate);
return (MockHttpServletRequestBuilder) configureUrlTemplateRequestAttribute(
MockMvcRequestBuilders.head(urlTemplate, urlVariables), urlTemplate);
}

/**
Expand All @@ -200,8 +200,8 @@ public static MockHttpServletRequestBuilder head(URI uri) {
*/
public static MockHttpServletRequestBuilder request(HttpMethod httpMethod, String urlTemplate,
Object... urlVariables) {
return MockMvcRequestBuilders.request(httpMethod, urlTemplate, urlVariables)
.requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate);
return (MockHttpServletRequestBuilder) configureUrlTemplateRequestAttribute(
MockMvcRequestBuilders.request(httpMethod, urlTemplate, urlVariables), urlTemplate);
}

/**
Expand All @@ -224,8 +224,8 @@ public static MockHttpServletRequestBuilder request(HttpMethod httpMethod, URI u
* @since 2.0.6
*/
public static MockMultipartHttpServletRequestBuilder multipart(String urlTemplate, Object... urlVariables) {
return (MockMultipartHttpServletRequestBuilder) MockMvcRequestBuilders.multipart(urlTemplate, urlVariables)
.requestAttr(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate);
return (MockMultipartHttpServletRequestBuilder) configureUrlTemplateRequestAttribute(
MockMvcRequestBuilders.multipart(urlTemplate, urlVariables), urlTemplate);
}

/**
Expand All @@ -238,4 +238,15 @@ public static MockMultipartHttpServletRequestBuilder multipart(URI uri) {
return MockMvcRequestBuilders.multipart(uri);
}

private static Object configureUrlTemplateRequestAttribute(Object builder, String urlTemplate) {
try {
return builder.getClass()
.getMethod("requestAttr", String.class, Object.class)
.invoke(builder, RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 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 @@ -34,6 +34,7 @@
import org.springframework.restdocs.operation.OperationRequestPart;
import org.springframework.restdocs.operation.RequestCookie;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -236,4 +237,8 @@ private OperationRequest createOperationRequest(MockHttpServletRequestBuilder bu
return this.factory.convert(builder.buildRequest(new MockServletContext()));
}

private OperationRequest createOperationRequest(MockMultipartHttpServletRequestBuilder builder) {
return this.factory.convert(builder.buildRequest(new MockServletContext()));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 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 @@ -26,6 +26,7 @@
import org.springframework.mock.web.MockServletContext;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
Expand Down Expand Up @@ -140,6 +141,15 @@ public void multipartUri() {

private void assertTemplate(MockHttpServletRequestBuilder builder, HttpMethod httpMethod) {
MockHttpServletRequest request = builder.buildRequest(this.servletContext);
assertTemplate(httpMethod, request);
}

private void assertTemplate(MockMultipartHttpServletRequestBuilder builder, HttpMethod httpMethod) {
MockHttpServletRequest request = builder.buildRequest(this.servletContext);
assertTemplate(httpMethod, request);
}

private void assertTemplate(HttpMethod httpMethod, MockHttpServletRequest request) {
assertThat((String) request.getAttribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE))
.isEqualTo("/{template}");
assertThat(request.getRequestURI()).isEqualTo("/t");
Expand All @@ -148,6 +158,15 @@ private void assertTemplate(MockHttpServletRequestBuilder builder, HttpMethod ht

private void assertUri(MockHttpServletRequestBuilder builder, HttpMethod httpMethod) {
MockHttpServletRequest request = builder.buildRequest(this.servletContext);
assertUri(httpMethod, request);
}

private void assertUri(MockMultipartHttpServletRequestBuilder builder, HttpMethod httpMethod) {
MockHttpServletRequest request = builder.buildRequest(this.servletContext);
assertUri(httpMethod, request);
}

private void assertUri(HttpMethod httpMethod, MockHttpServletRequest request) {
assertThat(request.getRequestURI()).isEqualTo("/uri");
assertThat(request.getMethod()).isEqualTo(httpMethod.name());
}
Expand Down
2 changes: 1 addition & 1 deletion spring-restdocs-webtestclient/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ dependencies {
compatibilityTest {
dependency("Spring Framework") { springFramework ->
springFramework.groupId = "org.springframework"
springFramework.versions = ["6.1.+"]
springFramework.versions = ["6.1.+", "6.2.+"]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 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 @@ -173,8 +173,10 @@ public void postRequestWithFormDataParameters() {
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getContentAsString()).isEqualTo("a=alpha&a=apple&b=br%26vo");
assertThat(request.getHeaders().getContentType())
.isEqualTo(new MediaType(MediaType.APPLICATION_FORM_URLENCODED, StandardCharsets.UTF_8));
assertThat(request.getHeaders().getContentType()).satisfiesAnyOf(
(mediaType) -> assertThat(mediaType)
.isEqualTo(new MediaType(MediaType.APPLICATION_FORM_URLENCODED, StandardCharsets.UTF_8)),
(mediaType) -> assertThat(mediaType).isEqualTo(new MediaType(MediaType.APPLICATION_FORM_URLENCODED)));
}

@Test
Expand Down Expand Up @@ -217,8 +219,10 @@ public void postRequestWithQueryStringAndFormDataParameters() {
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo?a=alpha&b=br%26vo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getContentAsString()).isEqualTo("a=apple");
assertThat(request.getHeaders().getContentType())
.isEqualTo(new MediaType(MediaType.APPLICATION_FORM_URLENCODED, StandardCharsets.UTF_8));
assertThat(request.getHeaders().getContentType()).satisfiesAnyOf(
(mediaType) -> assertThat(mediaType)
.isEqualTo(new MediaType(MediaType.APPLICATION_FORM_URLENCODED, StandardCharsets.UTF_8)),
(mediaType) -> assertThat(mediaType).isEqualTo(new MediaType(MediaType.APPLICATION_FORM_URLENCODED)));
}

@Test
Expand Down

0 comments on commit cf451c9

Please sign in to comment.