Skip to content

Commit

Permalink
changed lazy loading to eagerly loading of schema [ES-2153, ES-2155] (#…
Browse files Browse the repository at this point in the history
…321)

* changed lazy loading to eagerly loading of schema

Signed-off-by: Sachin Rana <[email protected]>

* removed annotation which was not required

Signed-off-by: Sachin Rana <[email protected]>

---------

Signed-off-by: Sachin Rana <[email protected]>
  • Loading branch information
sacrana0 authored Jan 21, 2025
1 parent 02fa4ca commit c55e1b9
Showing 1 changed file with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.*;
import io.mosip.esignet.mock.identitysystem.dto.IdentityData;
import io.mosip.esignet.mock.identitysystem.dto.RequestWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jose4j.lang.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
Expand All @@ -32,7 +32,7 @@ public class IdentitySchemaValidator implements ConstraintValidator<IdentitySche

private String action;

private volatile JsonSchema schema;
private JsonSchema schema;

@Autowired
ObjectMapper objectMapper;
Expand All @@ -46,6 +46,13 @@ public void initialize(IdentitySchema constraintAnnotation) {
this.action = constraintAnnotation.action();
}

@PostConstruct
public void initSchema() {
InputStream schemaResponse = getResource(identitySchemaUrl);
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);
schema = jsonSchemaFactory.getSchema(schemaResponse);
}

@Override
public boolean isValid(Object object, ConstraintValidatorContext context) {

Expand All @@ -65,7 +72,7 @@ public boolean isValid(Object object, ConstraintValidatorContext context) {
}

private Set<ValidationMessage> validateIdentityData(JsonNode identityJsonNode) {
Set<ValidationMessage> errors = getSchema().validate(identityJsonNode);
Set<ValidationMessage> errors = schema.validate(identityJsonNode);
// If not a create operation, filter out specific errors
if (action.equals("UPDATE")) {
// Ignore validation errors with code 1029 (null value) and for exempted fields when validating updateIdentity
Expand All @@ -85,18 +92,6 @@ private void addValidationErrorCode(Set<ValidationMessage> errors, ConstraintVal
.addConstraintViolation());
}

private JsonSchema getSchema() {
if(schema !=null ) return schema;
synchronized (this) {
if (schema == null) {
InputStream schemaResponse = getResource(identitySchemaUrl);
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);
schema = jsonSchemaFactory.getSchema(schemaResponse);
}
}
return schema;
}

private InputStream getResource(String url) {
try{
Resource resource = resourceLoader.getResource(url);
Expand Down

0 comments on commit c55e1b9

Please sign in to comment.