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/not required in case of default #7407

Merged
merged 3 commits into from
Feb 18, 2025
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
23 changes: 23 additions & 0 deletions core/src/main/java/io/kestra/core/docs/JsonSchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,35 @@ public <T> Map<String, Object> schemas(Class<? extends T> cls, boolean arrayOf)
}
replaceAnyOfWithOneOf(objectNode);
pullOfDefaultFromOneOf(objectNode);
removeRequiredOnPropsWithDefaults(objectNode);

return JacksonMapper.toMap(objectNode);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Unable to generate jsonschema for '" + cls.getName() + "'", e);
}
}

private void removeRequiredOnPropsWithDefaults(ObjectNode objectNode) {
objectNode.findParents("required").forEach(jsonNode -> {
if (jsonNode instanceof ObjectNode clazzSchema && clazzSchema.get("required") instanceof ArrayNode requiredPropsNode && clazzSchema.get("properties") instanceof ObjectNode properties) {
List<String> requiredFieldValues = StreamSupport.stream(requiredPropsNode.spliterator(), false)
.map(JsonNode::asText)
.toList();

properties.fields().forEachRemaining(e -> {
int indexInRequiredArray = requiredFieldValues.indexOf(e.getKey());
if (indexInRequiredArray != -1 && e.getValue() instanceof ObjectNode valueNode && valueNode.has("default")) {
requiredPropsNode.remove(indexInRequiredArray);
}
});

if (requiredPropsNode.isEmpty()) {
clazzSchema.remove("required");
}
}
});
}

private void replaceAnyOfWithOneOf(ObjectNode objectNode) {
objectNode.findParents("anyOf").forEach(jsonNode -> {
if (jsonNode instanceof ObjectNode oNode) {
Expand Down Expand Up @@ -605,6 +627,7 @@ protected <T> Map<String, Object> generate(Class<? extends T> cls, @Nullable Cla
ObjectNode objectNode = generator.generateSchema(cls);
replaceAnyOfWithOneOf(objectNode);
pullOfDefaultFromOneOf(objectNode);
removeRequiredOnPropsWithDefaults(objectNode);

return JacksonMapper.toMap(extractMainRef(objectNode));
} catch (IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.kestra.plugin.core.log.Log;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.inject.Inject;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -238,6 +239,15 @@ void betaTask() {
assertThat(((Map<String, Map<String, Object>>) generate.get("properties")).get("beta").get("$beta"), is(true));
}

@SuppressWarnings("unchecked")
@Test
void requiredAreRemovedIfThereIsADefault() {
Map<String, Object> generate = jsonSchemaGenerator.properties(Task.class, RequiredWithDefault.class);
assertThat(generate, is(not(nullValue())));
assertThat((List<String>) generate.get("required"), not(containsInAnyOrder("requiredWithDefault")));
assertThat((List<String>) generate.get("required"), containsInAnyOrder("requiredWithNoDefault"));
}

@SuppressWarnings("unchecked")
@Test
void dashboard() throws URISyntaxException {
Expand Down Expand Up @@ -324,6 +334,7 @@ private enum TestEnum {
}

@Schema(title = "Test class")
@Builder
private static class TestClass {
@Schema(title = "Test property")
public String testProperty;
Expand Down Expand Up @@ -360,4 +371,21 @@ public VoidOutput sendLogs(RunContext runContext, Flux<LogRecord> logRecord) thr
return null;
}
}

@SuperBuilder
@ToString
@EqualsAndHashCode
@Getter
@NoArgsConstructor
@Plugin
public static class RequiredWithDefault extends Task {
@PluginProperty
@NotNull
@Builder.Default
private Property<TaskWithEnum.TestClass> requiredWithDefault = Property.of(TaskWithEnum.TestClass.builder().testProperty("test").build());

@PluginProperty
@NotNull
private Property<TaskWithEnum.TestClass> requiredWithNoDefault;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kestra.jdbc.runner;

import com.google.common.collect.ImmutableMap;
import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.core.models.conditions.ConditionContext;
import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.executions.TaskRun;
Expand All @@ -11,14 +12,7 @@
import io.kestra.core.queues.QueueFactoryInterface;
import io.kestra.core.queues.QueueInterface;
import io.kestra.core.repositories.LocalFlowRepositoryLoader;
import io.kestra.core.runners.RunContextFactory;
import io.kestra.core.runners.StandAloneRunner;
import io.kestra.core.runners.Worker;
import io.kestra.core.runners.WorkerJob;
import io.kestra.core.runners.WorkerTask;
import io.kestra.core.runners.WorkerTaskResult;
import io.kestra.core.runners.WorkerTrigger;
import io.kestra.core.runners.WorkerTriggerResult;
import io.kestra.core.runners.*;
import io.kestra.core.services.SkipExecutionService;
import io.kestra.core.tasks.test.SleepTrigger;
import io.kestra.core.utils.Await;
Expand All @@ -28,7 +22,6 @@
import io.kestra.plugin.core.flow.Sleep;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.annotation.Property;
import io.kestra.core.junit.annotations.KestraTest;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -157,7 +150,7 @@ void taskResubmitSkipExecution() throws Exception {
});

workerJobQueue.emit(workerTask);
boolean runningLatchAwait = runningLatch.await(2, TimeUnit.SECONDS);
boolean runningLatchAwait = runningLatch.await(10, TimeUnit.SECONDS);
assertThat(runningLatchAwait, is(true));
worker.shutdown();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ public static Docker from(DockerOptions dockerOptions) {

@Override
public TaskRunnerResult<DockerTaskRunnerDetailResult> run(RunContext runContext, TaskCommands taskCommands, List<String> filesToDownload) throws Exception {
Boolean renderedDelete = runContext.render(delete).as(Boolean.class).orElseThrow();

if (taskCommands.getContainerImage() == null && this.image == null) {
throw new IllegalArgumentException("This task runner needs the `containerImage` property to be set");
}
Expand Down Expand Up @@ -538,7 +540,7 @@ public void onComplete() {
// come to a normal end.
kill();

if (Boolean.TRUE.equals(runContext.render(delete).as(Boolean.class).orElseThrow())) {
if (Boolean.TRUE.equals(renderedDelete)) {
dockerClient.removeContainerCmd(exec.getId()).exec();
if (logger.isTraceEnabled()) {
logger.trace("Container deleted: {}", exec.getId());
Expand Down
Loading