You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ossie.apache.org by "RyutoYoda (via GitHub)" <gi...@apache.org> on 2026/10/12 12:40:46 UTC

[PR] fix(salesforce): decode the HTML escaping on imported formulas [ossie]

RyutoYoda opened a new pull request, #572:
URL: https://github.com/apache/ossie/pull/572

   ## Summary
   
   The Salesforce semantic model API accepts a formula verbatim but returns it
   HTML-escaped, so `'A&B'` comes back as `&#39;A&amp;B&#39;`. Import carried that
   text into Ossie as-is, and exporting it again was rejected by Salesforce with
   `Syntax Error - token recognition error at: '&'`.
   
   Decode the five entities the API emits, `&amp;` last so a formula whose own text
   contains `&lt;` survives the round trip. The decoding is applied where a formula
   enters Ossie:
   
   - a calculated measurement's expression,
   - a calculated dimension's expression, whether it becomes a dataset field or is
     preserved in `custom_extensions`,
   - a relationship criterion whose join side is a formula rather than a column,
     before the relationship is sorted into supported or unsupported, so the text is
     decoded either way. A plain field apiName carries no entities, so decoding
     leaves it untouched.
   
   ## Verification
   
   - `mvn -B clean verify` in `converters/salesforce`: 350 tests, 0 failures, 0 errors, `apache-rat:check` clean.
   - 14 new tests in `SalesforceFormulaEscapingTest`, covering each entity, the
     `&amp;` ordering, a formula with no escaping, and each of the three entry points.
   
   ## Related Issues
   
   No existing issue covers this.
   
   ## Checklist
   
   ### Converters
   - [x] Converter changes include tests
   - [x] Round-trip conversion verified
   
   ### Documentation
   - [x] `converters/salesforce/README.md` describes the decoding
   
   ### Tests
   - [x] All tests pass locally
   - [x] New tests added for the fix
   
   ### Compliance
   - [x] No third-party code added
   - [x] Apache RAT check passes
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ossie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(salesforce): decode the HTML escaping on imported formulas [ossie]

Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre merged PR #572:
URL: https://github.com/apache/ossie/pull/572


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ossie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(salesforce): decode the HTML escaping on imported formulas [ossie]

Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #572:
URL: https://github.com/apache/ossie/pull/572#discussion_r4249924339


