Browse Source

Use spring.restdocs as prefix for Spring REST Docs properties

Closes gh-47481
pull/47549/head
Andy Wilkinson 2 months ago
parent
commit
a502f4db8d
  1. 5
      module/spring-boot-restdocs/src/main/java/org/springframework/boot/restdocs/test/autoconfigure/AutoConfigureRestDocs.java
  2. 10
      module/spring-boot-restdocs/src/main/java/org/springframework/boot/restdocs/test/autoconfigure/RestDocsMockMvcBuilderCustomizer.java
  3. 44
      module/spring-boot-restdocs/src/main/java/org/springframework/boot/restdocs/test/autoconfigure/RestDocsProperties.java
  4. 8
      module/spring-boot-restdocs/src/main/java/org/springframework/boot/restdocs/test/autoconfigure/RestDocsRestAssuredBuilderCustomizer.java
  5. 8
      module/spring-boot-restdocs/src/main/java/org/springframework/boot/restdocs/test/autoconfigure/RestDocsWebTestClientBuilderCustomizer.java
  6. 35
      module/spring-boot-restdocs/src/main/resources/META-INF/additional-spring-configuration-metadata.json

5
module/spring-boot-restdocs/src/main/java/org/springframework/boot/restdocs/test/autoconfigure/AutoConfigureRestDocs.java

@ -58,7 +58,7 @@ import org.springframework.test.web.servlet.MockMvc; @@ -58,7 +58,7 @@ import org.springframework.test.web.servlet.MockMvc;
@Inherited
@ImportAutoConfiguration
@Import(RestDocumentationContextProviderRegistrar.class)
@PropertyMapping("spring.test.restdocs")
@PropertyMapping("spring.restdocs")
public @interface AutoConfigureRestDocs {
/**
@ -82,18 +82,21 @@ public @interface AutoConfigureRestDocs { @@ -82,18 +82,21 @@ public @interface AutoConfigureRestDocs {
* Defaults to {@code http}.
* @return the scheme
*/
@PropertyMapping("uri.scheme")
String uriScheme() default "http";
/**
* The host to be used in documented URIs. Defaults to {@code localhost}.
* @return the host
*/
@PropertyMapping("uri.host")
String uriHost() default "localhost";
/**
* The port to be used in documented URIs. Defaults to {@code 8080}.
* @return the port
*/
@PropertyMapping("uri.port")
int uriPort() default 8080;
}

10
module/spring-boot-restdocs/src/main/java/org/springframework/boot/restdocs/test/autoconfigure/RestDocsMockMvcBuilderCustomizer.java

@ -20,6 +20,7 @@ import org.jspecify.annotations.Nullable; @@ -20,6 +20,7 @@ import org.jspecify.annotations.Nullable;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.restdocs.test.autoconfigure.RestDocsProperties.Uri;
import org.springframework.boot.webmvc.test.autoconfigure.MockMvcBuilderCustomizer;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
@ -51,10 +52,11 @@ public class RestDocsMockMvcBuilderCustomizer implements InitializingBean, MockM @@ -51,10 +52,11 @@ public class RestDocsMockMvcBuilderCustomizer implements InitializingBean, MockM
public void afterPropertiesSet() throws Exception {
PropertyMapper map = PropertyMapper.get();
RestDocsProperties properties = this.properties;
UriConfigurer uri = this.delegate.uris();
map.from(properties::getUriScheme).whenHasText().to(uri::withScheme);
map.from(properties::getUriHost).whenHasText().to(uri::withHost);
map.from(properties::getUriPort).to(uri::withPort);
UriConfigurer uriConfigurer = this.delegate.uris();
Uri uri = properties.getUri();
map.from(uri::getScheme).whenHasText().to(uriConfigurer::withScheme);
map.from(uri::getHost).whenHasText().to(uriConfigurer::withHost);
map.from(uri::getPort).to(uriConfigurer::withPort);
}
@Override

44
module/spring-boot-restdocs/src/main/java/org/springframework/boot/restdocs/test/autoconfigure/RestDocsProperties.java

