Bump json-schema-validator from 1.5.7 to 2.0.0 (#660)

This commit is contained in:
Uladzislau Arlouski 2025-11-12 16:21:09 +03:00 • committed by GitHub
commit ed85f7ee3e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 19 deletions

View file

@ -3,17 +3,17 @@
*/ */
package io.modelcontextprotocol.json.schema.jackson; package io.modelcontextprotocol.json.schema.jackson;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchema; import com.networknt.schema.Schema;
import com.networknt.schema.JsonSchemaFactory; import com.networknt.schema.SchemaRegistry;
import com.networknt.schema.SpecVersion; import com.networknt.schema.Error;
import com.networknt.schema.ValidationMessage; import com.networknt.schema.dialect.Dialects;
import io.modelcontextprotocol.json.schema.JsonSchemaValidator; import io.modelcontextprotocol.json.schema.JsonSchemaValidator;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -31,10 +31,10 @@ public class DefaultJsonSchemaValidator implements JsonSchemaValidator {
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;
private final JsonSchemaFactory schemaFactory; private final SchemaRegistry schemaFactory;
// TODO: Implement a strategy to purge the cache (TTL, size limit, etc.) // TODO: Implement a strategy to purge the cache (TTL, size limit, etc.)
private final ConcurrentHashMap<String, JsonSchema> schemaCache; private final ConcurrentHashMap<String, Schema> schemaCache;
public DefaultJsonSchemaValidator() { public DefaultJsonSchemaValidator() {
this(new ObjectMapper()); this(new ObjectMapper());
@ -42,7 +42,7 @@ public class DefaultJsonSchemaValidator implements JsonSchemaValidator {
public DefaultJsonSchemaValidator(ObjectMapper objectMapper) { public DefaultJsonSchemaValidator(ObjectMapper objectMapper) {
this.objectMapper = objectMapper; this.objectMapper = objectMapper;
this.schemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012); this.schemaFactory = SchemaRegistry.withDialect(Dialects.getDraft202012());
this.schemaCache = new ConcurrentHashMap<>(); this.schemaCache = new ConcurrentHashMap<>();
} }
@ -62,7 +62,7 @@ public class DefaultJsonSchemaValidator implements JsonSchemaValidator {
? this.objectMapper.readTree((String) structuredContent) ? this.objectMapper.readTree((String) structuredContent)
: this.objectMapper.valueToTree(structuredContent); : this.objectMapper.valueToTree(structuredContent);
Set<ValidationMessage> validationResult = this.getOrCreateJsonSchema(schema).validate(jsonStructuredOutput); List<Error> validationResult = this.getOrCreateJsonSchema(schema).validate(jsonStructuredOutput);
// Check if validation passed // Check if validation passed
if (!validationResult.isEmpty()) { if (!validationResult.isEmpty()) {
@ -85,36 +85,36 @@ public class DefaultJsonSchemaValidator implements JsonSchemaValidator {
} }
/** /**
* Gets a cached JsonSchema or creates and caches a new one. * Gets a cached Schema or creates and caches a new one.
* @param schema the schema map to convert * @param schema the schema map to convert
* @return the compiled JsonSchema * @return the compiled Schema
* @throws JsonProcessingException if schema processing fails * @throws JsonProcessingException if schema processing fails
*/ */
private JsonSchema getOrCreateJsonSchema(Map<String, Object> schema) throws JsonProcessingException { private Schema getOrCreateJsonSchema(Map<String, Object> schema) throws JsonProcessingException {
// Generate cache key based on schema content // Generate cache key based on schema content
String cacheKey = this.generateCacheKey(schema); String cacheKey = this.generateCacheKey(schema);
// Try to get from cache first // Try to get from cache first
JsonSchema cachedSchema = this.schemaCache.get(cacheKey); Schema cachedSchema = this.schemaCache.get(cacheKey);
if (cachedSchema != null) { if (cachedSchema != null) {
return cachedSchema; return cachedSchema;
} }
// Create new schema if not in cache // Create new schema if not in cache
JsonSchema newSchema = this.createJsonSchema(schema); Schema newSchema = this.createJsonSchema(schema);
// Cache the schema // Cache the schema
JsonSchema existingSchema = this.schemaCache.putIfAbsent(cacheKey, newSchema); Schema existingSchema = this.schemaCache.putIfAbsent(cacheKey, newSchema);
return existingSchema != null ? existingSchema : newSchema; return existingSchema != null ? existingSchema : newSchema;
} }
/** /**
* Creates a new JsonSchema from the given schema map. * Creates a new Schema from the given schema map.
* @param schema the schema map * @param schema the schema map
* @return the compiled JsonSchema * @return the compiled Schema
* @throws JsonProcessingException if schema processing fails * @throws JsonProcessingException if schema processing fails
*/ */
private JsonSchema createJsonSchema(Map<String, Object> schema) throws JsonProcessingException { private Schema createJsonSchema(Map<String, Object> schema) throws JsonProcessingException {
// Convert schema map directly to JsonNode (more efficient than string // Convert schema map directly to JsonNode (more efficient than string
// serialization) // serialization)
JsonNode schemaNode = this.objectMapper.valueToTree(schema); JsonNode schemaNode = this.objectMapper.valueToTree(schema);

View file

@ -96,7 +96,7 @@
<awaitility.version>4.2.0</awaitility.version> <awaitility.version>4.2.0</awaitility.version>
<bnd-maven-plugin.version>7.1.0</bnd-maven-plugin.version> <bnd-maven-plugin.version>7.1.0</bnd-maven-plugin.version>
<json-unit-assertj.version>4.1.0</json-unit-assertj.version> <json-unit-assertj.version>4.1.0</json-unit-assertj.version>
<json-schema-validator.version>1.5.7</json-schema-validator.version> <json-schema-validator.version>2.0.0</json-schema-validator.version>
</properties> </properties>