Skip to content

Commit

Permalink
port preserve-unknown-fields-fix from 5.12.2 to 6.14.3 branch (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
euberseder-hubspot authored Jan 3, 2025
1 parent 0e42f50 commit 5f25722
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private T internalFromImpl(TypeDef definition, LinkedHashMap<String, String> vis
}
final T schema = internalFromImpl(name, possiblyRenamedProperty.getTypeRef(), visited, schemaSwaps, parameterMap);
visited = savedVisited;
if (facade.preserveUnknownFields) {
if (facade.isJsonAny) {
preserveUnknownFields = true;
}

Expand All @@ -383,7 +383,7 @@ private T internalFromImpl(TypeDef definition, LinkedHashMap<String, String> vis
facade.validationRules,
facade.nullable,
facade.required,
facade.preserveUnknownFields);
facade.preserveUnknownFields || facade.isJsonAny);

addProperty(possiblyRenamedProperty, builder, possiblyUpdatedSchema, options);
}
Expand Down Expand Up @@ -466,6 +466,7 @@ private static class PropertyOrAccessor {
private boolean nullable;
private boolean required;
private boolean ignored;
private boolean isJsonAny;
private boolean preserveUnknownFields;
private String description;
private TypeRef schemaFrom;
Expand Down Expand Up @@ -531,6 +532,8 @@ public void process() {
break;
case ANNOTATION_JSON_ANY_GETTER:
case ANNOTATION_JSON_ANY_SETTER:
isJsonAny = true;
break;
case ANNOTATION_PERSERVE_UNKNOWN_FIELDS:
preserveUnknownFields = true;
break;
Expand Down Expand Up @@ -581,6 +584,10 @@ public boolean isIgnored() {
return ignored;
}

public boolean isJsonAny() {
return isJsonAny;
}

public boolean isPreserveUnknownFields() {
return preserveUnknownFields;
}
Expand Down Expand Up @@ -623,6 +630,7 @@ private static class PropertyFacade {
private boolean nullable;
private boolean required;
private boolean ignored;
private boolean isJsonAny;
private boolean preserveUnknownFields;
private final Property original;
private String nameContributedBy;
Expand Down Expand Up @@ -695,6 +703,7 @@ public Property process() {
ignored = true;
}

isJsonAny = p.isJsonAny() || isJsonAny;
preserveUnknownFields = p.isPreserveUnknownFields() || preserveUnknownFields;

if (p.contributeSchemaFrom()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public class ExtractionSpec {
@PreserveUnknownFields
private Foo bar;

private Qux qux;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.fabric8.crd.example.extraction;

import io.fabric8.crd.generator.annotation.PreserveUnknownFields;

public class Qux {
@PreserveUnknownFields
public Foo foo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ void shouldProduceKubernetesPreserveFields() {

assertEquals("object", fooField.getType());
assertTrue(fooField.getXKubernetesPreserveUnknownFields());

Map<String, JSONSchemaProps> fooProperties = fooField.getProperties();
JSONSchemaProps configAsMapField = fooProperties.get("configAsMap");

assertNotNull(configAsMapField);
assertTrue(configAsMapField.getXKubernetesPreserveUnknownFields());
}

@Test
Expand All @@ -214,7 +220,7 @@ void shouldExtractPropertiesSchemaFromExtractValueAnnotation() {
assertNotNull(schema);
Map<String, JSONSchemaProps> properties = assertSchemaHasNumberOfProperties(schema, 2);
final JSONSchemaProps specSchema = properties.get("spec");
Map<String, JSONSchemaProps> spec = assertSchemaHasNumberOfProperties(specSchema, 2);
Map<String, JSONSchemaProps> spec = assertSchemaHasNumberOfProperties(specSchema, 3);

// check typed SchemaFrom
JSONSchemaProps foo = spec.get("foo");
Expand All @@ -240,6 +246,13 @@ void shouldExtractPropertiesSchemaFromExtractValueAnnotation() {

// you can exclude fields
assertNull(barProps.get("baz"));

// verify that x-kubernetes-preserve-unknown-fields isn't on parent object when
// nested object is annotated with @PreserveUnknownFields
JSONSchemaProps qux = spec.get("qux");
Map<String, JSONSchemaProps> quxProps = qux.getProperties();
assertTrue(quxProps.get("foo").getXKubernetesPreserveUnknownFields());
assertNull(qux.getXKubernetesPreserveUnknownFields());
}

@Test
Expand Down

0 comments on commit 5f25722

Please sign in to comment.