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

OPA 0.53: object.union_n would change the nested objects of the objects passed in #5975

Closed
qshu-splunk opened this issue Jun 5, 2023 · 3 comments
Labels

Comments

@qshu-splunk
Copy link
Contributor

Short description

Based on the documentation which does not mention any side effect, I think when object.union_n is used to merge an array of objects, it should not change any of the objects being passed into the function, including the nested objects. The original objects should stay the same after calling the function.

However, the current implementation of object.union_n would cause the original objects being passed in to change, which is not mentioned in the doc. I do think this is a bug with object.union_n.

If this is intended, please document how the original objects should be changed and any side effects it would cause.

Steps To Reproduce

  1. Go to the Rego playground
  2. Paste the code below:
a := {"spec": {"key_a": "Should belong to A ONLY"}}
b := {"spec": {"key_b": "Should belong to B ONLY"}}

p := p {
	p := object.union_n([a, b])
}
  1. You can see the output is as below:
{
    "a": {
        "spec": {
            "key_a": "Should belong to A ONLY"
        }
    },
    "b": {
        "spec": {
            "key_a": "Should belong to A ONLY",
            "key_b": "Should belong to B ONLY"
        }
    },
    "p": {
        "spec": {
            "key_a": "Should belong to A ONLY",
            "key_b": "Should belong to B ONLY"
        }
    }
}

object b's nested object spec now contains "key_a": "Should belong to A ONLY" which means that object b has been modified

Expected behavior

object b should stay as {"spec": {"key_b": "Should belong to B ONLY"}} after object.union_n

Additional context

@qshu-splunk qshu-splunk added the bug label Jun 5, 2023
@ashutosh-narkar
Copy link
Member

Looks like the last object that's passed is modified. This is where the builtin implementation is if you'd like to take a look and contribute a fix.

@qshu-splunk
Copy link
Contributor Author

@ashutosh-narkar I have created a PR to address the bug:#5976

@ashutosh-narkar
Copy link
Member

Fixed in #5976.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants