mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 20:19:06 +00:00
Use spring.restdocs as prefix for Spring REST Docs properties
Closes gh-47481
This commit is contained in:
+4
-1
@@ -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 {
|
||||
* 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;
|
||||
|
||||
}
|
||||
|
||||
+6
-4
@@ -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
|
||||
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
|
||||
|
||||
+41
-31
@@ -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 {
|
||||
|
||||
/**
|
||||
* The URI scheme for to use (for example http).
|
||||
*/
|
||||
private @Nullable String uriScheme;
|
||||
private final Uri uri = new Uri();
|
||||
|
||||
/**
|
||||
* The URI host to use.
|
||||
*/
|
||||
private @Nullable String uriHost;
|
||||
|
||||
/**
|
||||
* The URI port to use.
|
||||
*/
|
||||
private @Nullable Integer uriPort;
|
||||
|
||||
public @Nullable String getUriScheme() {
|
||||
return this.uriScheme;
|
||||
public Uri getUri() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
public void setUriScheme(@Nullable String uriScheme) {
|
||||
this.uriScheme = uriScheme;
|
||||
}
|
||||
public static class Uri {
|
||||
|
||||
public @Nullable String getUriHost() {
|
||||
return this.uriHost;
|
||||
}
|
||||
/**
|
||||
* The URI scheme to use (for example http).
|
||||
*/
|
||||
private @Nullable String scheme;
|
||||
|
||||
public void setUriHost(@Nullable String uriHost) {
|
||||
this.uriHost = uriHost;
|
||||
}
|
||||
/**
|
||||
* The URI host to use.
|
||||
*/
|
||||
private @Nullable String host;
|
||||
|
||||
public @Nullable Integer getUriPort() {
|
||||
return this.uriPort;
|
||||
}
|
||||
/**
|
||||
* The URI port to use.
|
||||
*/
|
||||
private @Nullable Integer port;
|
||||
|
||||
public @Nullable String getScheme() {
|
||||
return this.scheme;
|
||||
}
|
||||
|
||||
public void setScheme(@Nullable String scheme) {
|
||||
this.scheme = scheme;
|
||||
}
|
||||
|
||||
public @Nullable String getHost() {
|
||||
return this.host;
|
||||
}
|
||||
|
||||
public void setHost(@Nullable String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public @Nullable Integer getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void setPort(@Nullable Integer port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void setUriPort(@Nullable Integer uriPort) {
|
||||
this.uriPort = uriPort;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-3
@@ -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 {
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-3
@@ -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
|
||||
}
|
||||
|
||||
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
@@ -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"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user