mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-10-01 06:49:42 +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
|
@Inherited
|
||||||
@ImportAutoConfiguration
|
@ImportAutoConfiguration
|
||||||
@Import(RestDocumentationContextProviderRegistrar.class)
|
@Import(RestDocumentationContextProviderRegistrar.class)
|
||||||
@PropertyMapping("spring.test.restdocs")
|
@PropertyMapping("spring.restdocs")
|
||||||
public @interface AutoConfigureRestDocs {
|
public @interface AutoConfigureRestDocs {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -82,18 +82,21 @@ public @interface AutoConfigureRestDocs {
|
|||||||
* Defaults to {@code http}.
|
* Defaults to {@code http}.
|
||||||
* @return the scheme
|
* @return the scheme
|
||||||
*/
|
*/
|
||||||
|
@PropertyMapping("uri.scheme")
|
||||||
String uriScheme() default "http";
|
String uriScheme() default "http";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The host to be used in documented URIs. Defaults to {@code localhost}.
|
* The host to be used in documented URIs. Defaults to {@code localhost}.
|
||||||
* @return the host
|
* @return the host
|
||||||
*/
|
*/
|
||||||
|
@PropertyMapping("uri.host")
|
||||||
String uriHost() default "localhost";
|
String uriHost() default "localhost";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The port to be used in documented URIs. Defaults to {@code 8080}.
|
* The port to be used in documented URIs. Defaults to {@code 8080}.
|
||||||
* @return the port
|
* @return the port
|
||||||
*/
|
*/
|
||||||
|
@PropertyMapping("uri.port")
|
||||||
int uriPort() default 8080;
|
int uriPort() default 8080;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-4
@@ -20,6 +20,7 @@ import org.jspecify.annotations.Nullable;
|
|||||||
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.boot.context.properties.PropertyMapper;
|
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.boot.webmvc.test.autoconfigure.MockMvcBuilderCustomizer;
|
||||||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer;
|
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer;
|
||||||
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
|
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
|
||||||
@@ -51,10 +52,11 @@ public class RestDocsMockMvcBuilderCustomizer implements InitializingBean, MockM
|
|||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
PropertyMapper map = PropertyMapper.get();
|
PropertyMapper map = PropertyMapper.get();
|
||||||
RestDocsProperties properties = this.properties;
|
RestDocsProperties properties = this.properties;
|
||||||
UriConfigurer uri = this.delegate.uris();
|
UriConfigurer uriConfigurer = this.delegate.uris();
|
||||||
map.from(properties::getUriScheme).whenHasText().to(uri::withScheme);
|
Uri uri = properties.getUri();
|
||||||
map.from(properties::getUriHost).whenHasText().to(uri::withHost);
|
map.from(uri::getScheme).whenHasText().to(uriConfigurer::withScheme);
|
||||||
map.from(properties::getUriPort).to(uri::withPort);
|
map.from(uri::getHost).whenHasText().to(uriConfigurer::withHost);
|
||||||
|
map.from(uri::getPort).to(uriConfigurer::withPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+41
-31
@@ -28,46 +28,56 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @since 4.0.0
|
* @since 4.0.0
|
||||||
*/
|
*/
|
||||||
@ConfigurationProperties("spring.test.restdocs")
|
@ConfigurationProperties("spring.restdocs")
|
||||||
public class RestDocsProperties {
|
public class RestDocsProperties {
|
||||||
|
|
||||||
/**
|
private final Uri uri = new Uri();
|
||||||
* The URI scheme for to use (for example http).
|
|
||||||
*/
|
|
||||||
private @Nullable String uriScheme;
|
|
||||||
|
|
||||||
/**
|
public Uri getUri() {
|
||||||
* The URI host to use.
|
return this.uri;
|
||||||
*/
|
|
||||||
private @Nullable String uriHost;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The URI port to use.
|
|
||||||
*/
|
|
||||||
private @Nullable Integer uriPort;
|
|
||||||
|
|
||||||
public @Nullable String getUriScheme() {
|
|
||||||
return this.uriScheme;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUriScheme(@Nullable String uriScheme) {
|
public static class Uri {
|
||||||
this.uriScheme = uriScheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
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.beans.factory.InitializingBean;
|
||||||
import org.springframework.boot.context.properties.PropertyMapper;
|
import org.springframework.boot.context.properties.PropertyMapper;
|
||||||
|
import org.springframework.boot.restdocs.test.autoconfigure.RestDocsProperties.Uri;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,11 +42,12 @@ class RestDocsRestAssuredBuilderCustomizer implements InitializingBean {
|
|||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
PropertyMapper map = PropertyMapper.get();
|
PropertyMapper map = PropertyMapper.get();
|
||||||
String host = this.properties.getUriHost();
|
Uri uri = this.properties.getUri();
|
||||||
map.from(this.properties::getUriScheme)
|
String host = uri.getHost();
|
||||||
|
map.from(uri::getScheme)
|
||||||
.when((scheme) -> StringUtils.hasText(scheme) && StringUtils.hasText(host))
|
.when((scheme) -> StringUtils.hasText(scheme) && StringUtils.hasText(host))
|
||||||
.to((scheme) -> this.delegate.baseUri(scheme + "://" + 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.jspecify.annotations.Nullable;
|
||||||
|
|
||||||
|
import org.springframework.boot.restdocs.test.autoconfigure.RestDocsProperties.Uri;
|
||||||
import org.springframework.boot.webtestclient.WebTestClientBuilderCustomizer;
|
import org.springframework.boot.webtestclient.WebTestClientBuilderCustomizer;
|
||||||
import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentationConfigurer;
|
import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentationConfigurer;
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
@@ -48,11 +49,12 @@ class RestDocsWebTestClientBuilderCustomizer implements WebTestClientBuilderCust
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void customizeBaseUrl(WebTestClient.Builder builder) {
|
private void customizeBaseUrl(WebTestClient.Builder builder) {
|
||||||
String scheme = this.properties.getUriScheme();
|
Uri uri = this.properties.getUri();
|
||||||
String host = this.properties.getUriHost();
|
String scheme = uri.getScheme();
|
||||||
|
String host = uri.getHost();
|
||||||
String baseUrl = (StringUtils.hasText(scheme) ? scheme : "http") + "://"
|
String baseUrl = (StringUtils.hasText(scheme) ? scheme : "http") + "://"
|
||||||
+ (StringUtils.hasText(host) ? host : "localhost");
|
+ (StringUtils.hasText(host) ? host : "localhost");
|
||||||
Integer port = this.properties.getUriPort();
|
Integer port = uri.getPort();
|
||||||
if (!isStandardPort(scheme, port)) {
|
if (!isStandardPort(scheme, port)) {
|
||||||
baseUrl += ":" + 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