Skip to content

Commit 5322352

Browse files
committed
Polish "Tighten rules around profile naming"
See gh-43176
1 parent 0be0bed commit 5322352

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -149,13 +149,13 @@ private Set<StandardConfigDataReference> getReferences(ConfigDataLocationResolve
149149
@Override
150150
public List<StandardConfigDataResource> resolveProfileSpecific(ConfigDataLocationResolverContext context,
151151
ConfigDataLocation location, Profiles profiles) {
152+
validateProfiles(profiles);
152153
return resolve(getProfileSpecificReferences(context, location.split(), profiles));
153154
}
154155

155156
private Set<StandardConfigDataReference> getProfileSpecificReferences(ConfigDataLocationResolverContext context,
156157
ConfigDataLocation[] configDataLocations, Profiles profiles) {
157158
Set<StandardConfigDataReference> references = new LinkedHashSet<>();
158-
validateProfiles(profiles);
159159
for (String profile : profiles) {
160160
for (ConfigDataLocation configDataLocation : configDataLocations) {
161161
String resourceLocation = getResourceLocation(context, configDataLocation);
@@ -172,7 +172,7 @@ private void validateProfiles(Profiles profiles) {
172172
}
173173

174174
private void validateProfile(String profile) {
175-
Assert.hasText(profile, "Profile must contain text");
175+
Assert.hasText(profile, "'profile' must contain text");
176176
Assert.state(!profile.startsWith("-") && !profile.startsWith("_"),
177177
() -> String.format("Invalid profile '%s': must not start with '-' or '_'", profile));
178178
Assert.state(!profile.endsWith("-") && !profile.endsWith("_"),
@@ -181,7 +181,8 @@ private void validateProfile(String profile) {
181181
if (codePoint == '-' || codePoint == '_' || Character.isLetterOrDigit(codePoint)) {
182182
return;
183183
}
184-
throw new IllegalStateException(String.format("Invalid profile '%s': must contain only letters or digits or '-' or '_'", profile));
184+
throw new IllegalStateException(
185+
String.format("Invalid profile '%s': must contain only letters or digits or '-' or '_'", profile));
185186
});
186187
}
187188

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ void logsActiveProfilesWithoutProfileAndMultipleDefaults(CapturedOutput output)
258258
application.setWebApplicationType(WebApplicationType.NONE);
259259
application.setEnvironment(environment);
260260
this.context = application.run();
261-
assertThat(output)
262-
.contains("No active profile set, falling back to 2 default profiles: \"p0\", \"default\"");
261+
assertThat(output).contains("No active profile set, falling back to 2 default profiles: \"p0\", \"default\"");
263262
}
264263

265264
@Test

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataLocationResolverTests.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ void resolveProfileSpecificWithNonAsciiCharactersShouldNotThrowException() {
309309
this.environment.setActiveProfiles("dev-테스트_123");
310310
Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList());
311311
assertThatNoException()
312-
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles));
312+
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles));
313313
}
314314

315315
@Test
@@ -368,8 +368,7 @@ void resolveProfileSpecificWhenProfileContainsInvalidCharactersThrowsException()
368368
Profiles profiles = new Profiles(this.environment, this.environmentBinder, Collections.emptyList());
369369
assertThatIllegalStateException()
370370
.isThrownBy(() -> this.resolver.resolveProfileSpecific(this.context, location, profiles))
371-
.withMessageStartingWith(
372-
"Invalid profile 'dev*test': must contain only letters or digits or '-' or '_'");
371+
.withMessageStartingWith("Invalid profile 'dev*test': must contain only letters or digits or '-' or '_'");
373372
}
374373

375374
private String filePath(String... components) {

0 commit comments

Comments
 (0)