Skip to content

Commit

Permalink
Restore previous serialization formatting in Actuator responses
Browse files Browse the repository at this point in the history
Fixes gh-33236
  • Loading branch information
wilkinsona committed Nov 17, 2022
1 parent cb1ee20 commit cd455a9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Expand Up @@ -16,7 +16,9 @@

package org.springframework.boot.actuate.autoconfigure.endpoint.jackson;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import org.springframework.boot.actuate.endpoint.jackson.EndpointObjectMapper;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
Expand All @@ -42,7 +44,10 @@ public class JacksonEndpointAutoConfiguration {
@ConditionalOnProperty(name = "management.endpoints.jackson.isolated-object-mapper", matchIfMissing = true)
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
public EndpointObjectMapper endpointObjectMapper() {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.serializationInclusion(Include.NON_NULL).build();
return () -> objectMapper;
}

Expand Down
Expand Up @@ -16,6 +16,12 @@

package org.springframework.boot.actuate.autoconfigure.endpoint.jackson;

import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -54,6 +60,37 @@ void endpointObjectMapperWhenPropertyFalse() {
.run((context) -> assertThat(context).doesNotHaveBean(EndpointObjectMapper.class));
}

@Test
void endpointObjectMapperDoesNotSerializeDatesAsTimestamps() {
this.runner.run((context) -> {
ObjectMapper objectMapper = context.getBean(EndpointObjectMapper.class).get();
Instant now = Instant.now();
String json = objectMapper.writeValueAsString(Map.of("timestamp", now));
assertThat(json).contains(DateTimeFormatter.ISO_INSTANT.format(now));
});
}

@Test
void endpointObjectMapperDoesNotSerializeDurationsAsTimestamps() {
this.runner.run((context) -> {
ObjectMapper objectMapper = context.getBean(EndpointObjectMapper.class).get();
Duration duration = Duration.ofSeconds(42);
String json = objectMapper.writeValueAsString(Map.of("duration", duration));
assertThat(json).contains(duration.toString());
});
}

@Test
void endpointObjectMapperDoesNotSerializeNullValues() {
this.runner.run((context) -> {
ObjectMapper objectMapper = context.getBean(EndpointObjectMapper.class).get();
HashMap<String, String> map = new HashMap<>();
map.put("key", null);
String json = objectMapper.writeValueAsString(map);
assertThat(json).isEqualTo("{}");
});
}

@Configuration(proxyBeanMethods = false)
static class TestEndpointMapperConfiguration {

Expand Down
Expand Up @@ -26,10 +26,14 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.jackson.JacksonEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive.WebFluxEndpointManagementContextConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration;
import org.springframework.boot.actuate.endpoint.jackson.EndpointObjectMapper;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
Expand All @@ -38,6 +42,7 @@
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.restdocs.operation.preprocess.ContentModifyingOperationPreprocessor;
import org.springframework.restdocs.operation.preprocess.OperationPreprocessor;
Expand All @@ -54,8 +59,7 @@
*
* @author Andy Wilkinson
*/
@TestPropertySource(properties = { "spring.jackson.serialization.indent_output=true",
"management.endpoints.web.exposure.include=*", "spring.jackson.default-property-inclusion=non_null" })
@TestPropertySource(properties = { "management.endpoints.web.exposure.include=*" })
public abstract class AbstractEndpointDocumentationTests {

protected static String describeEnumValues(Class<? extends Enum<?>> enumType) {
Expand Down Expand Up @@ -119,9 +123,26 @@ private <T> List<Object> select(List<Object> candidates, Predicate<T> filter) {
WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class, EndpointAutoConfiguration.class,
WebEndpointAutoConfiguration.class, WebMvcEndpointManagementContextConfiguration.class,
WebFluxEndpointManagementContextConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
WebFluxAutoConfiguration.class, HttpHandlerAutoConfiguration.class })
WebFluxAutoConfiguration.class, HttpHandlerAutoConfiguration.class,
JacksonEndpointAutoConfiguration.class })
static class BaseDocumentationConfiguration {

@Bean
static BeanPostProcessor endpointObjectMapperBeanPostProcessor() {
return new BeanPostProcessor() {

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof EndpointObjectMapper) {
return (EndpointObjectMapper) () -> ((EndpointObjectMapper) bean).get()
.enable(SerializationFeature.INDENT_OUTPUT);
}
return bean;
}

};
}

}

}

0 comments on commit cd455a9

Please sign in to comment.