Skip to content

Commit

Permalink
fix(core): properly handle select input with expression (#5035) (#5036)
Browse files Browse the repository at this point in the history
Fix: #5035
  • Loading branch information
fhussonnois authored Sep 23, 2024
1 parent 242d2c4 commit 3df5a7f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.experimental.SuperBuilder;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

Expand Down Expand Up @@ -104,7 +105,6 @@ public Input<?> render(final Function<String, Object> renderer) {
return this;
}

@SuppressWarnings("unchecked")
private List<String> renderExpressionValues(final Function<String, Object> renderer) {
Object result;
try {
Expand All @@ -120,8 +120,9 @@ private List<String> renderExpressionValues(final Function<String, Object> rende
}

if (result instanceof List<?> list) {
return (List<String>) list;
return list.stream().filter(Objects::nonNull).map(Object::toString).toList();
}

String type = Optional.ofNullable(result).map(Object::getClass).map(Class::getSimpleName).orElse("<null>");
throw ManualConstraintViolation.toConstraintViolationException(
"Invalid expression result. Expected a list of strings, but received " + type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.experimental.SuperBuilder;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

Expand Down Expand Up @@ -74,7 +75,6 @@ public Input<?> render(final Function<String, Object> renderer) {
return this;
}

@SuppressWarnings("unchecked")
private List<String> renderExpressionValues(final Function<String, Object> renderer) {
Object result;
try {
Expand All @@ -90,8 +90,9 @@ private List<String> renderExpressionValues(final Function<String, Object> rende
}

if (result instanceof List<?> list) {
return (List<String>) list;
return list.stream().filter(Objects::nonNull).map(Object::toString).toList();
}

String type = Optional.ofNullable(result).map(Object::getClass).map(Class::getSimpleName).orElse("<null>");
throw ManualConstraintViolation.toConstraintViolationException(
"Invalid expression result. Expected a list of strings, but received " + type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MultiselectInputTest {
RunContextFactory runContextFactory;

@Test
void shouldRenderInputGivenExpression() {
void shouldRenderInputGivenExpressionReturningStrings() {
// Given
RunContext runContext = runContextFactory.of(Map.of("values", List.of("V1", "V2")));
MultiselectInput input = MultiselectInput
Expand All @@ -39,4 +39,25 @@ void shouldRenderInputGivenExpression() {
// Then
Assertions.assertEquals(((MultiselectInput)renderInput).getValues(), List.of("V1", "V2"));
}

@Test
void shouldRenderInputGivenExpressionReturningIntegers() {
// Given
RunContext runContext = runContextFactory.of(Map.of("values", List.of(1, 2)));
MultiselectInput input = MultiselectInput
.builder()
.id("id")
.expression("{{ values }}")
.build();
// When
Input<?> renderInput = RenderableInput.mayRenderInput(input, s -> {
try {
return runContext.renderTyped(s);
} catch (IllegalVariableEvaluationException e) {
throw new RuntimeException(e);
}
});
// Then
Assertions.assertEquals(((MultiselectInput)renderInput).getValues(), List.of("1", "2"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SelectInputTest {
RunContextFactory runContextFactory;

@Test
void shouldRenderInputGivenExpression() {
void shouldRenderInputGivenExpressionReturningStrings() {
// Given
RunContext runContext = runContextFactory.of(Map.of("values", List.of("V1", "V2")));
SelectInput input = SelectInput
Expand All @@ -39,4 +39,25 @@ void shouldRenderInputGivenExpression() {
// Then
Assertions.assertEquals(((SelectInput)renderInput).getValues(), List.of("V1", "V2"));
}

@Test
void shouldRenderInputGivenExpressionReturningIntegers() {
// Given
RunContext runContext = runContextFactory.of(Map.of("values", List.of(1, 2)));
SelectInput input = SelectInput
.builder()
.id("id")
.expression("{{ values }}")
.build();
// When
Input<?> renderInput = RenderableInput.mayRenderInput(input, s -> {
try {
return runContext.renderTyped(s);
} catch (IllegalVariableEvaluationException e) {
throw new RuntimeException(e);
}
});
// Then
Assertions.assertEquals(((SelectInput)renderInput).getValues(), List.of("1", "2"));
}
}

0 comments on commit 3df5a7f

Please sign in to comment.