Include the environment default profiles in the env endpoint's response

See gh-39257
This commit is contained in:
Wzy19930507 2024-01-21 22:28:55 +08:00 committed by Moritz Halbritter
parent f36b69f961
commit dae3952144
3 changed files with 27 additions and 7 deletions

View File

@ -64,6 +64,9 @@ class EnvironmentEndpointDocumentationTests extends MockMvcEndpointDocumentation
private static final FieldDescriptor activeProfiles = fieldWithPath("activeProfiles") private static final FieldDescriptor activeProfiles = fieldWithPath("activeProfiles")
.description("Names of the active profiles, if any."); .description("Names of the active profiles, if any.");
private static final FieldDescriptor defaultProfiles = fieldWithPath("defaultProfiles")
.description("Names of the default profiles, if any.");
private static final FieldDescriptor propertySources = fieldWithPath("propertySources") private static final FieldDescriptor propertySources = fieldWithPath("propertySources")
.description("Property sources in order of precedence."); .description("Property sources in order of precedence.");
@ -79,7 +82,7 @@ class EnvironmentEndpointDocumentationTests extends MockMvcEndpointDocumentation
replacePattern(Pattern.compile( replacePattern(Pattern.compile(
"org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/"), ""), "org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/"), ""),
filterProperties()), filterProperties()),
responseFields(activeProfiles, propertySources, propertySourceName, responseFields(activeProfiles, defaultProfiles, propertySources, propertySourceName,
fieldWithPath("propertySources.[].properties") fieldWithPath("propertySources.[].properties")
.description("Properties in the property source keyed by property name."), .description("Properties in the property source keyed by property name."),
fieldWithPath("propertySources.[].properties.*.value") fieldWithPath("propertySources.[].properties.*.value")
@ -101,7 +104,7 @@ class EnvironmentEndpointDocumentationTests extends MockMvcEndpointDocumentation
.optional(), .optional(),
fieldWithPath("property.source").description("Name of the source of the property."), fieldWithPath("property.source").description("Name of the source of the property."),
fieldWithPath("property.value").description("Value of the property."), activeProfiles, fieldWithPath("property.value").description("Value of the property."), activeProfiles,
propertySources, propertySourceName, defaultProfiles, propertySources, propertySourceName,
fieldWithPath("propertySources.[].property") fieldWithPath("propertySources.[].property")
.description("Property in the property source, if any.") .description("Property in the property source, if any.")
.optional(), .optional(),

View File

@ -101,7 +101,8 @@ public class EnvironmentEndpoint {
propertyNamePredicate, showUnsanitized)); propertyNamePredicate, showUnsanitized));
} }
}); });
return new EnvironmentDescriptor(Arrays.asList(this.environment.getActiveProfiles()), propertySources); return new EnvironmentDescriptor(Arrays.asList(this.environment.getActiveProfiles()),
Arrays.asList(this.environment.getDefaultProfiles()), propertySources);
} }
@ReadOperation @ReadOperation
@ -114,7 +115,7 @@ public class EnvironmentEndpoint {
Map<String, PropertyValueDescriptor> descriptors = getPropertySourceDescriptors(propertyName, showUnsanitized); Map<String, PropertyValueDescriptor> descriptors = getPropertySourceDescriptors(propertyName, showUnsanitized);
PropertySummaryDescriptor summary = getPropertySummaryDescriptor(descriptors); PropertySummaryDescriptor summary = getPropertySummaryDescriptor(descriptors);
return new EnvironmentEntryDescriptor(summary, Arrays.asList(this.environment.getActiveProfiles()), return new EnvironmentEntryDescriptor(summary, Arrays.asList(this.environment.getActiveProfiles()),
toPropertySourceDescriptors(descriptors)); Arrays.asList(this.environment.getDefaultProfiles()), toPropertySourceDescriptors(descriptors));
} }
private List<PropertySourceEntryDescriptor> toPropertySourceDescriptors( private List<PropertySourceEntryDescriptor> toPropertySourceDescriptors(
@ -209,10 +210,14 @@ public class EnvironmentEndpoint {
private final List<String> activeProfiles; private final List<String> activeProfiles;
private final List<String> defaultProfiles;
private final List<PropertySourceDescriptor> propertySources; private final List<PropertySourceDescriptor> propertySources;
private EnvironmentDescriptor(List<String> activeProfiles, List<PropertySourceDescriptor> propertySources) { private EnvironmentDescriptor(List<String> activeProfiles, List<String> defaultProfiles,
List<PropertySourceDescriptor> propertySources) {
this.activeProfiles = activeProfiles; this.activeProfiles = activeProfiles;
this.defaultProfiles = defaultProfiles;
this.propertySources = propertySources; this.propertySources = propertySources;
} }
@ -220,6 +225,10 @@ public class EnvironmentEndpoint {
return this.activeProfiles; return this.activeProfiles;
} }
public List<String> getDefaultProfiles() {
return this.defaultProfiles;
}
public List<PropertySourceDescriptor> getPropertySources() { public List<PropertySourceDescriptor> getPropertySources() {
return this.propertySources; return this.propertySources;
} }
@ -236,12 +245,15 @@ public class EnvironmentEndpoint {
private final List<String> activeProfiles; private final List<String> activeProfiles;
private final List<String> defaultProfiles;
private final List<PropertySourceEntryDescriptor> propertySources; private final List<PropertySourceEntryDescriptor> propertySources;
EnvironmentEntryDescriptor(PropertySummaryDescriptor property, List<String> activeProfiles, EnvironmentEntryDescriptor(PropertySummaryDescriptor property, List<String> activeProfiles,
List<PropertySourceEntryDescriptor> propertySources) { List<String> defaultProfiles, List<PropertySourceEntryDescriptor> propertySources) {
this.property = property; this.property = property;
this.activeProfiles = activeProfiles; this.activeProfiles = activeProfiles;
this.defaultProfiles = defaultProfiles;
this.propertySources = propertySources; this.propertySources = propertySources;
} }
@ -253,6 +265,10 @@ public class EnvironmentEndpoint {
return this.activeProfiles; return this.activeProfiles;
} }
public List<String> getDefaultProfiles() {
return this.defaultProfiles;
}
public List<PropertySourceEntryDescriptor> getPropertySources() { public List<PropertySourceEntryDescriptor> getPropertySources() {
return this.propertySources; return this.propertySources;
} }

View File

@ -86,7 +86,8 @@ class EnvironmentEndpointWebExtensionTests {
private void verifyPrefixed(SecurityContext securityContext, boolean showUnsanitized) { private void verifyPrefixed(SecurityContext securityContext, boolean showUnsanitized) {
given(this.delegate.getEnvironmentEntryDescriptor("test", showUnsanitized)) given(this.delegate.getEnvironmentEntryDescriptor("test", showUnsanitized))
.willReturn(new EnvironmentEntryDescriptor(null, Collections.emptyList(), Collections.emptyList())); .willReturn(new EnvironmentEntryDescriptor(null, Collections.emptyList(), Collections.emptyList(),
Collections.emptyList()));
this.webExtension.environmentEntry(securityContext, "test"); this.webExtension.environmentEntry(securityContext, "test");
then(this.delegate).should().getEnvironmentEntryDescriptor("test", showUnsanitized); then(this.delegate).should().getEnvironmentEntryDescriptor("test", showUnsanitized);
} }