Skip to content

Commit

Permalink
fix x-kubernetes-preserve-unknown-fields mistakenly added to parent o…
Browse files Browse the repository at this point in the history
…bject
  • Loading branch information
euberseder-hubspot committed Apr 29, 2024
1 parent e0ecd81 commit b434dd6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private T internalFromImpl(TypeDef definition, Set<String> visited, List<Interna
continue;
}
final T schema = internalFromImpl(name, possiblyRenamedProperty.getTypeRef(), visited, schemaSwaps, parameterMap);
if (facade.preserveUnknownFields) {
if (facade.isJsonAny) {
preserveUnknownFields = true;
}

Expand Down Expand Up @@ -416,6 +416,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 @@ -478,6 +479,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 @@ -520,6 +523,10 @@ public boolean isIgnored() {
return ignored;
}

public boolean isJsonAny() {
return isJsonAny;
}

public boolean isPreserveUnknownFields() {
return preserveUnknownFields;
}
Expand Down Expand Up @@ -564,6 +571,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 @@ -644,6 +652,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 @@ -196,7 +196,7 @@ void shouldExtractPropertiesSchemaFromExtractValueAnnotation() {
assertEquals(2, properties.size());
final JSONSchemaProps specSchema = properties.get("spec");
Map<String, JSONSchemaProps> spec = specSchema.getProperties();
assertEquals(2, spec.size());
assertEquals(3, spec.size());

// check typed SchemaFrom
JSONSchemaProps foo = spec.get("foo");
Expand All @@ -222,6 +222,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 b434dd6

Please sign in to comment.