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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,7 @@ public abstract class AbstractJsonSchema<T, B> { | |
public static final String ANNOTATION_REQUIRED = "io.fabric8.generator.annotation.Required"; | ||
public static final String ANNOTATION_NOT_NULL = "javax.validation.constraints.NotNull"; | ||
public static final String ANNOTATION_SCHEMA_FROM = "io.fabric8.crd.generator.annotation.SchemaFrom"; | ||
public static final String ANNOTATION_PERSERVE_UNKNOWN_FIELDS = "io.fabric8.crd.generator.annotation.PreserveUnknownFields"; | ||
public static final String ANNOTATION_SCHEMA_SWAP = "io.fabric8.crd.generator.annotation.SchemaSwap"; | ||
|
||
public static final String JSON_NODE_TYPE = "com.fasterxml.jackson.databind.JsonNode"; | ||
|
@@ -123,21 +124,25 @@ protected static class SchemaPropsOptions { | |
final boolean nullable; | ||
final boolean required; | ||
|
||
final boolean preserveUnknownFields; | ||
|
||
SchemaPropsOptions() { | ||
min = Optional.empty(); | ||
max = Optional.empty(); | ||
pattern = Optional.empty(); | ||
nullable = false; | ||
required = false; | ||
preserveUnknownFields = false; | ||
} | ||
|
||
public SchemaPropsOptions(Optional<Double> min, Optional<Double> max, Optional<String> pattern, | ||
boolean nullable, boolean required) { | ||
boolean nullable, boolean required, boolean preserveUnknownFields) { | ||
this.min = min; | ||
this.max = max; | ||
this.pattern = pattern; | ||
this.nullable = nullable; | ||
this.required = required; | ||
this.preserveUnknownFields = preserveUnknownFields; | ||
} | ||
|
||
public Optional<Double> getMin() { | ||
|
@@ -159,6 +164,10 @@ public boolean isNullable() { | |
public boolean getRequired() { | ||
return nullable; | ||
} | ||
|
||
public boolean isPreserveUnknownFields() { | ||
return preserveUnknownFields; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -321,7 +330,8 @@ private T internalFromImpl(TypeDef definition, Set<String> visited, List<Interna | |
facade.max, | ||
facade.pattern, | ||
facade.nullable, | ||
facade.required); | ||
facade.required, | ||
facade.preserveSelfUnknownFields); | ||
|
||
addProperty(possiblyRenamedProperty, builder, possiblyUpdatedSchema, options); | ||
} | ||
|
@@ -352,6 +362,7 @@ private static class PropertyOrAccessor { | |
private boolean required; | ||
private boolean ignored; | ||
private boolean preserveUnknownFields; | ||
private boolean preserveSelfUnknownFields; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need an extra field here? |
||
private String description; | ||
private TypeRef schemaFrom; | ||
|
||
|
@@ -416,6 +427,9 @@ public void process() { | |
case ANNOTATION_JSON_ANY_SETTER: | ||
preserveUnknownFields = true; | ||
break; | ||
case ANNOTATION_PERSERVE_UNKNOWN_FIELDS: | ||
preserveSelfUnknownFields = true; | ||
break; | ||
case ANNOTATION_SCHEMA_FROM: | ||
schemaFrom = extractClassRef(a.getParameters().get("type")); | ||
break; | ||
|
@@ -455,6 +469,10 @@ public boolean isPreserveUnknownFields() { | |
return preserveUnknownFields; | ||
} | ||
|
||
public boolean isPreserveSelfUnknownFields() { | ||
return preserveSelfUnknownFields; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
@@ -494,6 +512,7 @@ private static class PropertyFacade { | |
private boolean required; | ||
private boolean ignored; | ||
private boolean preserveUnknownFields; | ||
private boolean preserveSelfUnknownFields; | ||
private final Property original; | ||
private String nameContributedBy; | ||
private String descriptionContributedBy; | ||
|
@@ -583,6 +602,10 @@ public Property process() { | |
preserveUnknownFields = true; | ||
} | ||
|
||
if (p.isPreserveSelfUnknownFields()) { | ||
preserveSelfUnknownFields = true; | ||
} | ||
|
||
if (p.contributeSchemaFrom()) { | ||
schemaFrom = p.getSchemaFrom(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.crd.generator.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/* | ||
* Used to tweak the behavior of the crd-generator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. something like |
||
*/ | ||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface PreserveUnknownFields { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you, please, add the annotation to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also s/annotation/annotate/ ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
sorry I can't find where it is, can I ask for help😂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please replace
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
sorry😂……fixed |
||
|
||
```java | ||
interface ExampleInterface {} | ||
|
||
public class ExampleSpec { | ||
@PreserveUnknownFields | ||
ExampleInterface someValue; | ||
} | ||
``` | ||
|
||
will be generated as: | ||
|
||
```yaml | ||
spec: | ||
properties: | ||
someValue: | ||
type: object | ||
x-kubernetes-preserve-unknown-fields: true | ||
type: object | ||
``` | ||
|
||
## Features cheatsheet | ||
|
||
| Annotation | Description | | ||
|
There was a problem hiding this comment.
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