Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 28 additions & 0 deletions docs/utilities/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,31 @@ If you need to configure the Jackson ObjectMapper, you can use the `ValidationCo
}
}
```

## Advanced

### SnapStart priming
To enable priming with AWS Lambda SnapStart, you need an explicit reference to `ValidationConfig` to allow the library to register before SnapStart takes a memory snapshot.

The `@Validation` annotation uses AspectJ compile-time weaving, which means there may be no runtime reference that triggers `ValidationConfig` to load during the INIT phase.
Hence without explicit reference, the priming logic will not execute.

So to ensure `ValidationConfig` is loaded, you need to add an explicit reference in your handler class. This can be done by adding one of the following to your handler class:

=== "Constructor"

```java hl_lines="3"
...
public MyHandler() {
ValidationConfig.get(); // Ensure ValidationConfig is loaded for SnapStart
}
```

=== "Static Initializer"

```java hl_lines="3"
...
static {
ValidationConfig.get(); // Ensure ValidationConfig is loaded for SnapStart
}
```
25 changes: 25 additions & 0 deletions powertools-validation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-serialization</artifactId>
</dependency>
<dependency>
<groupId>org.crac</groupId>
<artifactId>crac</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down Expand Up @@ -119,4 +123,25 @@
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>generate-classesloaded-file</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-Xlog:class+load=info:classesloaded.txt
--add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import io.burt.jmespath.JmesPath;
import io.burt.jmespath.function.BaseFunction;
import org.crac.Context;
import org.crac.Core;
import org.crac.Resource;
import software.amazon.lambda.powertools.common.internal.ClassPreLoader;
import software.amazon.lambda.powertools.utilities.JsonConfig;
import software.amazon.lambda.powertools.utilities.jmespath.Base64Function;
import software.amazon.lambda.powertools.utilities.jmespath.Base64GZipFunction;
Expand All @@ -31,10 +36,15 @@
* For everything but the validation features (factory, schemaVersion), {@link ValidationConfig}
* is just a wrapper of {@link JsonConfig}.
*/
public class ValidationConfig {
public class ValidationConfig implements Resource {

Check failure on line 39 in powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/ValidationConfig.java

View workflow job for this annotation

GitHub Actions / pmd_analyse

This class has only private constructors and may be final

Reports classes that may be made final because they cannot be extended from outside their compilation unit anyway. This is because all their constructors are private, so a subclass could not call the super constructor. ClassWithOnlyPrivateConstructorsShouldBeFinal (Priority: 1, Ruleset: Design) https://docs.pmd-code.org/snapshot/pmd_rules_java_design.html#classwithonlyprivateconstructorsshouldbefinal
private SpecVersion.VersionFlag jsonSchemaVersion = SpecVersion.VersionFlag.V7;
private JsonSchemaFactory factory = JsonSchemaFactory.getInstance(jsonSchemaVersion);

// Static block to ensure CRaC registration happens at class loading time
static {
Core.getGlobalContext().register(get());
}

private ValidationConfig() {
}

Expand Down Expand Up @@ -102,7 +112,27 @@
return JsonConfig.get().getObjectMapper();
}

@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
// Initialize key components
getObjectMapper();
getJmesPath();
getFactory();

// Dummy validation
String sampleSchema = "{\"type\":\"object\"}";
JsonSchema schema = ValidationUtils.getJsonSchema(sampleSchema);
ValidationUtils.validate("{\"test\":\"dummy\"}", schema);

ClassPreLoader.preloadClasses();
}

@Override
public void afterRestore(Context<? extends Resource> context) throws Exception {
// No action needed after restore
}

private static class ConfigHolder {

Check failure on line 135 in powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/ValidationConfig.java

View workflow job for this annotation

GitHub Actions / pmd_analyse

This class has only private constructors and may be final

Reports classes that may be made final because they cannot be extended from outside their compilation unit anyway. This is because all their constructors are private, so a subclass could not call the super constructor. ClassWithOnlyPrivateConstructorsShouldBeFinal (Priority: 1, Ruleset: Design) https://docs.pmd-code.org/snapshot/pmd_rules_java_design.html#classwithonlyprivateconstructorsshouldbefinal
private static final ValidationConfig instance = new ValidationConfig();
}
}
Loading
Loading