Skip to content

Commit

Permalink
fix for #2989 disable the native type id feature for yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Apr 13, 2021
1 parent 498952e commit 30975c9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#### Bugs

* Fix #2989: serialization will generate valid yaml when using subtypes

#### Improvements

#### Dependency Upgrade
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.client.KubernetesClientException;
Expand Down Expand Up @@ -46,7 +47,7 @@ private Serialization() { }
static {
JSON_MAPPER.registerModule(new JavaTimeModule());
}
private static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory());
private static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID));
private static final String DOCUMENT_DELIMITER = "---";

public static ObjectMapper jsonMapper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.TextNode;
Expand Down Expand Up @@ -162,4 +164,50 @@ void testSerializeYamlWithAlias() {
assertEquals("python:3.7", pod.getSpec().getContainers().get(1).getImage());
assertEquals(new Quantity("100m"), pod.getSpec().getContainers().get(1).getResources().getRequests().get("cpu"));
}

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "type"
)
@JsonSubTypes(
@JsonSubTypes.Type(value = Typed.class, name = "x")
)
public interface Typeable {

String getType();

}

public static class Typed implements Typeable {

@Override
public String getType() {
return "x";
}

}

public static class Root {

private Typeable typeable;

public Typeable getTypeable() {
return typeable;
}

public void setTypeable(Typeable typeable) {
this.typeable = typeable;
}
}

@Test
void testSerializeYamlWithJsonSubTypes() {
Root root = new Root();
root.setTypeable(new Typed());
assertEquals("---\n"
+ "typeable:\n"
+ " type: \"x\"", Serialization.asYaml(root));
}

}

0 comments on commit 30975c9

Please sign in to comment.