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

feat: add annotation @PreserveUnknownFields for field #4398

Merged
merged 4 commits into from Sep 16, 2022

Conversation

wineway
Copy link
Contributor

@wineway wineway commented Sep 7, 2022

Signed-off-by: wineway wangyuweihx@gmail.com

Description

like #4313, we need to use a sealed interface as a field type and offer multi implementations for this interface, it seems we can't and don't need to validate its fields in this situation, and we don't want to add something like @JsonAnyGetter might change its serialize behavior. so we think we can add a new annotation @PreserveUnknownFields for this scenario.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Checklist

  • Code contributed by me aligns with current project license: Apache 2.0
  • I Added CHANGELOG entry regarding this change
  • I have implemented unit tests to cover my changes
  • I have added/updated the javadocs and other documentation accordingly
  • No new bugs, code smells, etc. in SonarCloud report
  • I tested my code in Kubernetes
  • I tested my code in OpenShift

Signed-off-by: wineway <wangyuweihx@gmail.com>
@wineway wineway changed the title add annotation @PreserveUnknownFields for field feat: add annotation @PreserveUnknownFields for field Sep 7, 2022
@andreaTP
Copy link
Member

andreaTP commented Sep 7, 2022

As much as I'm unhappy adding yet another way to emit x-kubernetes-preserve-unknown-fields, I do understand the reasoning and I don't want to oppose it.

Just one more question: have you considered extending JsonNode instead of adding AnyGetter or similar?

@andreaTP
Copy link
Member

andreaTP commented Sep 7, 2022

side note: you can run mvn spotless:apply to get the formatting right.

Signed-off-by: wineway <wangyuweihx@gmail.com>
@wineway
Copy link
Contributor Author

wineway commented Sep 7, 2022

Just one more question: have you considered extending JsonNode instead of adding AnyGetter or similar?

Hmmm. I think this could be just a not so good workaround, it destroyed the inheritance and did not work for the sealed interface.

https://github.com/fabric8io/kubernetes-client/blob/master/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java#L266

    boolean preserveUnknownFields = (definition.getFullyQualifiedName() != null &&
        definition.getFullyQualifiedName().equals(JSON_NODE_TYPE));

More than this, it seems that it won't check if the type of the field extends JsonNode, it just checks field type name is equal to JsonNode

@andreaTP
Copy link
Member

andreaTP commented Sep 7, 2022

@wineway thanks for the argument, you can use SchemaFrom to get JsonNode type for the annotation processor purposes ... but I understand this workaround might not be good enough for some use-cases

Copy link
Member

@andreaTP andreaTP left a comment

Choose a reason for hiding this comment

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

A few minor comments and one question, have you attempted to refactor the code such as we can setXKubernetesPreserveUnknownFields in only one place instead of two in the JsonSchema classes?

Thanks a lot!

CHANGELOG.md Outdated
@@ -11,6 +11,7 @@
* Fix #4383: bump snakeyaml from 1.30 to 1.31

#### New Features
* Feat: add annotation @PreserveUnknownFields for generation field
Copy link
Member

Choose a reason for hiding this comment

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

can you please expand a little this sentence, for example saying that it affects the crd-generator

@@ -352,6 +362,7 @@ private static class PropertyOrAccessor {
private boolean required;
private boolean ignored;
private boolean preserveUnknownFields;
private boolean preserveSelfUnknownFields;
Copy link
Member

Choose a reason for hiding this comment

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

Do we need an extra field here?
Is it possible to rely on the already existing preserveUnknownFields ?

import java.lang.annotation.*;

/*
* Used to tweak the behavior of the crd-generator
Copy link
Member

Choose a reason for hiding this comment

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

something like to emit 'x-kubernetes-preserve-unknown-fields'

@@ -357,6 +357,28 @@ Corresponding `x-kubernetes-preserve-unknown-fields: true` will be generated in
x-kubernetes-preserve-unknown-fields: true
```

You can also annotation a field with @PreserveUnknownFields:
Copy link
Member

Choose a reason for hiding this comment

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

Can you, please, add the annotation to the cheatsheet below.

@@ -357,6 +357,28 @@ Corresponding `x-kubernetes-preserve-unknown-fields: true` will be generated in
x-kubernetes-preserve-unknown-fields: true
```

You can also annotation a field with @PreserveUnknownFields:
Copy link
Contributor

Choose a reason for hiding this comment

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

Also s/annotation/annotate/ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also s/annotation/annotate/ ?

sorry I can't find where it is, can I ask for help😂

Copy link
Member

Choose a reason for hiding this comment

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

Please replace annotation with annotate so that the sentence reads:

You can also annotate a field with @PreserveUnknownFields:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please replace annotation with annotate so that the sentence reads:

sorry😂……fixed

@wineway
Copy link
Contributor Author

wineway commented Sep 14, 2022

have you attempted to refactor the code such as we can setXKubernetesPreserveUnknownFields in only one place instead of two in the JsonSchema classes?

I have tried, but I'm afraid I can't refactor the two fields to one in limited code change. if I just separate facade.process to two-stage, or make the type of preserveUnknownFields from boolean to enum, Hmmm, I think adding a new field will be more clear.

Signed-off-by: wineway <wangyuweihx@gmail.com>
@sonarcloud
Copy link

sonarcloud bot commented Sep 15, 2022

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

85.7% 85.7% Coverage
12.7% 12.7% Duplication

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

Successfully merging this pull request may close these issues.

None yet

4 participants