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

fix(core): prevent invalid plugin from breaking JSON schema generation (#4159) #4420

Merged
merged 1 commit into from
Jul 24, 2024
Merged
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
21 changes: 15 additions & 6 deletions core/src/main/java/io/kestra/core/docs/JsonSchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,39 +296,38 @@ public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, Sch
.stream()
.flatMap(registeredPlugin -> registeredPlugin.getTasks().stream())
.filter(Predicate.not(io.kestra.core.models.Plugin::isInternal))
.map(clz -> typeContext.resolveSubtype(declaredType, clz))
.flatMap(clz -> safelyResolveSubtype(declaredType, clz, typeContext).stream())
.toList();
} else if (declaredType.getErasedType() == AbstractTrigger.class) {
return getRegisteredPlugins()
.stream()
.flatMap(registeredPlugin -> registeredPlugin.getTriggers().stream())
.filter(Predicate.not(io.kestra.core.models.Plugin::isInternal))
.map(clz -> typeContext.resolveSubtype(declaredType, clz))
.flatMap(clz -> safelyResolveSubtype(declaredType, clz, typeContext).stream())
.toList();
} else if (declaredType.getErasedType() == Condition.class) {
return getRegisteredPlugins()
.stream()
.flatMap(registeredPlugin -> registeredPlugin.getConditions().stream())
.filter(Predicate.not(io.kestra.core.models.Plugin::isInternal))
.map(clz -> typeContext.resolveSubtype(declaredType, clz))
.flatMap(clz -> safelyResolveSubtype(declaredType, clz, typeContext).stream())
.toList();
} else if (declaredType.getErasedType() == ScheduleCondition.class) {
return getRegisteredPlugins()
.stream()
.flatMap(registeredPlugin -> registeredPlugin.getConditions().stream())
.filter(ScheduleCondition.class::isAssignableFrom)
.filter(Predicate.not(io.kestra.core.models.Plugin::isInternal))
.map(clz -> typeContext.resolveSubtype(declaredType, clz))
.flatMap(clz -> safelyResolveSubtype(declaredType, clz, typeContext).stream())
.toList();
} else if (declaredType.getErasedType() == TaskRunner.class) {
return getRegisteredPlugins()
.stream()
.flatMap(registeredPlugin -> registeredPlugin.getTaskRunners().stream())
.filter(Predicate.not(io.kestra.core.models.Plugin::isInternal))
.map(clz -> typeContext.resolveSubtype(declaredType, clz))
.flatMap(clz -> safelyResolveSubtype(declaredType, clz, typeContext).stream())
.toList();
}

return null;
});
// description as Markdown
Expand Down Expand Up @@ -413,6 +412,16 @@ public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, Sch
}
}

private static Optional<ResolvedType> safelyResolveSubtype(ResolvedType declaredType, Class<?> clz, TypeContext typeContext) {
try {
return Optional.ofNullable(typeContext.resolveSubtype(declaredType, clz));
} catch (Exception e) {
// exception can be thrown when resolving a plugin-type depending on
// a non-backward compatible kestra (e.g., java.lang.TypeNotPresentException).
return Optional.empty();
}
}

protected List<RegisteredPlugin> getRegisteredPlugins() {
return pluginRegistry.plugins();
}
Expand Down