Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce cache loaded extensions in OpenAPIDeserializer #1997

Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public void processSchema(Schema schema) {
return;
}
if (openapi31) {
// TODO use as singleton somewhere loaded as static used by both here and deserializer
List<JsonSchemaParserExtension> jsonschemaExtensions = OpenAPIDeserializer.getJsonSchemaParserExtensions();
for (JsonSchemaParserExtension jsonschemaExtension: jsonschemaExtensions) {
if (jsonschemaExtension.resolveSchema(schema, cache, openAPI, openapi31)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ public class OpenAPIDeserializer {
"(\\d{2}):(\\d{2})(\\.\\d+)?((Z)|([+-]\\d{2}:\\d{2}))$");
private static final Pattern RFC3339_DATE_PATTERN = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$");
private static final String REFERENCE_SEPARATOR = "#/";

private static final int MAX_EXTENSION_ENTRIES = 20;

// Holds extensions to a given classloader. Implemented as a least-recently used cache
private static final Map<ClassLoader, List<JsonSchemaParserExtension>> jsonSchemaParserExtensionMap = new LinkedHashMap<ClassLoader, List<JsonSchemaParserExtension>>() {
@Override
protected boolean removeEldestEntry(Map.Entry<ClassLoader, List<JsonSchemaParserExtension>> eldest) {
return size() > MAX_EXTENSION_ENTRIES;
}
};

private Components components;
private JsonNode rootNode;
private Map<String, Object> rootMap;
Expand Down Expand Up @@ -3780,15 +3791,30 @@ public static List<JsonSchemaParserExtension> getJsonSchemaParserExtensions() {
return extensions;
}

protected static List<JsonSchemaParserExtension> getJsonSchemaParserExtensions(ClassLoader cl) {
final List<JsonSchemaParserExtension> extensions = new ArrayList<>();
/**
* Locates the extensions for given {@link ClassLoader} and stores them for performance reason in an in-memory
* (LRU) cache.
*
* @param cl the {@link ClassLoader} for which the extensions are located
* @return
*/
protected static List<JsonSchemaParserExtension> getJsonSchemaParserExtensions(ClassLoader cl) {
if (jsonSchemaParserExtensionMap.containsKey(cl)) {
return jsonSchemaParserExtensionMap.get(cl);
}

final ServiceLoader<JsonSchemaParserExtension> loader = ServiceLoader.load(JsonSchemaParserExtension.class, cl);
for (JsonSchemaParserExtension extension : loader) {
extensions.add(extension);
}
return extensions;
}
final List<JsonSchemaParserExtension> extensions = new ArrayList<>();
final ServiceLoader<JsonSchemaParserExtension> loader = ServiceLoader.load(JsonSchemaParserExtension.class, cl);
for (JsonSchemaParserExtension extension : loader) {
extensions.add(extension);
}

// don't cache null-Value classLoader (e.g. Bootstrap Classloader)
if (cl != null) {
jsonSchemaParserExtensionMap.put(cl, extensions);
}
return extensions;
}

public Schema getJsonSchema(JsonNode jsonNode, String location, ParseResult result) {
if (jsonNode == null) {
Expand Down