Skip to content

Commit 1ffefcb

Browse files
committed
LiveBeansView escapes double quotes in resource descriptions
Issue: SPR-12252 (cherry picked from commit 92f7121)
1 parent 0c680d6 commit 1ffefcb

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

spring-context/src/main/java/org/springframework/context/support/LiveBeansView.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
175175
else {
176176
result.append("\"type\": null,\n");
177177
}
178-
String resource = StringUtils.replace(bd.getResourceDescription(), "\\", "/");
179-
result.append("\"resource\": \"").append(resource).append("\",\n");
178+
result.append("\"resource\": \"").append(getEscapedResourceDescription(bd)).append("\",\n");
180179
result.append("\"dependencies\": [");
181180
String[] dependencies = bf.getDependenciesForBean(beanName);
182181
if (dependencies.length > 0) {
@@ -201,7 +200,7 @@ protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
201200
}
202201

203202
/**
204-
* Determine whether the specified bean is eligible for inclusion in the
203+
* Determine whether the specified bean is eligible for inclusion in the
205204
* LiveBeansView JSON snapshot.
206205
* @param beanName the name of the bean
207206
* @param bd the corresponding bean definition
@@ -213,4 +212,31 @@ protected boolean isBeanEligible(String beanName, BeanDefinition bd, Configurabl
213212
(!bd.isLazyInit() || bf.containsSingleton(beanName)));
214213
}
215214

215+
/**
216+
* Determine a resource description for the given bean definition and
217+
* apply basic JSON escaping (backslashes, double quotes) to it.
218+
* @param bd the bean definition to build the resource description for
219+
* @return the JSON-escaped resource description
220+
*/
221+
protected String getEscapedResourceDescription(BeanDefinition bd) {
222+
String resourceDescription = bd.getResourceDescription();
223+
if (resourceDescription == null) {
224+
return null;
225+
}
226+
StringBuilder result = new StringBuilder(resourceDescription.length() + 16);
227+
for (int i = 0; i < resourceDescription.length(); i++) {
228+
char character = resourceDescription.charAt(i);
229+
if (character == '\\') {
230+
result.append('/');
231+
}
232+
else if (character == '"') {
233+
result.append("\\").append('"');
234+
}
235+
else {
236+
result.append(character);
237+
}
238+
}
239+
return result.toString();
240+
}
241+
216242
}

0 commit comments

Comments
 (0)