This is an automated email from the ASF dual-hosted git repository. heneveld pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git
commit 7d5c247e82f658bdca39146def7a1348a6725f9a Author: Alex Heneveld <[email protected]> AuthorDate: Tue Jun 20 02:45:39 2023 +0100 comment or fix places that need deeply --- .../core/resolve/jackson/BeanWithTypeUtils.java | 2 ++ .../workflow/WorkflowExpressionResolution.java | 27 +++++++++++----------- .../steps/variables/SetVariableWorkflowStep.java | 16 +++++++++---- .../brooklyn/util/core/flags/TypeCoercions.java | 1 + ...klynRegisteredTypeJacksonSerializationTest.java | 7 ++++-- .../core/workflow/WorkflowTransformTest.java | 4 ++-- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/BeanWithTypeUtils.java b/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/BeanWithTypeUtils.java index a920995cac..91835f23ee 100644 --- a/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/BeanWithTypeUtils.java +++ b/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/BeanWithTypeUtils.java @@ -190,6 +190,8 @@ public class BeanWithTypeUtils { } catch (Exception e) { try { + // needed for a few things, mainly where a bean has a type field that conflicts with the type here, + // tryCoercer 81-wrong-bean uses this return convertDeeply(mgmt, mapOrListToSerializeThenDeserialize, type, allowRegisteredTypes, loader, allowJavaTypes); } catch (Exception e2) { throw Exceptions.propagate(Arrays.asList(e, e2)); diff --git a/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExpressionResolution.java b/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExpressionResolution.java index 5c207c8b6c..a18b78d8bd 100644 --- a/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExpressionResolution.java +++ b/core/src/main/java/org/apache/brooklyn/core/workflow/WorkflowExpressionResolution.java @@ -388,24 +388,25 @@ public class WorkflowExpressionResolution { /** does not use templates */ public <T> T resolveCoercingOnly(Object expression, TypeToken<T> type) { - try { - if (expression==null || (Jsonya.isJsonPrimitiveDeep(expression) && !(expression instanceof Set))) { + if (expression==null) return null; + if (Jsonya.isJsonPrimitiveDeep(expression) && !(expression instanceof Set)) { + try { // only try yaml coercion, as values are normally set from yaml and will be raw at this stage (but not if they are from a DSL) // (might be better to always to TC.coerce) return BeanWithTypeUtils.convert(context.getManagementContext(), expression, type, true, RegisteredTypes.getClassLoadingContext(context.getEntity()), true /* needed for wrapped resolved holders */); - } else { - return TypeCoercions.coerce(expression, type); - } - } catch (Exception e) { - Exceptions.propagateIfFatal(e); - try { - // fallback to simple coercion - return TypeCoercions.coerce(expression, type); - } catch (Exception e2) { - Exceptions.propagateIfFatal(e2); - throw Exceptions.propagate(e); + } catch (Exception e) { + Exceptions.propagateIfFatal(e); + try { + // fallback to simple coercion + return TypeCoercions.coerce(expression, type); + } catch (Exception e2) { + Exceptions.propagateIfFatal(e2); + throw Exceptions.propagate(e); + } } + } else { + return TypeCoercions.coerce(expression, type); } } diff --git a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/SetVariableWorkflowStep.java b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/SetVariableWorkflowStep.java index 2cb1bfd108..fb2d2ebfa1 100644 --- a/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/SetVariableWorkflowStep.java +++ b/core/src/main/java/org/apache/brooklyn/core/workflow/steps/variables/SetVariableWorkflowStep.java @@ -241,6 +241,10 @@ public class SetVariableWorkflowStep extends WorkflowStepDefinition { } Object process(List<String> w) { + return process(w, false); + } + + Object process(List<String> w, boolean ternaryColonAllowed) { // if no tokens, treat as null if (w.isEmpty()) return null; @@ -251,10 +255,12 @@ public class SetVariableWorkflowStep extends WorkflowStepDefinition { // not used: ! ~ - + & ++ -- (i.e. unary operators) // #__: ?: (i.e. ternary) - result = handleTokenIfPresent(w, false, MutableMap.of( - "?", this::handleTernaryCondition, - ":", this::handleTernaryArms - )); + result = handleTokenIfPresent(w, false, + ternaryColonAllowed ? MutableMap.of( + "?", this::handleTernaryCondition, + ":", this::handleTernaryArms) + : MutableMap.of("?", this::handleTernaryCondition) + ); if (result.isPresent()) return result.get(); @@ -512,7 +518,7 @@ public class SetVariableWorkflowStep extends WorkflowStepDefinition { rhs = handleChainedTernaryRhs(rhs0); } else { // non-nested or chained - rhs = process(rhs0); + rhs = process(rhs0, true); } //log.info(String.format("Ternary Condition 1: [lhs:%s][rhs:%s]", lhs, rhs)); diff --git a/core/src/main/java/org/apache/brooklyn/util/core/flags/TypeCoercions.java b/core/src/main/java/org/apache/brooklyn/util/core/flags/TypeCoercions.java index d630819a4a..ed2e66f165 100644 --- a/core/src/main/java/org/apache/brooklyn/util/core/flags/TypeCoercions.java +++ b/core/src/main/java/org/apache/brooklyn/util/core/flags/TypeCoercions.java @@ -390,6 +390,7 @@ public class TypeCoercions { return null; } try { + // if a bean has a type field, we might need to try an explicit conversion to it Maybe<Map> resultMap = BeanWithTypeUtils.tryConvertOrAbsentUsingContext(Maybe.of(input), new TypeToken<Map>() {}, true); if (toMap || resultMap.isAbsentOrNull()) return (Maybe<T>) resultMap; return BeanWithTypeUtils.tryConvertOrAbsentUsingContext(Maybe.cast(resultMap), type); diff --git a/core/src/test/java/org/apache/brooklyn/core/resolve/jackson/BrooklynRegisteredTypeJacksonSerializationTest.java b/core/src/test/java/org/apache/brooklyn/core/resolve/jackson/BrooklynRegisteredTypeJacksonSerializationTest.java index dc9a429904..840371893b 100644 --- a/core/src/test/java/org/apache/brooklyn/core/resolve/jackson/BrooklynRegisteredTypeJacksonSerializationTest.java +++ b/core/src/test/java/org/apache/brooklyn/core/resolve/jackson/BrooklynRegisteredTypeJacksonSerializationTest.java @@ -223,8 +223,11 @@ public class BrooklynRegisteredTypeJacksonSerializationTest extends BrooklynMgmt // we have no choice but to fallback to map deserialization // however we should allow further coercion to Map (in case we read as typed something which should have been a map) // and also coercion that serializes input if complex type then deserializes to intended type, if the intended type has a field 'type' - Map redeserMap = TypeCoercions.coerce(deser(deser), Map.class); - Asserts.assertEquals(redeserMap.get("type"), OtherBean.class.getName()); + + // coercing to a map doesn't mean serializing it, that's too silly +// Map redeserMap = TypeCoercions.coerce(deser(deser), Map.class); +// Asserts.assertEquals(redeserMap.get("type"), OtherBean.class.getName()); + SampleBeanWithType redeserObj = TypeCoercions.coerce(deser(deser), SampleBeanWithType.class); Asserts.assertEquals(redeserObj.x, "hello"); } diff --git a/core/src/test/java/org/apache/brooklyn/core/workflow/WorkflowTransformTest.java b/core/src/test/java/org/apache/brooklyn/core/workflow/WorkflowTransformTest.java index 9f90a6dfcd..9d102cf6f6 100644 --- a/core/src/test/java/org/apache/brooklyn/core/workflow/WorkflowTransformTest.java +++ b/core/src/test/java/org/apache/brooklyn/core/workflow/WorkflowTransformTest.java @@ -125,9 +125,9 @@ public class WorkflowTransformTest extends BrooklynMgmtUnitTestSupport { public void testMapDirect() { Asserts.assertEquals(runWorkflowSteps( "let map myMap = {a: 1}", - "let myMap.a = 2", + "let myMap.a = ${myMap.a} + 2", "return ${myMap.a}"), - "2"); + 3); } @Test
