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

Don't overwrite local multi-instance variables #9947

Merged

Conversation

korthout
Copy link
Member

@korthout korthout commented Aug 1, 2022

Description

This PR makes it possible to refer to the same variable in the multi-instance inputElement and the outputElement expression.

Technically, this PR makes sure that the 'NIL initialization' (explained below) of the outputElement variable doesn't overwrite:

  • the inputElement variable
  • the loopCounter variable

When the outputElement expression (of a multi-instance element) refers directly to a variable (or the property of a variable), we NIL initialize the referenced variable. This is to make sure the outputElement exists on the local scope of the inner instance of a multi-instance body. However, when the outputElement expression refers to the same variable as the inputElement, then we would overwrite the locally scoped inputElement variable with this NIL value. Effectively erasing the inputElement variable's value.

I wondered whether we could also overwrite other locally scoped variables. However, there aren't many local variables that can exist on the inner instance of a multi-instance body (see below). User-defined variables (e.g. input mappings) cannot reach this scope during the activation of the inner instance. So apart from the inputElement variables, only the loopCounter variable can be NIL initialized by the outputElement expression.

  • inputElement variable
  • outputElement expression that refers to variable (or property of a variable)
  • loopCounter variable

Related issues

closes #4687

Definition of Done

Not all items need to be done depending on the issue and the pull request.

Code changes:

  • The changes are backwards compatibility with previous versions
  • If it fixes a bug then PRs are created to backport the fix to the last two minor versions. You can trigger a backport by assigning labels (e.g. backport stable/1.3) to the PR, in case that fails you need to create backports manually.

Testing:

  • There are unit/integration tests that verify all acceptance criterias of the issue
  • New tests are written to ensure backwards compatibility with further versions
  • The behavior is tested manually
  • The change has been verified by a QA run
  • The impact of the changes is verified by a benchmark

Documentation:

  • The documentation is updated (e.g. BPMN reference, configuration, examples, get-started guides, etc.)
  • New content is added to the release announcement
  • If the PR changes how BPMN processes are validated (e.g. support new BPMN element) then the Camunda modeling team should be informed to adjust the BPMN linting.

Please refer to our review guidelines.

When the output element expression is a variable (or a nested property
of a variable) then it needs to be nil initialized, to make sure that
the variable exists at the local scope.

However, when the output element expression refers to the same variable
as the input element, then it shouldn't overwrite this variable. Note
that the input variable is already created at the local scope.

This adds a test case that fails if the output element variable is nil
initialized when it refers to the same variable as the input element.
When the output element expression is a variable (or a nested property
of a variable) then it needs to be nil initialized, to make sure that
the variable exists at the local scope.

However, when the output element expression refers to the same variable
as the input element, then it shouldn't overwrite this variable. Note
that the input variable is already created at the local scope.

We can simply filter out this case, and only nil init the other output
element variables.
The zeebe extension property Output Element must be an expression. The
method only exists to allow a test case that rejects a process when the
output element is a static value.
When the output element expression is a variable (or a nested property
of a variable) then it needs to be nil initialized, to make sure that
the variable exists at the local scope.

However, when the output element expression refers to the loopCounter
variable, then it shouldn't overwrite this variable. Note
that the loopCounter is already created at the local scope.

This adds a test case that fails if the output element variable is nil
initialized when it refers to the loopCounter variable.
Similar to the scenario of the input element variable, if the output
element expressions refers to the `loopCounter` variable, then we
shouldn't NIL initialize it.
@github-actions
Copy link
Contributor

github-actions bot commented Aug 1, 2022

Test Results

   799 files  ±    0         1 errors  798 suites  ±0   1h 38m 56s ⏱️ + 6m 36s
6 116 tests  - 112  6 107 ✔️  - 112  9 💤 ±0  0 ±0 
6 302 runs   - 112  6 293 ✔️  - 112  9 💤 ±0  0 ±0 

For more details on these parsing errors, see this check.

Results for commit 64a4309. ± Comparison against base commit d678d46.

♻️ This comment has been updated with latest results.

@korthout korthout changed the title Don't overwrite local multi-instance input element variable Don't overwrite local multi-instance variables Aug 1, 2022
@korthout korthout marked this pull request as ready for review August 2, 2022 09:00
Copy link
Contributor

@remcowesterhoud remcowesterhoud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Great! Thanks @korthout

@@ -894,6 +894,57 @@ public void shouldSetOutputElementVariable() {
OUTPUT_COLLECTION.stream().map(JsonUtil::toJson).collect(Collectors.toList()));
}

@Test
public void shouldNotInitializeOutputElementVariableIfSameNameAsInputElement() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Could this problem also occur for an output collection? If so please also add a testcase for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question.

This was somewhat discussed in another issue. To summarize, the input collection variable exists at a higher scope, the output collection variable is created at the multi-instance element's scope. So the output collection variable can shadow the input collection variable.

What that analysis doesn't discuss is the case combined with an input mapping to set the input collection variable at the multi-instance element scope. However, that shouldn't work because the input mappings are only applied for each inner activity, not for the entire multi-instance element. As far as I know, we consider it not a valid use case at this time.

@korthout
Copy link
Member Author

korthout commented Aug 2, 2022

bors merge

@zeebe-bors-camunda
Copy link
Contributor

Build succeeded:

@zeebe-bors-camunda zeebe-bors-camunda bot merged commit c47121a into main Aug 2, 2022
@zeebe-bors-camunda zeebe-bors-camunda bot deleted the korthout-4687-same-input-output-variable-name branch August 2, 2022 15:05
@backport-action
Copy link
Collaborator

Successfully created backport PR #9957 for stable/1.3.

@backport-action
Copy link
Collaborator

Successfully created backport PR #9958 for stable/8.0.

zeebe-bors-camunda bot added a commit that referenced this pull request Aug 2, 2022
9957: [Backport stable/1.3] Don't overwrite local multi-instance variables r=korthout a=backport-action

# Description
Backport of #9947 to `stable/1.3`.

relates to #4687

Co-authored-by: Nico Korthout <nico.korthout@camunda.com>
zeebe-bors-camunda bot added a commit that referenced this pull request Aug 2, 2022
9958: [Backport stable/8.0] Don't overwrite local multi-instance variables r=korthout a=backport-action

# Description
Backport of #9947 to `stable/8.0`.

relates to #4687

Co-authored-by: Nico Korthout <nico.korthout@camunda.com>
@saig0 saig0 added release/8.0.8 version:1.3.14 Marks an issue as being completely or in parts released in 1.3.14 labels Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
version:1.3.14 Marks an issue as being completely or in parts released in 1.3.14
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to use the same input/output element in multi-instance
4 participants