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

[Enhancement] CrdGenerator validate @JsonPropertyOrder #2779

Closed
tombentley opened this issue Apr 2, 2020 · 12 comments · Fixed by #9831
Closed

[Enhancement] CrdGenerator validate @JsonPropertyOrder #2779

tombentley opened this issue Apr 2, 2020 · 12 comments · Fixed by #9831

Comments

@tombentley
Copy link
Member

Can we improve the CrdGenerator to:

  1. validate that the @JsonPropertyOrder is present
  2. validate that the properties given are all properties of the class
  3. validate that no properties of the class (including inherited properties) are missing
@samuel-hawker
Copy link
Contributor

@tombentley this is a great idea.

A question of implementation, would you want this to fail a build if any of the above validations fail, or print them out as warnings?
I am guessing the prior, but just thought it might be useful to clarify in this issue.

@tombentley
Copy link
Member Author

Fail the build. People usually overlook warnings.

@samuel-hawker samuel-hawker self-assigned this Apr 10, 2020
samuel-hawker added a commit to samuel-hawker/strimzi-kafka-operator that referenced this issue Apr 10, 2020
Stage 1 of Issue 2779
Validate @JsonPropertyOrder present for all CRD classes
and subclasses with exception of Java primitive `Object`

Added test class to validate this new functionality and
added Exception: InvalidCrdException

Followup PRs will see the check that JsonPropertyOrder
contains exactly all of the classes fields and these fields
will be ordered for each individual class

Contributes to: strimzi#2779

Signed-off-by: Samuel Hawker <samuel.hawker@ibm.com>
samuel-hawker added a commit to samuel-hawker/strimzi-kafka-operator that referenced this issue May 5, 2020
Stage 1 of Issue 2779
Validate @JsonPropertyOrder present for all CRD classes
and subclasses with exception of Java primitive `Object`

Added test class to validate this new functionality and
added Exception: InvalidCrdException

Followup PRs will see the check that JsonPropertyOrder
contains exactly all of the classes fields and these fields
will be ordered for each individual class

Contributes to: strimzi#2779

Signed-off-by: Samuel Hawker <samuel.hawker@ibm.com>
@samuel-hawker samuel-hawker removed their assignment Aug 7, 2020
@samuel-hawker
Copy link
Contributor

I haven't had time to finish this.
The core new logic for validating the order property is present was added here:
https://github.com/strimzi/strimzi-kafka-operator/pull/2820/files#diff-52292ed5de154b1a03087dc5c1d64bceR447-R458
However in a larger test restructure I got pretty lost and a lot of the tests no longer work due to the complications that errors collect in an array and many pre-existing tests actually print out many errors, meaning it fairly difficult or unreliable to test.

If someone wants to pick this up, feel free to look at my above closed PR #2820

@scholzj
Copy link
Member

scholzj commented Mar 23, 2021

Where does the property order actually matter? When working on the api-conversion, it in theory should be used. But in practice, for example when using kubectl, it anyway reorders them. So the question is ... does this really matter?

@scholzj
Copy link
Member

scholzj commented Jun 7, 2022

Triaged on 7.6.2022: We should either validate it or remove it completely. But right now it is not clear what is it actually useful for.

@tombentley why do you think we should have this and define the order?

@tombentley
Copy link
Member Author

It mostly doesn't matter, because whatever order we use it's only honoured when it's us serializing the JSON. That's why kubectl reorders them. But for things like our tests it allows us to compare strings, which can be useful. It is also be useful for any tooling we (or others, since this is part of the api module) might write that displays YAMLs (e.g. strimzictl). I think this is worth keeping.

@scholzj
Copy link
Member

scholzj commented Jun 21, 2022

Triaged on 21st June: We should validate that all fields are present at build time.

@steffen-karlsson
Copy link
Contributor

Hello @scholzj

Is this still relevant? If so, I'd like to give it a go.
If possible can you list expected outcome after the solution is successfully implemented?
Is it still expected to be implemented in the CrdGenerator?

@scholzj
Copy link
Member

scholzj commented Mar 12, 2024

I think the expectation is that it will check if:

  • The JsonPropertyOrder annotation is set
  • If it has all the relevant fields (not sure how easy or hard the definition of relevant field is -> we want those generated into the CRD spec to be included. But not the additionalProperties, constants etc.)

If the annotation or some fields is missing, it should raise the error and fail. The PR adding this feature should also update any of the missing fields so that we can merge it without failing the main build.

And then once it is merged, when new field is added tot he API but not to the property order or when a new class is added without the property order, it would simply fail the build and force the user to fix it.

@steffen-karlsson
Copy link
Contributor

steffen-karlsson commented Mar 13, 2024

Sounds good @scholzj.

Just to clarify, when you say all the relevant fields, you mean the ones represented in JsonPropertyOrder?
And they all need to have a value and default is acceptable as well?

Secondly, how can we identify the classes requiring JsonPropertyOrder? Is it just all in io.strimzi.api.kafka.model or all being a property of a class inheriting from CustomResource?

Ill take a look at this one :)

@scholzj
Copy link
Member

scholzj commented Mar 13, 2024

No, what the check should do is:

  • Get all the fields from the class that will be added to the custom resource definition (I think it identifies them through the get methods)
  • Validate that all of them are listed in the JsonPropertyOrder annotation

it would apply to all the POJO classes in the api module basically. The whole https://github.com/strimzi/strimzi-kafka-operator/tree/main/api/src/main/java/io/strimzi/api/kafka/model tree. But as you would validate it in the CRD Generator, it basically goes through these classes from the Custom Resource implementations, so you do not need to browse the tree, you can just follow the classes as processed in the CRD Generator.

@steffen-karlsson
Copy link
Contributor

steffen-karlsson commented Mar 13, 2024

Makes sense, thanks @scholzj :)

This is already what I did, so makes sense.
All left is to do the comparison of the fields with the JsonPropertyOrder and fix all the classes.

@scholzj scholzj linked a pull request Mar 14, 2024 that will close this issue
8 tasks
scholzj pushed a commit that referenced this issue Mar 14, 2024
Signed-off-by: Steffen Karlsson <steffen.karlsson@maersk.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants