Skip to content

Actuator /configprops shows Duration properties as { "units": [ "SECONDS", "NANOS" ] } #16539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,6 +41,7 @@
import com.fasterxml.jackson.databind.ser.SerializerFactory;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -171,10 +172,12 @@ private Map<String, Object> safeSerialize(ObjectMapper mapper, Object bean,
*/
protected void configureObjectMapper(ObjectMapper mapper) {
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that related to the support of Duration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snicoll I pushed a revised version that registers a module. I've tried this actually before, but it didn't work because I called it before the serializerFactory is overwritten again in applySerializationModifier. Anyway, this should be easier now. And btw: there would be a way of calling ObjectMapper.findAndRegisterModules but that might register more than you want and is sort of expensive.

mapper.configure(MapperFeature.USE_STD_BEAN_NAMING, true);
mapper.setSerializationInclusion(Include.NON_NULL);
applyConfigurationPropertiesFilter(mapper);
applySerializationModifier(mapper);
mapper.registerModule(new JavaTimeModule());
}

private ObjectMapper getObjectMapper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.actuate.context.properties;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -157,6 +158,16 @@ public void mixedCase() {
});
}

@Test
public void duration() {
load((context, properties) -> {
Map<String, Object> nestedProperties = properties.getBeans()
.get("testProperties").getProperties();
assertThat(nestedProperties.get("duration"))
.isEqualTo(Duration.ofSeconds(10).toString());
});
}

@Test
public void singleLetterProperty() {
load((context, properties) -> {
Expand Down Expand Up @@ -276,6 +287,8 @@ public static class TestProperties {

private String nullValue = null;

private Duration duration = Duration.ofSeconds(10);

public TestProperties() {
this.secrets.put("mine", "myPrivateThing");
this.secrets.put("yours", "yourPrivateThing");
Expand Down Expand Up @@ -379,6 +392,14 @@ public void setNullValue(String nullValue) {
this.nullValue = nullValue;
}

public Duration getDuration() {
return this.duration;
}

public void setDuration(Duration duration) {
this.duration = duration;
}

public static class Hidden {

private String mine = "mySecret";
Expand Down