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

Preserve unknown fields in presence of JsonAnyGetter or JsonAnySetter #3691

Merged
merged 1 commit into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public abstract class AbstractJsonSchema<T, B> {
public static final String ANNOTATION_JSON_PROPERTY = "com.fasterxml.jackson.annotation.JsonProperty";
public static final String ANNOTATION_JSON_PROPERTY_DESCRIPTION = "com.fasterxml.jackson.annotation.JsonPropertyDescription";
public static final String ANNOTATION_JSON_IGNORE = "com.fasterxml.jackson.annotation.JsonIgnore";
public static final String ANNOTATION_JSON_ANY_GETTER = "com.fasterxml.jackson.annotation.JsonAnyGetter";
public static final String ANNOTATION_JSON_ANY_SETTER = "com.fasterxml.jackson.annotation.JsonAnySetter";
public static final String ANNOTATION_NOT_NULL = "javax.validation.constraints.NotNull";

public static final String JSON_NODE_TYPE = "com.fasterxml.jackson.databind.JsonNode";
Expand Down Expand Up @@ -125,7 +127,7 @@ private T internalFromImpl(TypeDef definition, Set<String> visited, String... ig
.emptySet();
List<String> required = new ArrayList<>();

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

Expand All @@ -149,6 +151,10 @@ private T internalFromImpl(TypeDef definition, Set<String> visited, String... ig
continue;
}
final T schema = internalFromImpl(name, possiblyRenamedProperty.getTypeRef(), visited);
if (facade.preserveUnknownFields) {
preserveUnknownFields = true;
}

// if we got a description from the field or an accessor, use it
final String description = facade.description;
final T possiblyUpdatedSchema;
Expand Down Expand Up @@ -180,6 +186,7 @@ private static class PropertyOrAccessor {
private String renamedTo;
private boolean required;
private boolean ignored;
private boolean preserveUnknownFields;
private String description;

private PropertyOrAccessor(Collection<AnnotationRef> annotations, String name, String propertyName, boolean isMethod) {
Expand Down Expand Up @@ -217,6 +224,9 @@ public void process() {
break;
case ANNOTATION_JSON_IGNORE:
ignored = true;
case ANNOTATION_JSON_ANY_GETTER:
case ANNOTATION_JSON_ANY_SETTER:
preserveUnknownFields = true;
break;
}
});
Expand All @@ -234,6 +244,10 @@ public boolean isIgnored() {
return ignored;
}

public boolean isPreserveUnknownFields() {
return preserveUnknownFields;
}

public String getDescription() {
return description;
}
Expand All @@ -258,6 +272,7 @@ private static class PropertyFacade {
private String description;
private boolean required;
private boolean ignored;
private boolean preserveUnknownFields;
private final Property original;
private String nameContributedBy;
private String descriptionContributedBy;
Expand Down Expand Up @@ -310,6 +325,10 @@ public Property process() {
} else if (p.isIgnored()) {
ignored = true;
}

if (p.isPreserveUnknownFields()) {
preserveUnknownFields = true;
}
});

return renamedTo != null ? new Property(original.getAnnotations(), original.getTypeRef(), renamedTo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ public JsonNode getFree() {
return free;
}

private Foo foo;

public Foo getFoo() { return foo; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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.example.json;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;

import java.util.HashMap;
import java.util.Map;

public class Foo {

private Map<String, Object> configAsMap = new HashMap<>();

@JsonAnyGetter
public Map<String, Object> getConfigAsMap() {
return configAsMap;
}

@JsonAnySetter
public void setConfigAsMap(String name, Object value) {
this.configAsMap.put(name, value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ void shouldProbuceKubernetesPreserveFields() {
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 descriptions are present
// check preserve unknown fields is present
assertTrue(spec.containsKey("free"));
JSONSchemaProps freeField = spec.get("free");

Expand All @@ -130,5 +130,10 @@ void shouldProbuceKubernetesPreserveFields() {
JSONSchemaProps field = spec.get("field");

assertNull(field.getXKubernetesPreserveUnknownFields());

assertTrue(spec.containsKey("foo"));
JSONSchemaProps fooField = spec.get("foo");

assertTrue(fooField.getXKubernetesPreserveUnknownFields());
}
}