Skip to content

Commit 9639d9e

Browse files
committed
Use new utility module for JSON config
1 parent 11d9d9a commit 9639d9e

File tree

3 files changed

+16
-34
lines changed

3 files changed

+16
-34
lines changed

powertools-validation/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
<groupId>software.amazon.lambda</groupId>
4747
<artifactId>powertools-core</artifactId>
4848
</dependency>
49+
<dependency>
50+
<groupId>software.amazon.lambda</groupId>
51+
<artifactId>powertools-utilities</artifactId>
52+
</dependency>
4953
<dependency>
5054
<groupId>com.amazonaws</groupId>
5155
<artifactId>aws-lambda-java-events</artifactId>

powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/ValidationConfig.java

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818
import com.networknt.schema.JsonSchemaFactory;
1919
import com.networknt.schema.SpecVersion;
2020
import io.burt.jmespath.JmesPath;
21-
import io.burt.jmespath.RuntimeConfiguration;
2221
import io.burt.jmespath.function.BaseFunction;
23-
import io.burt.jmespath.function.FunctionRegistry;
24-
import io.burt.jmespath.jackson.JacksonRuntime;
25-
import software.amazon.lambda.powertools.validation.jmespath.Base64Function;
26-
import software.amazon.lambda.powertools.validation.jmespath.Base64GZipFunction;
27-
28-
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
22+
import software.amazon.lambda.powertools.utilities.JsonConfig;
23+
import software.amazon.lambda.powertools.utilities.jmespath.Base64Function;
24+
import software.amazon.lambda.powertools.utilities.jmespath.Base64GZipFunction;
2925

3026
/**
3127
* Use this if you need to customize some part of the JSON Schema validation
32-
* (eg. specification version, Jackson ObjectMapper, or adding functions to JMESPath)
28+
* (eg. specification version, Jackson ObjectMapper, or adding functions to JMESPath).
29+
*
30+
* For everything but the validation features (factory, schemaVersion), {@link ValidationConfig}
31+
* is just a wrapper of {@link JsonConfig}.
3332
*/
3433
public class ValidationConfig {
3534
private ValidationConfig() {
@@ -43,24 +42,9 @@ public static ValidationConfig get() {
4342
return ConfigHolder.instance;
4443
}
4544

46-
private static final ThreadLocal<ObjectMapper> om = ThreadLocal.withInitial(() -> {
47-
ObjectMapper objectMapper = new ObjectMapper();
48-
objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
49-
return objectMapper;
50-
});
51-
5245
private SpecVersion.VersionFlag jsonSchemaVersion = SpecVersion.VersionFlag.V7;
5346
private JsonSchemaFactory factory = JsonSchemaFactory.getInstance(jsonSchemaVersion);
5447

55-
private final FunctionRegistry defaultFunctions = FunctionRegistry.defaultRegistry();
56-
private final FunctionRegistry customFunctions = defaultFunctions.extend(
57-
new Base64Function(),
58-
new Base64GZipFunction());
59-
private final RuntimeConfiguration configuration = new RuntimeConfiguration.Builder()
60-
.withFunctionRegistry(customFunctions)
61-
.build();
62-
private JmesPath<JsonNode> jmesPath = new JacksonRuntime(configuration, getObjectMapper());
63-
6448
/**
6549
* Set the version of the json schema specifications (default is V7)
6650
*
@@ -85,13 +69,7 @@ public SpecVersion.VersionFlag getSchemaVersion() {
8569
* @param <T> Must extends {@link BaseFunction}
8670
*/
8771
public <T extends BaseFunction> void addFunction(T function) {
88-
FunctionRegistry functionRegistryWithExtendedFunctions = configuration.functionRegistry().extend(function);
89-
90-
RuntimeConfiguration updatedConfig = new RuntimeConfiguration.Builder()
91-
.withFunctionRegistry(functionRegistryWithExtendedFunctions)
92-
.build();
93-
94-
jmesPath = new JacksonRuntime(updatedConfig, getObjectMapper());
72+
JsonConfig.get().addFunction(function);
9573
}
9674

9775
/**
@@ -109,7 +87,7 @@ public JsonSchemaFactory getFactory() {
10987
* @return the {@link JmesPath}
11088
*/
11189
public JmesPath<JsonNode> getJmesPath() {
112-
return jmesPath;
90+
return JsonConfig.get().getJmesPath();
11391
}
11492

11593
/**
@@ -118,6 +96,6 @@ public JmesPath<JsonNode> getJmesPath() {
11896
* @return the {@link ObjectMapper} to serialize / deserialize JSON
11997
*/
12098
public ObjectMapper getObjectMapper() {
121-
return om.get();
99+
return JsonConfig.get().getObjectMapper();
122100
}
123101
}

powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/internal/ValidationAspect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
import static java.nio.charset.StandardCharsets.UTF_8;
2727
import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.isHandlerMethod;
2828
import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.placedOnRequestHandler;
29+
import static software.amazon.lambda.powertools.utilities.jmespath.Base64Function.decode;
30+
import static software.amazon.lambda.powertools.utilities.jmespath.Base64GZipFunction.decompress;
2931
import static software.amazon.lambda.powertools.validation.ValidationUtils.getJsonSchema;
3032
import static software.amazon.lambda.powertools.validation.ValidationUtils.validate;
31-
import static software.amazon.lambda.powertools.validation.jmespath.Base64Function.decode;
32-
import static software.amazon.lambda.powertools.validation.jmespath.Base64GZipFunction.decompress;
3333

3434
/**
3535
* Aspect for {@link Validation} annotation

0 commit comments

Comments
 (0)