@ -28,46 +28,56 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @@ -28,46 +28,56 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* @author Phillip Webb
* @since 4.0.0
*/
@ConfigurationProperties("spring.test.restdocs")
@ConfigurationProperties("spring.restdocs")
public class RestDocsProperties {
private final Uri uri = new Uri();
public Uri getUri() {
return this.uri;
}
public static class Uri {
/**
* The URI scheme for to use (for example http).
* The URI scheme to use (for example http).
*/
private @Nullable String uriScheme;
private @Nullable String scheme;
/**
* The URI host to use.
*/
private @Nullable String uriHost;
private @Nullable String host;
/**
* The URI port to use.
*/
private @Nullable Integer uriPort;
private @Nullable Integer port;
public @Nullable String getScheme() {
return this.scheme;
}
public @Nullable String getUriScheme() {
return this.uriScheme;
public void setScheme(@Nullable String scheme) {
this.scheme = scheme;
}
public void setUriScheme(@Nullable String uriScheme) {
this.uriScheme = uriScheme;
public @Nullable String getHost() {
return this.host;
}
public @Nullable String getUriHost() {
return this.uriHost;
public void setHost(@Nullable String host) {
this.host = host;
}
public void setUriHost(@Nullable String uriHost) {
this.uriHost = uriHost;
public @Nullable Integer getPort() {
return this.port;
}
public @Nullable Integer getUriPort() {
return this.uriPort;
public void setPort(@Nullable Integer port) {
this.port = port;
}
public void setUriPort(@Nullable Integer uriPort) {
this.uriPort = uriPort;
}
}

8
module/spring-boot-restdocs/src/main/java/org/springframework/boot/restdocs/test/autoconfigure/RestDocsRestAssuredBuilderCustomizer.java

@ -20,6 +20,7 @@ import io.restassured.specification.RequestSpecification; @@ -20,6 +20,7 @@ import io.restassured.specification.RequestSpecification;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.restdocs.test.autoconfigure.RestDocsProperties.Uri;
import org.springframework.util.StringUtils;
/**
@ -41,11 +42,12 @@ class RestDocsRestAssuredBuilderCustomizer implements InitializingBean { @@ -41,11 +42,12 @@ class RestDocsRestAssuredBuilderCustomizer implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
PropertyMapper map = PropertyMapper.get();
String host = this.properties.getUriHost();
map.from(this.properties::getUriScheme)
Uri uri = this.properties.getUri();
String host = uri.getHost();
map.from(uri::getScheme)
.when((scheme) -> StringUtils.hasText(scheme) && StringUtils.hasText(host))
.to((scheme) -> this.delegate.baseUri(scheme + "://" + host));
map.from(this.properties::getUriPort).to(this.delegate::port);
map.from(uri::getPort).to(this.delegate::port);
}
}

8
module/spring-boot-restdocs/src/main/java/org/springframework/boot/restdocs/test/autoconfigure/RestDocsWebTestClientBuilderCustomizer.java

@ -18,6 +18,7 @@ package org.springframework.boot.restdocs.test.autoconfigure; @@ -18,6 +18,7 @@ package org.springframework.boot.restdocs.test.autoconfigure;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.restdocs.test.autoconfigure.RestDocsProperties.Uri;
import org.springframework.boot.webtestclient.WebTestClientBuilderCustomizer;
import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentationConfigurer;
import org.springframework.test.web.reactive.server.WebTestClient;
@ -48,11 +49,12 @@ class RestDocsWebTestClientBuilderCustomizer implements WebTestClientBuilderCust @@ -48,11 +49,12 @@ class RestDocsWebTestClientBuilderCustomizer implements WebTestClientBuilderCust
}
private void customizeBaseUrl(WebTestClient.Builder builder) {
String scheme = this.properties.getUriScheme();
String host = this.properties.getUriHost();
Uri uri = this.properties.getUri();
String scheme = uri.getScheme();
String host = uri.getHost();
String baseUrl = (StringUtils.hasText(scheme) ? scheme : "http") + "://"
+ (StringUtils.hasText(host) ? host : "localhost");
Integer port = this.properties.getUriPort();
Integer port = uri.getPort();
if (!isStandardPort(scheme, port)) {
baseUrl += ":" + port;
}

35
module/spring-boot-restdocs/src/main/resources/META-INF/additional-spring-configuration-metadata.json

@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
{
"properties": [
{
"name": "spring.test.restdocs.uri-host",
"type": "java.lang.String",
"description": "The URI host to use.",
"sourceType": "org.springframework.boot.restdocs.test.autoconfigure.RestDocsProperties",
"deprecation": {
"level": "error",
"replacement": "spring.restdocs.uri.host"
}
},
{
"name": "spring.test.restdocs.uri-port",
"type": "java.lang.Integer",
"description": "The URI port to use.",
"sourceType": "org.springframework.boot.restdocs.test.autoconfigure.RestDocsProperties",
"deprecation": {
"level": "error",
"replacement": "spring.restdocs.uri.port"
}
},
{
"name": "spring.test.restdocs.uri-scheme",
"type": "java.lang.String",
"description": "The URI scheme to use (for example http).",
"sourceType": "org.springframework.boot.restdocs.test.autoconfigure.RestDocsProperties",
"deprecation": {
"level": "error",
"replacement": "spring.restdocs.uri.scheme"
}
}
]
}
Loading…
Cancel
Save