Skip to content

Commit

Permalink
eagerly loading schemas (#1125)
Browse files Browse the repository at this point in the history
Signed-off-by: Sachin Rana <sacrana324@gmail.com>
  • Loading branch information
sacrana0 authored Jan 22, 2025
1 parent f9ebe81 commit f7e969f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import javax.annotation.PostConstruct;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.stream.Collectors;


@Slf4j
Expand All @@ -29,21 +28,27 @@ public class ClaimsSchemaValidator implements ConstraintValidator<ClaimsSchema,
@Value("${mosip.esignet.claims.schema.url}")
private String schemaUrl;

private volatile JsonSchema cachedSchema;
private JsonSchema schema;

@Autowired
private ObjectMapper objectMapper;

@Autowired
private ResourceLoader resourceLoader;

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

@Override
public boolean isValid(ClaimsV2 claims, ConstraintValidatorContext context) {
Set<ValidationMessage> errors = null;
try {
JsonNode jsonNode = objectMapper.valueToTree(claims);
errors = getCachedSchema().validate(jsonNode);
errors = schema.validate(jsonNode);
if(errors.isEmpty())return true;
} catch (Exception e) {
log.error("Error validating claims schema", e);
Expand All @@ -52,18 +57,6 @@ public boolean isValid(ClaimsV2 claims, ConstraintValidatorContext context) {
return false;
}

private JsonSchema getCachedSchema() throws EsignetException {
if(cachedSchema!=null ) return cachedSchema;
synchronized (this) {
if (cachedSchema == null) {
InputStream schemaResponse = getResource(schemaUrl);
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);
cachedSchema = jsonSchemaFactory.getSchema(schemaResponse);
}
}
return cachedSchema;
}

private InputStream getResource(String url) {
try{
Resource resource = resourceLoader.getResource(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import javax.annotation.PostConstruct;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.io.IOException;
Expand All @@ -27,14 +28,21 @@ public class ClientAdditionalConfigValidator implements
@Value("${mosip.esignet.additional-config.schema.url}")
private String schemaUrl;

private volatile JsonSchema cachedSchema;
private JsonSchema schema;

@Autowired
private ObjectMapper objectMapper;

@Autowired
private ResourceLoader resourceLoader;

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

@Override
public void initialize(ClientAdditionalConfig constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
Expand All @@ -48,7 +56,7 @@ public boolean isValid(Map<String, Object> additionalConfig, ConstraintValidator
Set<ValidationMessage> errors = null;
try {
JsonNode jsonNode = objectMapper.valueToTree(additionalConfig);
errors = getCachedSchema().validate(jsonNode);
errors = schema.validate(jsonNode);
if (errors.isEmpty()) return true;
} catch (Exception e) {
log.error("Error validating additional_config schema: ", e);
Expand All @@ -57,18 +65,6 @@ public boolean isValid(Map<String, Object> additionalConfig, ConstraintValidator
return false;
}

private JsonSchema getCachedSchema() throws EsignetException {
if(cachedSchema!=null ) return cachedSchema;
synchronized (this) {
if (cachedSchema == null) {
InputStream schemaResponse = getResource(schemaUrl);
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);
cachedSchema = jsonSchemaFactory.getSchema(schemaResponse);
}
}
return cachedSchema;
}

private InputStream getResource(String url) {
try {
Resource resource = resourceLoader.getResource(url);
Expand Down
32 changes: 10 additions & 22 deletions esignet-core/src/test/java/io/mosip/esignet/core/ValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ public void setup() throws EsignetException {
discoveryMap.put("claims_supported", Arrays.asList("name", "gender", "address"));
when(authenticationContextClassRefUtil.getSupportedACRValues()).thenReturn(mockACRs);
when(authenticator.isSupportedOtpChannel("email")).thenReturn(true);

ReflectionTestUtils.setField(claimSchemaValidator,"resourceLoader",resourceLoader);
ReflectionTestUtils.setField(claimSchemaValidator,"objectMapper",mapper);
ReflectionTestUtils.setField(claimSchemaValidator,"schemaUrl","classpath:/verified_claims_request_schema_test.json");
claimSchemaValidator.initSchema();

ReflectionTestUtils.setField(clientAdditionalConfigValidator, "resourceLoader", resourceLoader);
ReflectionTestUtils.setField(clientAdditionalConfigValidator, "objectMapper", mapper);
ReflectionTestUtils.setField(clientAdditionalConfigValidator, "schemaUrl", "classpath:additional_config_request_schema.json");
clientAdditionalConfigValidator.initSchema();
}

// ============================ Display Validator =========================
Expand Down Expand Up @@ -677,10 +687,6 @@ public void test_ClientNameLangValidator_WithInValidDetail_thenFail() {
@Test
public void claimSchemaValidator_withValidDetails_thenPass() throws IOException {

ReflectionTestUtils.setField(claimSchemaValidator, "resourceLoader", resourceLoader);
ReflectionTestUtils.setField(claimSchemaValidator, "objectMapper", mapper);
ReflectionTestUtils.setField(claimSchemaValidator, "schemaUrl", "classpath:/verified_claims_request_schema_test.json");

String address = "{\"essential\":true}";
String verifiedClaims = "[{\"verification\":{\"trust_framework\":{\"value\":\"income-tax\"}},\"claims\":{\"name\":null,\"email\":{\"essential\":true}}},{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"birthdate\":{\"essential\":true},\"address\":null}},{\"verification\":{\"trust_framework\":{\"value\":\"kaif\"}},\"claims\":{\"gender\":{\"essential\":true},\"email\":{\"essential\":true}}}]";

Expand All @@ -705,10 +711,6 @@ public void claimSchemaValidator_withValidDetails_thenPass() throws IOException
@Test
public void claimSchemaValidator_withTrustFrameWorkAsNull_thenFail() throws IOException {

ReflectionTestUtils.setField(claimSchemaValidator, "resourceLoader", resourceLoader);
ReflectionTestUtils.setField(claimSchemaValidator, "objectMapper", mapper);
ReflectionTestUtils.setField(claimSchemaValidator, "schemaUrl", "classpath:/verified_claims_request_schema_test.json");

String address = "{\"essential\":true}";
String verifiedClaims = "[{\"verification\":{\"trust_framework\":{\"value\":null}},\"claims\":{\"name\":null,\"email\":{\"essential\":true}}},{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"birthdate\":{\"essential\":true},\"address\":null}},{\"verification\":{\"trust_framework\":{\"value\":\"kaif\"}},\"claims\":{\"gender\":{\"essential\":true},\"email\":{\"essential\":true}}}]";

Expand All @@ -733,10 +735,6 @@ public void claimSchemaValidator_withTrustFrameWorkAsNull_thenFail() throws IOEx
@Test
public void claimSchemaValidator_withEssentialAsNonBoolean_thenFail() throws IOException {

ReflectionTestUtils.setField(claimSchemaValidator, "resourceLoader", resourceLoader);
ReflectionTestUtils.setField(claimSchemaValidator, "objectMapper", mapper);
ReflectionTestUtils.setField(claimSchemaValidator, "schemaUrl", "classpath:/verified_claims_request_schema_test.json");

String address = "{\"essential\":true}";
String verifiedClaims = "[{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"name\":null,\"email\":{\"essential\":1}}},{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"birthdate\":{\"essential\":true},\"address\":null}},{\"verification\":{\"trust_framework\":{\"value\":\"kaif\"}},\"claims\":{\"gender\":{\"essential\":true},\"email\":{\"essential\":true}}}]";

Expand All @@ -761,10 +759,6 @@ public void claimSchemaValidator_withEssentialAsNonBoolean_thenFail() throws IOE
@Test
public void test_ClaimSchemaValidator_withInvalidValue_thenFail() throws IOException {

ReflectionTestUtils.setField(claimSchemaValidator, "resourceLoader", resourceLoader);
ReflectionTestUtils.setField(claimSchemaValidator, "objectMapper", mapper);
ReflectionTestUtils.setField(claimSchemaValidator, "schemaUrl", "classpath:/verified_claims_request_schema_test.json");

String address = "{\"essential\":true}";
String verifiedClaims = "[{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"name\":null,\"email\":{\"essential\":1}}},{\"verification\":{\"trust_framework\":{\"value\":\"pwd\"}},\"claims\":{\"birthdate\":{\"essential\":true},\"address\":null}},{\"verification\":{\"trust_framework\":{\"value\":\"kf\"}},\"claims\":{\"gender\":{\"essential\":true},\"email\":{\"essential\":true}}}]";

Expand Down Expand Up @@ -839,18 +833,12 @@ public static List<Map<String, Object>> getInvalidAdditionalConfigs() {

@Test
public void test_ClientAdditionalConfigValidator_withValidValue_thenPass() {
ReflectionTestUtils.setField(clientAdditionalConfigValidator, "resourceLoader", resourceLoader);
ReflectionTestUtils.setField(clientAdditionalConfigValidator, "objectMapper", mapper);
ReflectionTestUtils.setField(clientAdditionalConfigValidator, "schemaUrl", "classpath:additional_config_request_schema.json");
Map<String, Object> validAdditionalConfig = getValidAdditionalConfig();
Assert.assertTrue(clientAdditionalConfigValidator.isValid(validAdditionalConfig, null));
}

@Test
public void test_ClientAdditionalConfigValidator_withInvalidValue_thenFail() {
ReflectionTestUtils.setField(clientAdditionalConfigValidator, "resourceLoader", resourceLoader);
ReflectionTestUtils.setField(clientAdditionalConfigValidator, "objectMapper", mapper);
ReflectionTestUtils.setField(clientAdditionalConfigValidator, "schemaUrl", "classpath:additional_config_request_schema.json");
for (Map<String, Object> additionalConfig : getInvalidAdditionalConfigs()) {
Assert.assertFalse(clientAdditionalConfigValidator.isValid(additionalConfig, null));
}
Expand Down

0 comments on commit f7e969f

Please sign in to comment.