Skip to content

Commit

Permalink
DT-750 Caching schema validators
Browse files Browse the repository at this point in the history
  • Loading branch information
gj0dcsa committed Dec 9, 2023
1 parent a840d22 commit a26f5ac
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ public JsonSchemaValidator getMessageSchemaValidator(String apiName, String json
String schemaFilePath = "/standards/booking/schemas/booking-%s-v%s0.json"
.formatted(apiName, standardVersion.charAt(0));

return new JsonSchemaValidator(
BookingComponentFactory.class.getResourceAsStream(schemaFilePath), jsonSchema);
return JsonSchemaValidator.getInstance(schemaFilePath, jsonSchema);
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,44 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.*;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.SneakyThrows;

public class JsonSchemaValidator {
private static final Map<String, Map<String, JsonSchemaValidator>> INSTANCES = new HashMap<>();

public static synchronized JsonSchemaValidator getInstance(String filePath, String schemaName) {
return INSTANCES
.computeIfAbsent(filePath, (ignored) -> new HashMap<>())
.computeIfAbsent(schemaName, (ignored) -> new JsonSchemaValidator(filePath, schemaName));
}

private final JsonSchema jsonSchema;

public JsonSchemaValidator(InputStream schemaFileInputStream, String schemaName) {
@SneakyThrows
private JsonSchemaValidator(String filePath, String schemaName) {
// https://github.com/networknt/json-schema-validator/issues/579#issuecomment-1488269135
InputStream schemaFileInputStream = JsonSchemaValidator.class.getResourceAsStream(filePath);
JsonSchemaFactory jsonSchemaFactory =
JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7))
.objectMapper(new ObjectMapper(new JsonFactory()))
.build();
SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig();
schemaValidatorsConfig.setTypeLoose(true);
JsonSchema rootJsonSchema = jsonSchemaFactory.getSchema(schemaFileInputStream, schemaValidatorsConfig);
JsonSchema rootJsonSchema =
jsonSchemaFactory.getSchema(schemaFileInputStream, schemaValidatorsConfig);
ValidationContext validationContext =
new ValidationContext(
jsonSchemaFactory.getUriFactory(),
null,
JsonMetaSchema.getV4(),
jsonSchemaFactory,
schemaValidatorsConfig);
jsonSchema = jsonSchemaFactory.create(
jsonSchema =
jsonSchemaFactory.create(
validationContext,
"#/components/schemas/" + schemaName,
rootJsonSchema.getSchemaNode().get("components").get("schemas").get(schemaName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ public JsonSchemaValidator getMessageSchemaValidator(String apiProviderRole, boo
EblIssuanceRole.isCarrier(apiProviderRole)
? (forRequest ? "issuanceRequest" : null)
: (forRequest ? "issuanceResponse" : null);
return new JsonSchemaValidator(
EblIssuanceComponentFactory.class.getResourceAsStream(schemaFilePath), schemaName);
return JsonSchemaValidator.getInstance(schemaFilePath, schemaName);
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ public JsonSchemaValidator getMessageSchemaValidator(String apiProviderRole, boo
EblSurrenderRole.isCarrier(apiProviderRole)
? (forRequest ? "surrenderRequestDetails" : "surrenderRequestAcknowledgement")
: (forRequest ? "surrenderRequestAnswer" : null);
return new JsonSchemaValidator(
EblSurrenderComponentFactory.class.getResourceAsStream(schemaFilePath), schemaName);
return JsonSchemaValidator.getInstance(schemaFilePath, schemaName);
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ public JsonSchemaValidator getMessageSchemaValidator(String apiName, String json
String schemaFilePath = "/standards/ebl/schemas/ebl-%s-v%s0.json"
.formatted(apiName, standardVersion.charAt(0));

return new JsonSchemaValidator(
EblComponentFactory.class.getResourceAsStream(schemaFilePath), jsonSchema);
return JsonSchemaValidator.getInstance(schemaFilePath, jsonSchema);
}

@SneakyThrows
Expand Down

0 comments on commit a26f5ac

Please sign in to comment.