Skip to content

Commit

Permalink
Fix #42: RoqObjectMapperBuildItem
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Jul 25, 2024
1 parent 6f09bf2 commit 19ab31d
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 0 deletions.
4 changes: 4 additions & 0 deletions common/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.roq</groupId>
<artifactId>quarkus-roq-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.ZoneId;
import java.util.TimeZone;

import org.jboss.logging.Logger;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;

import io.quarkiverse.roq.deployment.config.RoqConfig;
import io.quarkiverse.roq.deployment.config.RoqJacksonConfig;
import io.quarkiverse.roq.deployment.items.RoqObjectMapperBuildItem;
import io.quarkiverse.roq.deployment.items.RoqProjectBuildItem;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
Expand Down Expand Up @@ -37,6 +48,67 @@ RoqProjectBuildItem findProject(RoqConfig config, OutputTargetBuildItem outputTa
return roqProject;
}

@BuildStep
RoqObjectMapperBuildItem findProject(RoqJacksonConfig jacksonConfig) {
ObjectMapper objectMapper = new ObjectMapper();
if (!jacksonConfig.failOnUnknownProperties()) {
// this feature is enabled by default, so we disable it
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
if (!jacksonConfig.failOnEmptyBeans()) {
// this feature is enabled by default, so we disable it
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
if (!jacksonConfig.writeDatesAsTimestamps()) {
// this feature is enabled by default, so we disable it
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
if (!jacksonConfig.writeDurationsAsTimestamps()) {
// this feature is enabled by default, so we disable it
objectMapper.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS);
}
if (jacksonConfig.acceptCaseInsensitiveEnums()) {
objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
}
JsonInclude.Include serializationInclusion = jacksonConfig.serializationInclusion().orElse(null);
if (serializationInclusion != null) {
objectMapper.setSerializationInclusion(serializationInclusion);
}
ZoneId zoneId = jacksonConfig.timezone().orElse(null);
if ((zoneId != null) && !zoneId.getId().equals("UTC")) { // Jackson uses UTC as the default, so let's not reset it
objectMapper.setTimeZone(TimeZone.getTimeZone(zoneId));
}
if (jacksonConfig.propertyNamingStrategy().isPresent()) {
String propertyNamingStrategy = jacksonConfig.propertyNamingStrategy().get();
switch (propertyNamingStrategy) {
case "KEBAB_CASE":
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.KEBAB_CASE);
break;
case "SNAKE_CASE":
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
break;
case "LOWER_CAMEL_CASE":
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE);
break;
case "LOWER_CASE":
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CASE);
break;
case "LOWER_DOT_CASE":
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_DOT_CASE);
break;
case "UPPER_CAMEL_CASE":
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.UPPER_CAMEL_CASE);
break;
case "UPPER_SNAKE_CASE":
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.UPPER_SNAKE_CASE);
break;
default:
throw new IllegalArgumentException("Invalid propertyNamingStrategy: " + propertyNamingStrategy);
}
}
return new RoqObjectMapperBuildItem(objectMapper);
}

/**
* Resolves the project directories based on the provided configuration and output target.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.quarkiverse.roq.deployment.config;

import java.time.ZoneId;
import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonInclude;

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

@ConfigMapping(prefix = "quarkus.roq.jackson")
@ConfigRoot(phase = ConfigPhase.BUILD_TIME)
public interface RoqJacksonConfig {

/**
* If enabled, Jackson will fail when encountering unknown properties.
* <p>
* You can still override it locally with {@code @JsonIgnoreProperties(ignoreUnknown = false)}.
*/
@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.
*/
@WithDefault("true")
boolean failOnEmptyBeans();

/**
* If enabled, Jackson will serialize dates as numeric value(s).
* When disabled, they are serialized in ISO 8601 format.
*/
@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.
*/
@WithDefault("true")
boolean writeDurationsAsTimestamps();

/**
* If enabled, Jackson will ignore case during Enum deserialization.
*/
@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.
*/
@WithDefault("UTC")
Optional<ZoneId> timezone();

/**
* Define which properties of Java Beans are to be included in serialization.
*/
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}.
*
* The value can also be a fully qualified class name of a {@link com.fasterxml.jackson.databind.PropertyNamingStrategy}
* subclass.
*/
Optional<String> propertyNamingStrategy();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkiverse.roq.deployment.items;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.builder.item.SimpleBuildItem;

public final class RoqObjectMapperBuildItem extends SimpleBuildItem {

private final ObjectMapper objectMapper;

public RoqObjectMapperBuildItem(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

public ObjectMapper getObjectMapper() {
return objectMapper;
}
}
4 changes: 4 additions & 0 deletions roq-data/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.roq</groupId>
<artifactId>quarkus-roq-common-deployment</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions roq-data/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson</artifactId>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
Expand Down
4 changes: 4 additions & 0 deletions roq-generator/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.roq</groupId>
<artifactId>quarkus-roq-generator</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions roq-generator/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http</artifactId>
Expand Down

0 comments on commit 19ab31d

Please sign in to comment.