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

fix for #2989 disable the native type id feature for yaml #2990

Merged
merged 1 commit into from
Apr 14, 2021
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
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\"\n", Serialization.asYaml(root));
}

}