From c1e285e76fb8378f86bf9398b6358c131da25da6 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 24 Mar 2017 09:24:28 +0000 Subject: [PATCH] Fix property source ordering issue with bootstrap context The problem is that the bootstrap context gets created first with the correct wiremock server port, and then the environment is merged into the main context before the annotation property source is created, so the latter takes precedence. Fixed by re-ordering the property sources if detected. Also switch off bootstrap context for wiremock server Fixes gh-225 --- .../restdocs/http-client/.gitignore | 1 + .../standalone/restdocs/http-client/pom.xml | 12 ++++++++++ .../wiremock/SpringBootHttpServerFactory.java | 20 +++++++++++------ .../wiremock/WireMockApplicationListener.java | 22 ++++++++++++------- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/samples/standalone/restdocs/http-client/.gitignore b/samples/standalone/restdocs/http-client/.gitignore index b05ba7bd6d..a275b7b864 100644 --- a/samples/standalone/restdocs/http-client/.gitignore +++ b/samples/standalone/restdocs/http-client/.gitignore @@ -4,3 +4,4 @@ target/ .gradle build/ +/.apt_generated/ diff --git a/samples/standalone/restdocs/http-client/pom.xml b/samples/standalone/restdocs/http-client/pom.xml index 3ce7f6503c..2bcb4f7b6b 100644 --- a/samples/standalone/restdocs/http-client/pom.xml +++ b/samples/standalone/restdocs/http-client/pom.xml @@ -21,6 +21,7 @@ UTF-8 1.8 1.0.5.BUILD-SNAPSHOT + 1.1.8.RELEASE @@ -32,6 +33,10 @@ org.springframework.boot spring-boot-starter-actuator + + org.springframework.cloud + spring-cloud-starter + @@ -79,6 +84,13 @@ pom import + + org.springframework.cloud + spring-cloud-commons-dependencies + ${spring-cloud-commons.version} + pom + import + diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/SpringBootHttpServerFactory.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/SpringBootHttpServerFactory.java index fad716dd8b..ecf36be8cb 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/SpringBootHttpServerFactory.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/SpringBootHttpServerFactory.java @@ -111,7 +111,8 @@ class SpringBootHttpServer @Override public void start() { this.context = new SpringApplicationBuilder(WiremockServerConfiguration.class) - .logStartupInfo(false).bannerMode(Mode.OFF).listeners(this).run(); + .logStartupInfo(false).bannerMode(Mode.OFF) + .properties("spring.cloud.bootstrap.enabled=false").listeners(this).run(); this.running = true; } @@ -180,7 +181,8 @@ class SpringBootHttpServer return bean; } - private void setupHttps(WiremockServerProperties server, HttpsSettings httpsSettings) { + private void setupHttps(WiremockServerProperties server, + HttpsSettings httpsSettings) { if (httpsSettings.port() < 0 || !httpsSettings.enabled()) { return; } @@ -209,7 +211,7 @@ class SpringBootHttpServer } class WiremockServerProperties implements EmbeddedServletContainerCustomizer { - + private ServerProperties delegate = new ServerProperties(); public Integer getPort() { @@ -289,7 +291,8 @@ class WiremockServerConfiguration { WiremockServerConfiguration.this.adminRequestHandler); servletContext.setAttribute(StubRequestHandler.class.getName(), WiremockServerConfiguration.this.stubRequestHandler); - servletContext.setAttribute(Notifier.KEY, WiremockServerConfiguration.this.options.notifier()); + servletContext.setAttribute(Notifier.KEY, + WiremockServerConfiguration.this.options.notifier()); } }; } @@ -393,7 +396,7 @@ class ContainerConfiguration { @Autowired private ContainerProperties container; - + private Integer port; @Bean @@ -403,8 +406,11 @@ class ContainerConfiguration { undertow.addBuilderCustomizers(new UndertowBuilderCustomizer() { @Override public void customize(Builder builder) { - builder.addHttpListener(UndertowContainerConfiguration.this.options.portNumber(), "localhost"); - UndertowContainerConfiguration.this.port = UndertowContainerConfiguration.this.options.portNumber(); + builder.addHttpListener( + UndertowContainerConfiguration.this.options.portNumber(), + "localhost"); + UndertowContainerConfiguration.this.port = UndertowContainerConfiguration.this.options + .portNumber(); } }); } diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java index 565ff0b0d4..ce6f5cf9b7 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java @@ -26,6 +26,7 @@ import org.springframework.core.annotation.Order; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; import org.springframework.util.SocketUtils; /** @@ -47,10 +48,7 @@ public class WireMockApplicationListener private void registerPort(ConfigurableEnvironment environment) { if (environment.getProperty("wiremock.server.port", Integer.class, 0) == 0) { MutablePropertySources propertySources = environment.getPropertySources(); - if (!propertySources.contains("wiremock")) { - propertySources.addFirst( - new MapPropertySource("wiremock", new HashMap())); - } + addPropertySource(propertySources); Map source = ((MapPropertySource) propertySources .get("wiremock")).getSource(); source.put("wiremock.server.port", @@ -59,10 +57,7 @@ public class WireMockApplicationListener if (environment.getProperty("wiremock.server.https-port", Integer.class, 0) == 0) { MutablePropertySources propertySources = environment.getPropertySources(); - if (!propertySources.contains("wiremock")) { - propertySources.addFirst( - new MapPropertySource("wiremock", new HashMap())); - } + addPropertySource(propertySources); Map source = ((MapPropertySource) propertySources .get("wiremock")).getSource(); source.put("wiremock.server.https-port", @@ -70,4 +65,15 @@ public class WireMockApplicationListener } } + private void addPropertySource(MutablePropertySources propertySources) { + if (!propertySources.contains("wiremock")) { + propertySources.addFirst( + new MapPropertySource("wiremock", new HashMap())); + } else { + // Move it up into first place + PropertySource wiremock = propertySources.remove("wiremock"); + propertySources.addFirst(wiremock); + } + } + }