Skip to content

Commit

Permalink
If a spring.autoconfigure.exclude property is defined in a test prope…
Browse files Browse the repository at this point in the history
…rty source, don't undo the override in the ExcludeAutoConfigurationsEnvironmentPostProcessor
  • Loading branch information
paulbakker committed Jan 7, 2025
1 parent a300767 commit f0eb24b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ class ExcludeAutoConfigurationsEnvironmentPostProcessor : EnvironmentPostProcess
)
}

private fun extractAllExcludes(propertySources: MutablePropertySources): String =
propertySources
private fun extractAllExcludes(propertySources: MutablePropertySources): String {
if (propertySources.any { it.name == INLINED_TEST_PROPERTIES }) {
val testExclude = propertySources.find { it.name == INLINED_TEST_PROPERTIES }?.getProperty(EXCLUDE)
if (testExclude is String && testExclude.isNotBlank()) {
return testExclude
}
}

return propertySources
.stream()
.filter { src -> !ConfigurationPropertySources.isAttachedConfigurationPropertySource(src) }
.map { src ->
Expand All @@ -71,6 +78,7 @@ class ExcludeAutoConfigurationsEnvironmentPostProcessor : EnvironmentPostProcess
}.orElse(emptyList())
}.flatMap { it.stream() }
.collect(Collectors.joining(","))
}

companion object {
private val DISABLE_AUTOCONFIG_PROPERTIES =
Expand All @@ -86,5 +94,6 @@ class ExcludeAutoConfigurationsEnvironmentPostProcessor : EnvironmentPostProcess
)

private const val EXCLUDE = "spring.autoconfigure.exclude"
private const val INLINED_TEST_PROPERTIES = "Inlined Test Properties"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,22 @@ class ExcludeAutoConfigurationsEnvironmentPostProcessorTest {
"org.springframework.boot.autoconfigure.graphql.security.GraphQlWebMvcSecurityAutoConfiguration",
)
}

@Test
fun `does not reintroduce overridden excludes in test properties`() {
val env = StandardEnvironment()
env.propertySources.addLast(MapPropertySource("application-props", mapOf(Pair("spring.autoconfigure.exclude", "someexclude"))))
env.propertySources.addLast(MapPropertySource("Inlined Test Properties", mapOf(Pair("spring.autoconfigure.exclude", "someotherexclude"))))

ExcludeAutoConfigurationsEnvironmentPostProcessor().postProcessEnvironment(env, SpringApplication())
assertThat(env.getProperty("spring.autoconfigure.exclude"))
.contains(
"someotherexclude",
"org.springframework.boot.actuate.autoconfigure.observation.graphql.GraphQlObservationAutoConfiguration",
"org.springframework.boot.autoconfigure.graphql.security.GraphQlWebMvcSecurityAutoConfiguration",
)

assertThat(env.getProperty("spring.autoconfigure.exclude"))
.doesNotContain("someexclude")
}
}

0 comments on commit f0eb24b

Please sign in to comment.