Skip to content

Commit 7a2e29b

Browse files
winewaymanusa
authored andcommitted
add annotation @PreserveUnknownFields for field
Signed-off-by: wineway <[email protected]>
1 parent 9cf1baf commit 7a2e29b

File tree

6 files changed

+62
-2
lines changed

6 files changed

+62
-2
lines changed

crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public abstract class AbstractJsonSchema<T, B> {
8787
public static final String ANNOTATION_REQUIRED = "io.fabric8.generator.annotation.Required";
8888
public static final String ANNOTATION_NOT_NULL = "javax.validation.constraints.NotNull";
8989
public static final String ANNOTATION_SCHEMA_FROM = "io.fabric8.crd.generator.annotation.SchemaFrom";
90+
public static final String ANNOTATION_PERSERVE_UNKNOWN_FIELDS = "io.fabric8.crd.generator.annotation.PreserveUnknownFields";
9091
public static final String ANNOTATION_SCHEMA_SWAP = "io.fabric8.crd.generator.annotation.SchemaSwap";
9192
public static final String ANNOTATION_SCHEMA_SWAPS = "io.fabric8.crd.generator.annotation.SchemaSwaps";
9293

@@ -125,21 +126,25 @@ protected static class SchemaPropsOptions {
125126
final boolean nullable;
126127
final boolean required;
127128

129+
final boolean preserveUnknownFields;
130+
128131
SchemaPropsOptions() {
129132
min = Optional.empty();
130133
max = Optional.empty();
131134
pattern = Optional.empty();
132135
nullable = false;
133136
required = false;
137+
preserveUnknownFields = false;
134138
}
135139

136140
public SchemaPropsOptions(Optional<Double> min, Optional<Double> max, Optional<String> pattern,
137-
boolean nullable, boolean required) {
141+
boolean nullable, boolean required, boolean preserveUnknownFields) {
138142
this.min = min;
139143
this.max = max;
140144
this.pattern = pattern;
141145
this.nullable = nullable;
142146
this.required = required;
147+
this.preserveUnknownFields = preserveUnknownFields;
143148
}
144149

145150
public Optional<Double> getMin() {
@@ -161,6 +166,10 @@ public boolean isNullable() {
161166
public boolean getRequired() {
162167
return required;
163168
}
169+
170+
public boolean isPreserveUnknownFields() {
171+
return preserveUnknownFields;
172+
}
164173
}
165174

166175
/**
@@ -280,7 +289,8 @@ private T internalFromImpl(TypeDef definition, Set<String> visited, InternalSche
280289
facade.max,
281290
facade.pattern,
282291
facade.nullable,
283-
facade.required);
292+
facade.required,
293+
facade.preserveSelfUnknownFields);
284294

285295
addProperty(possiblyRenamedProperty, builder, possiblyUpdatedSchema, options);
286296
}
@@ -310,6 +320,7 @@ private static class PropertyOrAccessor {
310320
private boolean required;
311321
private boolean ignored;
312322
private boolean preserveUnknownFields;
323+
private boolean preserveSelfUnknownFields;
313324
private String description;
314325
private TypeRef schemaFrom;
315326

@@ -374,6 +385,9 @@ public void process() {
374385
case ANNOTATION_JSON_ANY_SETTER:
375386
preserveUnknownFields = true;
376387
break;
388+
case ANNOTATION_PERSERVE_UNKNOWN_FIELDS:
389+
preserveSelfUnknownFields = true;
390+
break;
377391
case ANNOTATION_SCHEMA_FROM:
378392
schemaFrom = extractClassRef(a.getParameters().get("type"));
379393
break;
@@ -413,6 +427,10 @@ public boolean isPreserveUnknownFields() {
413427
return preserveUnknownFields;
414428
}
415429

430+
public boolean isPreserveSelfUnknownFields() {
431+
return preserveSelfUnknownFields;
432+
}
433+
416434
public String getDescription() {
417435
return description;
418436
}
@@ -450,6 +468,7 @@ private static class PropertyFacade {
450468
private boolean required;
451469
private boolean ignored;
452470
private boolean preserveUnknownFields;
471+
private boolean preserveSelfUnknownFields;
453472
private final Property original;
454473
private String nameContributedBy;
455474
private String descriptionContributedBy;
@@ -528,6 +547,10 @@ public Property process() {
528547
preserveUnknownFields = true;
529548
}
530549

550+
if (p.isPreserveSelfUnknownFields()) {
551+
preserveSelfUnknownFields = true;
552+
}
553+
531554
if (p.contributeSchemaFrom()) {
532555
schemaFrom = p.getSchemaFrom();
533556
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (C) 2022 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.crd.generator.annotation;
17+
18+
import java.lang.annotation.*;
19+
20+
/*
21+
* Used to tweak the behavior of the crd-generator
22+
*/
23+
@Target(ElementType.FIELD)
24+
@Retention(RetentionPolicy.RUNTIME)
25+
public @interface PreserveUnknownFields {
26+
}

crd-generator/api/src/main/java/io/fabric8/crd/generator/v1/JsonSchema.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public void addProperty(Property property, JSONSchemaPropsBuilder builder,
6666
schema.setNullable(true);
6767
}
6868

69+
if (options.isPreserveUnknownFields()) {
70+
schema.setXKubernetesPreserveUnknownFields(true);
71+
}
72+
6973
builder.addToProperties(property.getName(), schema);
7074
}
7175
}

crd-generator/api/src/main/java/io/fabric8/crd/generator/v1beta1/JsonSchema.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public void addProperty(Property property, JSONSchemaPropsBuilder builder,
6767
schema.setNullable(true);
6868
}
6969

70+
if (options.isPreserveUnknownFields()) {
71+
schema.setXKubernetesPreserveUnknownFields(true);
72+
}
73+
7074
builder.addToProperties(property.getName(), schema);
7175
}
7276
}

crd-generator/api/src/test/java/io/fabric8/crd/example/extraction/ExtractionSpec.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*/
1616
package io.fabric8.crd.example.extraction;
1717

18+
import io.fabric8.crd.generator.annotation.PreserveUnknownFields;
1819
import io.fabric8.crd.generator.annotation.SchemaFrom;
1920

2021
public class ExtractionSpec {
2122

2223
@SchemaFrom(type = FooExtractor.class)
2324
private Foo foo;
2425

26+
@PreserveUnknownFields
2527
private Foo bar;
2628

2729
}

crd-generator/api/src/test/java/io/fabric8/crd/generator/v1/JsonSchemaTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ void shouldExtractPropertiesSchemaFromExtractValueAnnotation() {
190190
JSONSchemaProps bar = spec.get("bar");
191191
Map<String, JSONSchemaProps> barProps = bar.getProperties();
192192
assertNotNull(barProps);
193+
assertTrue(bar.getXKubernetesPreserveUnknownFields());
193194

194195
// you can change everything
195196
assertEquals("integer", barProps.get("BAZ").getType());

0 commit comments

Comments
 (0)