##########
converters/salesforce/src/main/java/org/apache/ossie/converter/SalesforceFormulaEscaping.java:
##########
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ossie.converter;
+
+/**
+ * Undoes the HTML escaping Salesforce applies to stored formula text.
+ *
+ * <p>The semantic model API accepts a formula verbatim but returns it escaped, so
+ * {@code 'A&B'} comes back as {@code &#39;A&amp;B&#39;}. Importing that text as-is
+ * produces an Ossie expression Salesforce itself rejects on the next export with
+ * {@code Syntax Error - token recognition error at: '&'}.
+ */
+final class SalesforceFormulaEscaping {
+
+    private SalesforceFormulaEscaping() {}
+
+    /**
+     * Decodes the five entities Salesforce emits. {@code &amp;} is decoded last so a

Review Comment:
   A formula that literally contains `&amp;amp;` can't be told apart from escaped output, so it is decoded once on import. That follows from how Salesforce escapes, so I don't see it as a defect.
   
   Could the Javadoc say so, or could a test pin the behavior?
   
   The `&#39;&amp;lt;&#39;` test covers double-decoding but not this limit.



##########
converters/salesforce/src/test/java/org/apache/ossie/converter/SalesforceFormulaEscapingTest.java:
##########
@@ -0,0 +1,213 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ossie.converter;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Stream;
+import org.apache.ossie.converter.pipeline.PipelineStep;
+import org.apache.ossie.mapper.FileBasedPropertyMapper;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.NullSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+class SalesforceFormulaEscapingTest {
+
+    @ParameterizedTest
+    @MethodSource("escapedFormulas")
+    void decodesTheEntitiesSalesforceEmits(String stored, String original) {
+        assertEquals(original, SalesforceFormulaEscaping.unescape(stored));
+    }
+
+    static Stream<Arguments> escapedFormulas() {
+        return Stream.of(
+                Arguments.of("SUM(IF ([Orders].[Segment] = &#39;SMB&#39;) THEN 1 ELSE 0 END)",
+                        "SUM(IF ([Orders].[Segment] = 'SMB') THEN 1 ELSE 0 END)"),
+                Arguments.of("SUM(IF ([Orders].[Sales] &lt; 100) THEN 1 ELSE 0 END)",
+                        "SUM(IF ([Orders].[Sales] < 100) THEN 1 ELSE 0 END)"),
+                Arguments.of("SUM(IF ([Orders].[Sales] &gt; 100) THEN 1 ELSE 0 END)",
+                        "SUM(IF ([Orders].[Sales] > 100) THEN 1 ELSE 0 END)"),
+                Arguments.of("CONTAINS([Orders].[Name], &quot;Inc&quot;)",
+                        "CONTAINS([Orders].[Name], \"Inc\")"),
+                Arguments.of("CONTAINS([Orders].[Name], &#39;A&amp;B&#39;)",
+                        "CONTAINS([Orders].[Name], 'A&B')"));
+    }
+
+    @Test
+    void decodesAmpersandLastSoEscapedEntityTextSurvives() {
+        assertEquals("'&lt;'", SalesforceFormulaEscaping.unescape("&#39;&amp;lt;&#39;"));
+    }
+
+    @ParameterizedTest
+    @NullSource
+    @ValueSource(strings = {"", "SUM([Orders].[Sales])"})
+    void leavesFormulasWithoutEntitiesUntouched(String formula) {
+        assertEquals(formula, SalesforceFormulaEscaping.unescape(formula));
+    }
+
+    @Test
+    void decodesImportedCalculatedMeasurementExpressions() {
+        Map<String, Object> source = new LinkedHashMap<>(Map.of(
+                "semanticCalculatedMeasurements", mutableList(Map.of(
+                        "apiName", "smb_orders",
+                        "expression", "SUM(IF ([Orders].[Segment] = &#39;A&amp;B&#39;) THEN 1 ELSE 0 END)",
+                        "dataType", "Number"))));
+        Map<String, Object> output = new LinkedHashMap<>();
+
+        handler(new MetricMappingHandler(ConversionDirection.SALESFORCE_TO_OSSIE, extensions()), source, output);
+
+        assertEquals("SUM(IF ([Orders].[Segment] = 'A&B') THEN 1 ELSE 0 END)",
+                tableauExpression(firstOf(output, "metrics")));
+    }
+
+    @Test
+    void decodesImportedCalculatedDimensionExpressions() {
+        Map<String, Object> source = new LinkedHashMap<>(Map.of(
+                "semanticDataObjects", mutableList(Map.of("apiName", "Orders")),
+                "semanticCalculatedDimensions", mutableList(Map.of(
+                        "apiName", "segment_label",
+                        "expression", "IF ([Orders].[Segment] = &#39;A&amp;B&#39;) THEN &#39;yes&#39; ELSE &#39;no&#39; END",
+                        "dataType", "Text",
+                        "dependencies", List.of(Map.of("dependentDefinitionApiName", "Orders"))))));
+        Map<String, Object> output = new LinkedHashMap<>(Map.of(
+                "datasets", mutableList(Map.of("name", "Orders"))));
+
+        handler(new FieldMappingHandler(ConversionDirection.SALESFORCE_TO_OSSIE, extensions()), source, output);
+
+        Map<String, Object> dataset = firstOf(output, "datasets");
+        assertEquals("IF ([Orders].[Segment] = 'A&B') THEN 'yes' ELSE 'no' END",
+                tableauExpression(firstOf(dataset, "fields")));
+    }
+
+    @Test
+    void decodesCalculatedDimensionsPreservedAsCustomExtensions() {
+        // No dependencies, so the dimension cannot become a dataset field and is kept verbatim.
+        Map<String, Object> source = new LinkedHashMap<>(Map.of(
+                "semanticDataObjects", mutableList(Map.of("apiName", "Orders")),
+                "semanticCalculatedDimensions", mutableList(Map.of(
+                        "apiName", "segment_label",
+                        "expression", "IF ([Orders].[Segment] = &#39;A&amp;B&#39;) THEN &#39;yes&#39; ELSE &#39;no&#39; END",
+                        "dataType", "Text"))));
+        Map<String, Object> output = new LinkedHashMap<>(Map.of(
+                "datasets", mutableList(Map.of("name", "Orders"))));
+
+        handler(new FieldMappingHandler(ConversionDirection.SALESFORCE_TO_OSSIE, extensions()), source, output);
+
+        Map<String, Object> preserved = firstOf(source, "semanticCalculatedDimensions");
+        assertEquals("IF ([Orders].[Segment] = 'A&B') THEN 'yes' ELSE 'no' END",
+                preserved.get("expression"));

Review Comment:
   This test is named for what lands in `custom_extensions`, but it asserts on the `source` map. It passes because `FieldMappingHandler` mutates `calcDim` in place, whether or not the dimension is later preserved.
   
   Could it assert on the `custom_extensions` entry in `output`, as the relationship test does with `customExtensionData`? 
   That checks the behavior the name describes.



##########
converters/salesforce/src/main/java/org/apache/ossie/converter/MetricMappingHandler.java:
##########
@@ -191,7 +191,7 @@ private void wrapExpressions(List<Object> sfMetrics, List<Object> ossieMetrics)
             String expressionValue = getString(sfMetric, EXPRESSION);
             if (expressionValue != null) {
                 // Wrap in Ossie dialect structure
-                ossieMetric.put(EXPRESSION, wrapExpression(expressionValue));
+                ossieMetric.put(EXPRESSION, wrapExpression(SalesforceFormulaEscaping.unescape(expressionValue)));

Review Comment:
   This covers calculated measurements that convert. If a measurement can't be converted and is preserved in `custom_extensions`, is its expression decoded too? 
   
   The dimension and relationship paths decode before the supported/unsupported split, so I would like to confirm the metric path matches. A test for the unconverted case would settle it.



##########
converters/salesforce/src/main/java/org/apache/ossie/converter/FieldMappingHandler.java:
##########
@@ -723,6 +723,13 @@ private void processModelLevelCalculatedDimensions(
         for (Object calcDimObj : calcDims) {
             Map<String, Object> calcDim = asMap(calcDimObj);
 
+            // Salesforce returns formulas HTML-escaped; decode before the dimension is
+            // either converted to a field or preserved in custom_extensions.
+            String expressionValue = getString(calcDim, EXPRESSION);
+            if (expressionValue != null) {
+                calcDim.put(EXPRESSION, SalesforceFormulaEscaping.unescape(expressionValue));

Review Comment:
   This overwrites the expression in the caller's input map. It works because that same map is what gets preserved in `custom_extensions`. `MetricMappingHandler` decodes into a new value instead.
   
   Is mutating `source` here intentional, and is a one-line comment worth adding to explain the difference?



##########
converters/salesforce/src/test/java/org/apache/ossie/converter/SalesforceFormulaEscapingTest.java:
##########
@@ -0,0 +1,213 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ossie.converter;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Stream;
+import org.apache.ossie.converter.pipeline.PipelineStep;
+import org.apache.ossie.mapper.FileBasedPropertyMapper;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.NullSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+class SalesforceFormulaEscapingTest {
+
+    @ParameterizedTest
+    @MethodSource("escapedFormulas")
+    void decodesTheEntitiesSalesforceEmits(String stored, String original) {
+        assertEquals(original, SalesforceFormulaEscaping.unescape(stored));
+    }
+
+    static Stream<Arguments> escapedFormulas() {
+        return Stream.of(
+                Arguments.of("SUM(IF ([Orders].[Segment] = &#39;SMB&#39;) THEN 1 ELSE 0 END)",
+                        "SUM(IF ([Orders].[Segment] = 'SMB') THEN 1 ELSE 0 END)"),
+                Arguments.of("SUM(IF ([Orders].[Sales] &lt; 100) THEN 1 ELSE 0 END)",
+                        "SUM(IF ([Orders].[Sales] < 100) THEN 1 ELSE 0 END)"),
+                Arguments.of("SUM(IF ([Orders].[Sales] &gt; 100) THEN 1 ELSE 0 END)",
+                        "SUM(IF ([Orders].[Sales] > 100) THEN 1 ELSE 0 END)"),
+                Arguments.of("CONTAINS([Orders].[Name], &quot;Inc&quot;)",
+                        "CONTAINS([Orders].[Name], \"Inc\")"),
+                Arguments.of("CONTAINS([Orders].[Name], &#39;A&amp;B&#39;)",
+                        "CONTAINS([Orders].[Name], 'A&B')"));
+    }
+
+    @Test
+    void decodesAmpersandLastSoEscapedEntityTextSurvives() {
+        assertEquals("'&lt;'", SalesforceFormulaEscaping.unescape("&#39;&amp;lt;&#39;"));
+    }
+
+    @ParameterizedTest
+    @NullSource
+    @ValueSource(strings = {"", "SUM([Orders].[Sales])"})
+    void leavesFormulasWithoutEntitiesUntouched(String formula) {
+        assertEquals(formula, SalesforceFormulaEscaping.unescape(formula));
+    }
+
+    @Test
+    void decodesImportedCalculatedMeasurementExpressions() {
+        Map<String, Object> source = new LinkedHashMap<>(Map.of(
+                "semanticCalculatedMeasurements", mutableList(Map.of(
+                        "apiName", "smb_orders",
+                        "expression", "SUM(IF ([Orders].[Segment] = &#39;A&amp;B&#39;) THEN 1 ELSE 0 END)",
+                        "dataType", "Number"))));
+        Map<String, Object> output = new LinkedHashMap<>();
+
+        handler(new MetricMappingHandler(ConversionDirection.SALESFORCE_TO_OSSIE, extensions()), source, output);
+
+        assertEquals("SUM(IF ([Orders].[Segment] = 'A&B') THEN 1 ELSE 0 END)",
+                tableauExpression(firstOf(output, "metrics")));
+    }
+
+    @Test
+    void decodesImportedCalculatedDimensionExpressions() {
+        Map<String, Object> source = new LinkedHashMap<>(Map.of(
+                "semanticDataObjects", mutableList(Map.of("apiName", "Orders")),
+                "semanticCalculatedDimensions", mutableList(Map.of(
+                        "apiName", "segment_label",
+                        "expression", "IF ([Orders].[Segment] = &#39;A&amp;B&#39;) THEN &#39;yes&#39; ELSE &#39;no&#39; END",
+                        "dataType", "Text",
+                        "dependencies", List.of(Map.of("dependentDefinitionApiName", "Orders"))))));
+        Map<String, Object> output = new LinkedHashMap<>(Map.of(
+                "datasets", mutableList(Map.of("name", "Orders"))));
+
+        handler(new FieldMappingHandler(ConversionDirection.SALESFORCE_TO_OSSIE, extensions()), source, output);
+
+        Map<String, Object> dataset = firstOf(output, "datasets");
+        assertEquals("IF ([Orders].[Segment] = 'A&B') THEN 'yes' ELSE 'no' END",
+                tableauExpression(firstOf(dataset, "fields")));
+    }
+
+    @Test
+    void decodesCalculatedDimensionsPreservedAsCustomExtensions() {
+        // No dependencies, so the dimension cannot become a dataset field and is kept verbatim.
+        Map<String, Object> source = new LinkedHashMap<>(Map.of(
+                "semanticDataObjects", mutableList(Map.of("apiName", "Orders")),
+                "semanticCalculatedDimensions", mutableList(Map.of(
+                        "apiName", "segment_label",
+                        "expression", "IF ([Orders].[Segment] = &#39;A&amp;B&#39;) THEN &#39;yes&#39; ELSE &#39;no&#39; END",
+                        "dataType", "Text"))));
+        Map<String, Object> output = new LinkedHashMap<>(Map.of(
+                "datasets", mutableList(Map.of("name", "Orders"))));
+
+        handler(new FieldMappingHandler(ConversionDirection.SALESFORCE_TO_OSSIE, extensions()), source, output);
+
+        Map<String, Object> preserved = firstOf(source, "semanticCalculatedDimensions");
+        assertEquals("IF ([Orders].[Segment] = 'A&B') THEN 'yes' ELSE 'no' END",
+                preserved.get("expression"));
+    }
+
+    @Test
+    void decodesFormulaJoinCriteriaPreservedAsCustomExtensions() {
+        // A criterion whose join side is a Formula makes the relationship unsupported, so it
+        // is kept verbatim in custom_extensions rather than becoming from_columns/to_columns.
+        Map<String, Object> source = new LinkedHashMap<>(Map.of(
+                "semanticRelationships", mutableList(Map.of(
+                        "apiName", "Orders_Customers",
+                        "leftSemanticDefinitionApiName", "Orders",
+                        "rightSemanticDefinitionApiName", "Customers",
+                        "criteria", mutableList(Map.of(
+                                "joinOperator", "Equals",
+                                "leftFieldType", "TableField",
+                                "leftSemanticFieldApiName", "CustomerId",
+                                "rightFieldType", "Formula",
+                                "rightSemanticFieldApiName",
+                                "LEFT(&#39;A&amp;B&#39; + [Customers].[Id], 4)"))))));
+        Map<String, Object> output = new LinkedHashMap<>();
+
+        handler(new RelationshipMappingHandler(ConversionDirection.SALESFORCE_TO_OSSIE, extensions()),
+                source, output);
+
+        assertTrue(customExtensionData(output).contains("LEFT('A&B' + [Customers].[Id], 4)"),
+                "formula kept escaped in custom_extensions: " + customExtensionData(output));

Review Comment:
   Nit: the message says "formula kept escaped in custom_extensions", but the assertion checks that the decoded text is present. If this fails, the message points the wrong way.
   
   Maybe "formula should be decoded in custom_extensions: ...".



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@ossie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org