Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

fix: reuses shared object mapper instead of recreating them #796

Merged
merged 1 commit into from
Jul 21, 2023
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 @@ -46,6 +46,7 @@
public class OpenApiToRestDslResource {

private static final Logger LOG = Logger.getLogger(OpenApiToRestDslResource.class);
private static final ObjectMapper MAPPER = new ObjectMapper();

@Inject
private CamelRuntime camelRuntime;
Expand All @@ -65,14 +66,13 @@ public String generate(
}

private JsonNode readOpenApiSpec(final String input) {
var mapper = new ObjectMapper();
try {
return mapper.readTree(input);
return MAPPER.readTree(input);
} catch (Exception e) {
LOG.debug("Failed to parse input as JSON, trying YAML", e);
Yaml loader = new Yaml(new SafeConstructor(new LoaderOptions()));
Map map = loader.load(input);
return mapper.convertValue(map, JsonNode.class);
return MAPPER.convertValue(map, JsonNode.class);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;

@QuarkusTest
@TestHTTPEndpoint(OpenApiToRestDslResource.class)
class OpenApiToRestDslResourceTest {
private static final ObjectMapper MAPPER = new ObjectMapper();

@Test
void jsonToYaml() throws Exception {
Expand Down Expand Up @@ -64,15 +65,14 @@ void yamlToYaml() throws Exception {
}

private void verifyYaml(String answer) throws Exception {
final ObjectMapper mapper = new ObjectMapper();
var loader = new Yaml(new SafeConstructor(new LoaderOptions()));
ArrayList list = loader.load(answer);
var actual = (ArrayNode) mapper.convertValue(list, JsonNode.class);
var actual = (ArrayNode) MAPPER.convertValue(list, JsonNode.class);
list = loader.load(Files.readString(Path.of(
OpenApiToRestDslResourceTest.class.getResource(
"restdsl-petstore.yaml")
.toURI())));
var expected = (ArrayNode) mapper.convertValue(list, JsonNode.class);
var expected = (ArrayNode) MAPPER.convertValue(list, JsonNode.class);
assertThat(actual).isEqualTo(expected);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;

import io.fabric8.kubernetes.client.CustomResource;
import io.kaoto.backend.api.service.deployment.generator.DeploymentGeneratorService;
Expand Down Expand Up @@ -50,6 +50,11 @@
public class DeploymentsResource {

private static final Logger LOG = Logger.getLogger(DeploymentsResource.class);

private static final ObjectMapper YAML_MAPPER = YAMLMapper.builder()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build();

private ClusterService clusterService;
private Instance<DeploymentGeneratorService> parsers;

Expand Down Expand Up @@ -111,18 +116,14 @@ public String start(

private String securityCheck(final String crd) {

ObjectMapper yamlMapper =
new ObjectMapper(new YAMLFactory())
.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);


boolean valid = false;
for (var parser : parsers) {
for (Class<? extends CustomResource> c
: parser.supportedCustomResources()) {
try {
yamlMapper.readValue(crd, c);
YAML_MAPPER.readValue(crd, c);
valid = true;
} catch (Exception e) {
LOG.trace("We tried to parse with " + c.getName() + " and"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.kaoto.backend.camel;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;

public final class KamelHelper {
public static final ObjectMapper JSON_MAPPER = JsonMapper.builder()
.build();

public static final ObjectMapper YAML_MAPPER = YAMLMapper.builder()
.build();

private KamelHelper() {
// final class
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import org.apache.camel.v1alpha1.kameletspec.definition.Properties;
import org.jboss.logging.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.fabric8.kubernetes.api.model.AnyType;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.kaoto.backend.api.metadata.catalog.StepCatalog;
Expand Down Expand Up @@ -192,8 +190,7 @@ public void populateKamelet(
if (original_spec != null && original_spec instanceof KameletSpec ospec) {
spec = ospec;
} else if (original_spec != null && original_spec instanceof Map ospec) {
ObjectMapper mapper = new ObjectMapper();
spec = mapper.convertValue(ospec, KameletSpec.class);
spec = KamelHelper.JSON_MAPPER.convertValue(ospec, KameletSpec.class);
} else {
spec = new KameletSpec();
}
Expand All @@ -216,8 +213,7 @@ public void populateKamelet(
if (metadata.get("definition") instanceof Definition def) {
kamelet.getSpec().setDefinition(def);
} else if (metadata.get("definition") instanceof Map map) {
ObjectMapper objectMapper = new ObjectMapper();
Definition def = objectMapper.convertValue(map, Definition.class);
Definition def = KamelHelper.JSON_MAPPER.convertValue(map, Definition.class);
kamelet.getSpec().setDefinition(def);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
import org.yaml.snakeyaml.error.YAMLException;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import io.kaoto.backend.camel.KamelHelper;
import io.kaoto.backend.camel.model.deployment.kamelet.KameletDefinitionProperty;
import io.kaoto.backend.camel.model.deployment.kamelet.SimplifiedKamelet;
import io.kaoto.backend.metadata.parser.YamlProcessFile;
Expand Down Expand Up @@ -46,9 +45,10 @@ public List<Step> parseInputStream(Reader reader) {
return List.of();
}

var YAML_MAPPER = new ObjectMapper(new YAMLFactory())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
SimplifiedKamelet kamelet = YAML_MAPPER.readValue(yaml, SimplifiedKamelet.class);
SimplifiedKamelet kamelet = KamelHelper.YAML_MAPPER.readerFor(SimplifiedKamelet.class)
.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.readValue(yaml);

Step step = new Step();
step.setKind(kind);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import org.apache.camel.v1.IntegrationStatus;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.CustomResource;
Expand All @@ -19,6 +17,7 @@
import io.fabric8.kubernetes.model.annotation.Singular;
import io.fabric8.kubernetes.model.annotation.Version;
import io.kaoto.backend.api.metadata.catalog.StepCatalog;
import io.kaoto.backend.camel.KamelHelper;
import io.kaoto.backend.camel.KamelPopulator;
import io.kaoto.backend.camel.model.deployment.kamelet.Bean;
import io.kaoto.backend.camel.model.deployment.kamelet.Flow;
Expand Down Expand Up @@ -68,8 +67,7 @@ public Integration(
if (original_spec != null && original_spec instanceof IntegrationSpec ospec) {
this.setSpec(ospec);
} else if (original_spec != null && original_spec instanceof Map ospec) {
ObjectMapper mapper = new ObjectMapper();
this.setSpec(mapper.convertValue(original_spec, IntegrationSpec.class));
this.setSpec(KamelHelper.JSON_MAPPER.convertValue(original_spec, IntegrationSpec.class));
} else {
this.setSpec(new IntegrationSpec());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.kaoto.backend.camel.model.deployment.kamelet.FlowStep;

Expand All @@ -23,18 +22,18 @@ public Object deserialize(
final DeserializationContext deserializationContext) {
try {
JsonNode n = jsonParser.getCodec().readTree(jsonParser);
return deserializeFlowStep(n);
return deserializeFlowStep(jsonParser, n);
} catch (Exception e) {
LOG.error("Error trying to deserialize step: " + e.getMessage());
}

return new UriFlowStep();
}

public FlowStep deserializeFlowStep(final JsonNode n) throws JsonProcessingException {
public FlowStep deserializeFlowStep(final JsonParser p, final JsonNode n) throws JsonProcessingException {
for (var step : getFlowSteps().entrySet()) {
if (n.get(step.getKey()) != null) {
return (FlowStep) new ObjectMapper().readValue(n.toPrettyString(), step.getValue());
return (FlowStep) p.getCodec().treeToValue(n, step.getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class LoadBalanceDeserializer extends JsonDeserializer {
private static final Logger LOG = Logger.getLogger(LoadBalanceDeserializer.class);
Expand All @@ -35,15 +34,15 @@ public Object deserialize(final JsonParser jsonParser,
field.getValue().elements().forEachRemaining(
s -> {
try {
step.getSteps().add(flowStepDeserializer.deserializeFlowStep(s));
step.getSteps().add(flowStepDeserializer.deserializeFlowStep(jsonParser, s));
} catch (JsonProcessingException e) {
LOG.error("Couldn't deserialize step", e);
}
});
}
else {
step.getProperties().put(field.getKey(),
new ObjectMapper().readValue(field.getValue().toPrettyString(), Map.class));
jsonParser.getCodec().treeToValue(field.getValue(), Map.class));
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.kaoto.backend.camel.service.deployment.generator;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Represent;

import java.util.Map;
import com.fasterxml.jackson.core.type.TypeReference;

import io.kaoto.backend.camel.KamelHelper;

public class Representer extends org.yaml.snakeyaml.representer.Representer {

Expand All @@ -20,8 +22,7 @@ protected Represent bean() {
return new RepresentMap() {
@Override
public Node representData(final Object data) {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> properties = mapper.convertValue(data,
Map<String, Object> properties = KamelHelper.JSON_MAPPER.convertValue(data,
new TypeReference<Map<String, Object>>() {});
return representMapping(getTag(data.getClass(), Tag.MAP),
properties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@

import org.jboss.logging.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.kaoto.backend.api.metadata.catalog.StepCatalog;
import io.kaoto.backend.api.service.deployment.generator.DeploymentGeneratorService;
import io.kaoto.backend.api.service.step.parser.StepParserService;
import io.kaoto.backend.camel.KamelHelper;
import io.kaoto.backend.camel.model.deployment.camelroute.Integration;
import io.kaoto.backend.camel.model.deployment.camelroute.IntegrationFlow;
import io.kaoto.backend.camel.service.deployment.generator.AbstractDeploymentGeneratorService;
Expand Down Expand Up @@ -93,8 +91,7 @@ public String parse(List<StepParserService.ParseResult<Step>> flows) {
public CustomResource parse(final String input) {
if (stepParserService.appliesTo(input)) {
try {
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
return yamlMapper.readValue(input, Integration.class);
return KamelHelper.YAML_MAPPER.readValue(input, Integration.class);
} catch (Exception e) {
LOG.trace("Tried creating an integration and it didn't work.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package io.kaoto.backend.camel.service.deployment.generator.camelroute;

import java.util.LinkedHashMap;
import java.util.Map;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.Tag;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.kaoto.backend.camel.service.deployment.generator.kamelet.KameletRepresenter;

import io.kaoto.backend.camel.KamelHelper;
import io.kaoto.backend.camel.model.deployment.camelroute.IntegrationSpec;
import io.kaoto.backend.camel.model.deployment.kamelet.Bean;
import io.kaoto.backend.camel.model.deployment.kamelet.Flow;
import io.kaoto.backend.camel.model.deployment.camelroute.IntegrationSpec;
import io.kaoto.backend.camel.model.deployment.rest.Rest;
import io.kaoto.backend.camel.service.deployment.generator.kamelet.KameletRepresenter;
import io.quarkus.runtime.util.StringUtil;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.Tag;

import java.util.LinkedHashMap;
import java.util.Map;

public class IntegrationRepresenter extends KameletRepresenter {

Expand All @@ -28,8 +30,7 @@ private void spec() {
new RepresentMap() {
@Override
public Node representData(final Object data) {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> properties = mapper.convertValue(data,
Map<String, Object> properties = KamelHelper.JSON_MAPPER.convertValue(data,
new TypeReference<Map<String, Object>>() {});
properties.put("flows", ((IntegrationSpec) data).get_flows());
properties.remove("_flows");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.kaoto.backend.api.service.deployment.generator.DeploymentGeneratorService;
import io.kaoto.backend.api.service.step.parser.StepParserService;
import io.kaoto.backend.camel.KamelHelper;
import io.kaoto.backend.camel.model.deployment.kamelet.KameletBinding;
import io.kaoto.backend.camel.model.deployment.kamelet.KameletBindingStep;
import io.kaoto.backend.camel.model.deployment.kamelet.KameletBindingStepRef;
Expand Down Expand Up @@ -76,8 +74,7 @@ public String parse(final List<Step> stepList,
if (original_spec != null && original_spec instanceof KameletBindingSpec ospec) {
spec = ospec;
} else if (original_spec != null && original_spec instanceof Map ospec) {
ObjectMapper mapper = new ObjectMapper();
spec = mapper.convertValue(ospec, KameletBindingSpec.class);
spec = KamelHelper.JSON_MAPPER.convertValue(ospec, KameletBindingSpec.class);
} else {
spec = new KameletBindingSpec();
}
Expand Down Expand Up @@ -228,8 +225,7 @@ public List<Class<? extends CustomResource>> supportedCustomResources() {
public CustomResource parse(final String input) {
if (stepParserService.appliesTo(input)) {
try {
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
return yamlMapper.readValue(input, KameletBinding.class);
return KamelHelper.YAML_MAPPER.readValue(input, KameletBinding.class);
} catch (Exception e) {
LOG.trace("Tried creating a kamelet binding and it didn't work.");
}
Expand Down
Loading