Repository: kestra-io/kestra
File: core/src/main/java/io/kestra/plugin/core/execution/SetVariables.java (Java)

Code before:
                  - id: hello
                    type: io.kestra.plugin.core.log.Log
                    message: "{{ vars.message }} {{ vars.name }}\""""
        )
    }
)
public class SetVariables extends Task implements ExecutionUpdatableTask {
    @Schema(title = "The variables")
    @NotNull
    private Property<Map<String, Object>> variables;

    @Schema(title = "Flag specifying whether to overwrite existing variables")
    @NotNull
    @Builder.Default
    private Property<Boolean> overwrite = Property.ofValue(true);

    @Override
    public Execution update(Execution execution, RunContext runContext) throws Exception {
        Map<String, Object> renderedVars =
            runContext.render(this.variables).asMap(String.class, Object.class);

        boolean renderedOverwrite =
            runContext.render(overwrite).as(Boolean.class).orElseThrow();

        Map<String, Object> currentVariables =
            execution.getVariables() == null ? new HashMap<>() : execution.getVariables();

        if (!renderedOverwrite) {
            // check that none of the new variables already exist
            List<String> duplicated = renderedVars.keySet().stream()
                .filter(currentVariables::containsKey)
                .toList();

            if (!duplicated.isEmpty()) {
                throw new IllegalArgumentException(
                    "`overwrite` is set to false and the following variables already exist: " +
                    String.join(",", duplicated)
                );
            }
        }

        return execution.withVariables(
            MapUtils.deepMerge(currentVariables, renderedVars)
        );
    }
}


Review comment (suggestion):
```suggestion
            execution.getVariables() == null ? Collections.emptyMap() : execution.getVariables();
```

Code after:
                  - id: hello
                    type: io.kestra.plugin.core.log.Log
                    message: "{{ vars.message }} {{ vars.name }}\""""
        )
    }
)
public class SetVariables extends Task implements ExecutionUpdatableTask {
    @Schema(title = "The variables")
    @NotNull
    private Property<Map<String, Object>> variables;

    @Schema(title = "Flag specifying whether to overwrite existing variables")
    @NotNull
    @Builder.Default
    private Property<Boolean> overwrite = Property.ofValue(true);

    @Override
    public Execution update(Execution execution, RunContext runContext) throws Exception {
        Map<String, Object> renderedVars = runContext.render(this.variables).asMap(String.class, Object.class);
        boolean renderedOverwrite = runContext.render(overwrite).as(Boolean.class).orElseThrow();

        Map<String, Object> currentVariables =
            execution.getVariables() == null ? Collections.emptyMap() : execution.getVariables();

        if (!renderedOverwrite) {
            // check that none of the new variables already exist
            List<String> duplicated = renderedVars.keySet().stream()
                .filter(currentVariables::containsKey)
                .toList();

            if (!duplicated.isEmpty()) {
                throw new IllegalArgumentException(
                    "`overwrite` is set to false and the following variables already exist: " +
                    String.join(",", duplicated)
                );
            }
        }

        return execution.withVariables(MapUtils.deepMerge(currentVariables, renderedVars));
    }
}