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

ast: preserve jsonOptions in NewAnnotationsRef #5753

Merged
merged 1 commit into from
Mar 14, 2023

Conversation

zregvart
Copy link
Contributor

Makes sure that jsonOptions are preserved in NewAnnotationsRef.

simonbaird
simonbaird previously approved these changes Mar 13, 2023
@charlieegan3
Copy link
Contributor

Thanks for this @zregvart! This is makes sense to me. I'm interested to know how you spotted this and if it caused any issues for your use case?

@anderseknert
Copy link
Member

Seconded! Also, could a test be added to assert desired functionality here?

@zregvart
Copy link
Contributor Author

We render our documentation using Antora, for which we built an extension that relies on opa-inspect-js Nodejs package to provide the Location in the AnnotationRef to generate the source links that you can see here.
The tests on the pull request from dependabot to upgrade to OPA 50.0 failed, so I investigated and found that when NewAnnotationsRef is invoked from Flatten options provided to ParseModuleWithOpts were not retained on AnnotationsRef. We take the output from Flatten and convert it to JavaScript objects by means of serializing to JSON first, so we need the JSONOptions to be preserved for the location to be present in the JSON.

I thought about creating a test for this change, the challenge with that is that it would be of little value as it would test only this specific scenario. What I think would help in terms of testing is to create a property based test NewAnnotationsRef that asserts that all fields are copied over from Annotations to AnnotationsRef with the Location being copied from the node. Such test would test the contract of the method with arbitrary inputs and would catch future regressions of that contract. For this a dependency like leanovate/gopter would need to be introduced. What do you think, would you like to proceed with this?

@charlieegan3
Copy link
Contributor

Thanks for the notes @zregvart, really interesting to see how you're using the tooling to create rich documentation - it's great!

Regarding the testing, I think you're right in that ideally we'd cover more than this specific case. However, since OPA is often included in applications as a dependency, we generally try to keep the dependencies we have in OPA to a minimum. I wonder if there's a compromise that tests more than this single case, but doesn't need a property based testing dependency? Perhaps using NewAnnotationsRef and marshalling the result to JSON might be suitable?

Copy link
Contributor

@charlieegan3 charlieegan3 left a comment

Choose a reason for hiding this comment

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

Thanks for adding this test case. I think this is good and covers the case we'd like to avoid being an issue in future.

I've suggested two small changes, hope they make sense. Sorry about the testify situation. It's come up before!

@@ -1095,6 +1097,319 @@ func TestAnnotations_toObject(t *testing.T) {
}
}

func TestNewAnnotationsSet_WithOptions(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure about this test name, how about TestNewAnnotationsRef_JSONOptions?

Also, I think that this test might be best located in ast/marshal_test.go. We added some pretty similar tests in there and I think it'd make sense for this to be located with them.


asJSON := toJSON(ref)

assert.JSONEq(t, tc.expected[i], asJSON, "serialized AnnotationsRef differs, expected:\n%s\n got\n%s\n", tc.expected[i], asJSON)
Copy link
Contributor

Choose a reason for hiding this comment

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

OPA's depending on github.com/stretchr/testify is, unfortunately, rather misleading. I will try and explain why we don't use it for such assertions.

The package is in our go.mod since we vendored and altered gojsonschema. This code came with some tests which used testify. This means that the only uses of testify are in this internal package. Ideally these tests wouldn't introduce additional dependencies to OPA.

Sorry that this likely isn't at all clear. I've added it to my todos as I'd like to not mislead future contributors in this way.

In our tests, we generally compare the JSON as strings, e.g. ast/marshal_test.go. Do you think you'd be able to do that here too?

Makes sure that `jsonOptions` are preserved in NewAnnotationsRef.

Signed-off-by: Zoran Regvart <zoran@regvart.com>
@zregvart
Copy link
Contributor Author

@charlieegan3 fancy having another look?

Copy link
Contributor

@charlieegan3 charlieegan3 left a comment

Choose a reason for hiding this comment

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

Thanks @zregvart, I think this looks good now 🙂 thanks for making these adjustments so quickly.

@charlieegan3 charlieegan3 changed the title fix: preserve jsonOptions in NewAnnotationsRef ast: preserve jsonOptions in NewAnnotationsRef Mar 14, 2023
@charlieegan3 charlieegan3 merged commit a33e0e0 into open-policy-agent:main Mar 14, 2023
charlieegan3 added a commit that referenced this pull request Mar 16, 2023
We have testify in our go.mod. This sometimes misleads contributors (including myself!) to think that we can use testify in test code. Since testify is a common testing package, many go developers default to using it.

I'm not sure if we want to merge this as we might rather leave these vendored packages untouched, but it was 10 minutes of work and I was interested to see if we can avoid issues like this in future:

* #5753 (comment)
* #5447 (comment)
* #3952 (review)

Signed-off-by: Charlie Egan <charlie@styra.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants