Skip to content
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

Convert jackson module to use @ConfigMapping #45712

Merged
merged 2 commits into from
Jan 21, 2025
Merged
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
3 changes: 0 additions & 3 deletions extensions/jackson/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,10 @@ public SyntheticBeanBuildItem jacksonSupport(JacksonSupportRecorder recorder,
}

private Optional<String> determinePropertyNamingStrategyClassName(JacksonBuildTimeConfig jacksonBuildTimeConfig) {
if (jacksonBuildTimeConfig.propertyNamingStrategy.isEmpty()) {
if (jacksonBuildTimeConfig.propertyNamingStrategy().isEmpty()) {
return Optional.empty();
}
var propertyNamingStrategy = jacksonBuildTimeConfig.propertyNamingStrategy.get();
var propertyNamingStrategy = jacksonBuildTimeConfig.propertyNamingStrategy().get();
Field field;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.jupiter.api.Assertions.fail;

import java.time.zone.ZoneRulesException;
import java.util.Date;

import jakarta.inject.Inject;
Expand All @@ -15,14 +14,15 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.config.ConfigValidationException;

public class JacksonErroneousTimeZonePropertiesTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClasses(Pojo.class, SomeBean.class))
.withConfigurationResource("application-erroneous-timezone-properties.properties")
.setExpectedException(ZoneRulesException.class);
.setExpectedException(ConfigValidationException.class);

@Test
public void test() {
Expand Down
3 changes: 0 additions & 3 deletions extensions/jackson/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ public class ConfigurationCustomizer implements ObjectMapperCustomizer {

@Override
public void customize(ObjectMapper objectMapper) {
if (!jacksonBuildTimeConfig.failOnUnknownProperties) {
if (!jacksonBuildTimeConfig.failOnUnknownProperties()) {
// this feature is enabled by default, so we disable it
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
if (!jacksonBuildTimeConfig.failOnEmptyBeans) {
if (!jacksonBuildTimeConfig.failOnEmptyBeans()) {
// this feature is enabled by default, so we disable it
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
if (!jacksonBuildTimeConfig.writeDatesAsTimestamps) {
if (!jacksonBuildTimeConfig.writeDatesAsTimestamps()) {
// this feature is enabled by default, so we disable it
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
if (!jacksonBuildTimeConfig.writeDurationsAsTimestamps) {
if (!jacksonBuildTimeConfig.writeDurationsAsTimestamps()) {
// this feature is enabled by default, so we disable it
objectMapper.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS);
}
if (jacksonBuildTimeConfig.acceptCaseInsensitiveEnums) {
if (jacksonBuildTimeConfig.acceptCaseInsensitiveEnums()) {
objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
}
JsonInclude.Include serializationInclusion = jacksonBuildTimeConfig.serializationInclusion.orElse(null);
JsonInclude.Include serializationInclusion = jacksonBuildTimeConfig.serializationInclusion().orElse(null);
if (serializationInclusion != null) {
objectMapper.setSerializationInclusion(serializationInclusion);
}
ZoneId zoneId = jacksonBuildTimeConfig.timezone.orElse(null);
if ((zoneId != null) && !zoneId.getId().equals("UTC")) { // Jackson uses UTC as the default, so let's not reset it
ZoneId zoneId = jacksonBuildTimeConfig.timezone();
if (!zoneId.getId().equals("UTC")) { // Jackson uses UTC as the default, so let's not reset it
objectMapper.setTimeZone(TimeZone.getTimeZone(zoneId));
}
if (jacksonSupport.configuredNamingStrategy().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,72 @@
import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class JacksonBuildTimeConfig {
@ConfigMapping(prefix = "quarkus.jackson")
public interface JacksonBuildTimeConfig {

/**
* If enabled, Jackson will fail when encountering unknown properties.
* <p>
* You can still override it locally with {@code @JsonIgnoreProperties(ignoreUnknown = false)}.
*/
@ConfigItem(defaultValue = "false")
public boolean failOnUnknownProperties;
@WithDefault("false")
boolean failOnUnknownProperties();

/**
* If enabled, Jackson will fail when no accessors are found for a type.
* This is enabled by default to match the default Jackson behavior.
*/
@ConfigItem(defaultValue = "true")
public boolean failOnEmptyBeans;
@WithDefault("true")
boolean failOnEmptyBeans();

/**
* If enabled, Jackson will serialize dates as numeric value(s).
* When disabled, they are serialized in ISO 8601 format.
*/
@ConfigItem(defaultValue = "false")
public boolean writeDatesAsTimestamps;
@WithDefault("false")
boolean writeDatesAsTimestamps();

/**
* If enabled, Jackson will serialize durations as numeric value(s).
* When disabled, they are serialized in ISO 8601 format.
* This is enabled by default to match the default Jackson behavior.
*/
@ConfigItem(defaultValue = "true")
public boolean writeDurationsAsTimestamps;
@WithDefault("true")
boolean writeDurationsAsTimestamps();

/**
* If enabled, Jackson will ignore case during Enum deserialization.
*/
@ConfigItem(defaultValue = "false")
public boolean acceptCaseInsensitiveEnums;
@WithDefault("false")
boolean acceptCaseInsensitiveEnums();

/**
* If set, Jackson will default to using the specified timezone when formatting dates.
* Some examples values are "Asia/Jakarta" and "GMT+3".
* If not set, Jackson will use its own default.
*/
@ConfigItem(defaultValue = "UTC")
public Optional<ZoneId> timezone;
@WithDefault("UTC")
ZoneId timezone();

/**
* Define which properties of Java Beans are to be included in serialization.
*/
@ConfigItem
public Optional<JsonInclude.Include> serializationInclusion;
Optional<JsonInclude.Include> serializationInclusion();

/**
* Defines how names of JSON properties ("external names") are derived
* from names of POJO methods and fields ("internal names").
* The value can be one of the one of the constants in {@link com.fasterxml.jackson.databind.PropertyNamingStrategies},
* so for example, {@code LOWER_CAMEL_CASE} or {@code UPPER_CAMEL_CASE}.
*
* <p>
* The value can also be a fully qualified class name of a {@link com.fasterxml.jackson.databind.PropertyNamingStrategy}
* subclass.
*/
@ConfigItem
public Optional<String> propertyNamingStrategy;
Optional<String> propertyNamingStrategy();
}
Loading