forked from quarkiverse/quarkus-qute-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkiverse#46 from ia3andy/use-configured-jackson…
…-with-new-typed-mapper Use Roq Jackson and with new specific mappers
- Loading branch information
Showing
13 changed files
with
205 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
common/deployment/src/main/java/io/quarkiverse/roq/deployment/RoqJacksonProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package io.quarkiverse.roq.deployment; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Optional; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; | ||
|
||
import io.quarkiverse.roq.deployment.config.RoqJacksonConfig; | ||
import io.quarkiverse.roq.deployment.items.RoqJacksonBuildItem; | ||
import io.quarkus.arc.impl.Reflections; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
|
||
public class RoqJacksonProcessor { | ||
|
||
private static final Logger LOG = Logger.getLogger(RoqJacksonProcessor.class); | ||
|
||
@BuildStep | ||
RoqJacksonBuildItem findProject(RoqJacksonConfig jacksonConfig) { | ||
YAMLMapper.Builder yamlMapperBuilder = YAMLMapper.builder(); | ||
JsonMapper.Builder jsonMapperBuilder = JsonMapper.builder(); | ||
if (!jacksonConfig.failOnUnknownProperties()) { | ||
// this feature is enabled by default, so we disable it | ||
yamlMapperBuilder.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | ||
jsonMapperBuilder.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | ||
} | ||
if (!jacksonConfig.failOnEmptyBeans()) { | ||
// this feature is enabled by default, so we disable it | ||
yamlMapperBuilder.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); | ||
jsonMapperBuilder.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); | ||
} | ||
|
||
if (jacksonConfig.acceptCaseInsensitiveEnums()) { | ||
yamlMapperBuilder.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS); | ||
jsonMapperBuilder.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS); | ||
} | ||
final Optional<PropertyNamingStrategy> propertyNamingStrategy = determinePropertyNamingStrategyClassName(jacksonConfig); | ||
if (propertyNamingStrategy.isPresent()) { | ||
jsonMapperBuilder.propertyNamingStrategy(propertyNamingStrategy.get()); | ||
yamlMapperBuilder.propertyNamingStrategy(propertyNamingStrategy.get()); | ||
} | ||
return new RoqJacksonBuildItem(jsonMapperBuilder.build(), yamlMapperBuilder.build()); | ||
} | ||
|
||
private Optional<PropertyNamingStrategy> determinePropertyNamingStrategyClassName(RoqJacksonConfig jacksonConfig) { | ||
if (jacksonConfig.propertyNamingStrategy().isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
var propertyNamingStrategy = jacksonConfig.propertyNamingStrategy().get(); | ||
Field field; | ||
|
||
try { | ||
// let's first try and see if the value is a constant defined in PropertyNamingStrategies | ||
field = Reflections.findField(PropertyNamingStrategies.class, propertyNamingStrategy); | ||
} catch (Exception e) { | ||
// the provided value does not correspond to any of the defined constants, so let's see if it's actually a class name | ||
try { | ||
var clazz = Thread.currentThread().getContextClassLoader().loadClass(propertyNamingStrategy); | ||
if (PropertyNamingStrategy.class.isAssignableFrom(clazz)) { | ||
return Optional.of((PropertyNamingStrategy) clazz.getConstructor().newInstance()); | ||
} | ||
throw new RuntimeException(invalidPropertyNameStrategyValueMessage(propertyNamingStrategy)); | ||
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | ||
| InvocationTargetException ex) { | ||
throw new RuntimeException(invalidPropertyNameStrategyValueMessage(propertyNamingStrategy)); | ||
} | ||
} | ||
|
||
try { | ||
// we have a matching field, so let's see if the type is correct | ||
final Object value = field.get(null); | ||
Class<?> clazz = value.getClass(); | ||
if (PropertyNamingStrategy.class.isAssignableFrom(clazz)) { | ||
return Optional.of((PropertyNamingStrategy) value); | ||
} | ||
throw new RuntimeException(invalidPropertyNameStrategyValueMessage(propertyNamingStrategy)); | ||
} catch (IllegalAccessException e) { | ||
// shouldn't ever happen | ||
throw new RuntimeException(invalidPropertyNameStrategyValueMessage(propertyNamingStrategy)); | ||
} | ||
} | ||
|
||
private static String invalidPropertyNameStrategyValueMessage(String propertyNamingStrategy) { | ||
return "Unable to determine the property naming strategy for value '" + propertyNamingStrategy | ||
+ "'. Make sure that the value is either a fully qualified class name of a subclass of '" | ||
+ PropertyNamingStrategy.class.getName() | ||
+ "' or one of the constants defined in '" + PropertyNamingStrategies.class.getName() + "'."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
common/deployment/src/main/java/io/quarkiverse/roq/deployment/items/RoqJacksonBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.quarkiverse.roq.deployment.items; | ||
|
||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; | ||
|
||
import io.quarkus.builder.item.SimpleBuildItem; | ||
|
||
public final class RoqJacksonBuildItem extends SimpleBuildItem { | ||
|
||
private final JsonMapper jsonMapper; | ||
private final YAMLMapper yamlMapper; | ||
|
||
public RoqJacksonBuildItem(JsonMapper jsonMapper, YAMLMapper yamlMapper) { | ||
this.jsonMapper = jsonMapper; | ||
this.yamlMapper = yamlMapper; | ||
} | ||
|
||
public JsonMapper getJsonMapper() { | ||
return jsonMapper; | ||
} | ||
|
||
public YAMLMapper getYamlMapper() { | ||
return yamlMapper; | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
...eployment/src/main/java/io/quarkiverse/roq/deployment/items/RoqObjectMapperBuildItem.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ment/src/main/java/io/quarkiverse/roq/data/deployment/converters/DataConverterFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.quarkiverse.roq.data.deployment.converters; | ||
|
||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; | ||
|
||
import io.quarkiverse.roq.data.deployment.DataConverter; | ||
|
||
public final class DataConverterFinder { | ||
|
||
private final Map<String, DataConverter> converterByExtension; | ||
|
||
public DataConverterFinder(JsonMapper jsonMapper, YAMLMapper yamlMapper) { | ||
DataConverter jsonConverter = new JsonConverter(jsonMapper); | ||
DataConverter yamlConverter = new YamlConverter(yamlMapper); | ||
this.converterByExtension = Map.of( | ||
"yaml", yamlConverter, | ||
"yml", yamlConverter, | ||
"json", jsonConverter); | ||
} | ||
|
||
public DataConverter fromFileName(String fileName) { | ||
if (!fileName.contains(".")) { | ||
return null; | ||
} | ||
final String extension = fileName.substring(fileName.lastIndexOf(".") + 1); | ||
return converterByExtension.get(extension); | ||
} | ||
|
||
} |
Oops, something went wrong.