From 25b06d0ebbe81bcfc61524dc68bf69121bcc6f0b Mon Sep 17 00:00:00 2001 From: Luca Burgazzoli Date: Thu, 20 Jul 2023 16:09:46 +0200 Subject: [PATCH] fix: reuses shared object mapper instead of recreating them #786 --- .../resource/v1/OpenApiToRestDslResource.java | 6 +-- .../v1/OpenApiToRestDslResourceTest.java | 14 +++--- .../api/resource/v1/DeploymentsResource.java | 15 ++++--- .../io/kaoto/backend/camel/KamelHelper.java | 17 +++++++ .../kaoto/backend/camel/KamelPopulator.java | 8 +--- .../step/kamelet/KameletFileProcessor.java | 10 ++--- .../deployment/camelroute/Integration.java | 6 +-- .../kamelet/step/FlowStepDeserializer.java | 7 ++- .../kamelet/step/LoadBalanceDeserializer.java | 5 +-- .../deployment/generator/Representer.java | 11 ++--- ...IntegrationDeploymentGeneratorService.java | 7 +-- .../camelroute/IntegrationRepresenter.java | 23 +++++----- ...eletBindingDeploymentGeneratorService.java | 10 ++--- .../KameletDeploymentGeneratorService.java | 7 +-- .../generator/kamelet/KameletRepresenter.java | 45 +++++++++---------- .../CamelRouteStepParserService.java | 9 ++-- .../IntegrationStepParserService.java | 35 ++++++++------- .../KameletBindingStepParserService.java | 6 +-- .../kamelet/KameletStepParserService.java | 6 +-- .../step/marshal/SerializationTest.java | 32 ++++++------- .../api/metadata/catalog/AbstractCatalog.java | 17 ++++--- 21 files changed, 148 insertions(+), 148 deletions(-) create mode 100644 camel-support/src/main/java/io/kaoto/backend/camel/KamelHelper.java diff --git a/api-rest/src/main/java/io/kaoto/backend/api/rest/resource/v1/OpenApiToRestDslResource.java b/api-rest/src/main/java/io/kaoto/backend/api/rest/resource/v1/OpenApiToRestDslResource.java index a5a58b996..3716f8226 100644 --- a/api-rest/src/main/java/io/kaoto/backend/api/rest/resource/v1/OpenApiToRestDslResource.java +++ b/api-rest/src/main/java/io/kaoto/backend/api/rest/resource/v1/OpenApiToRestDslResource.java @@ -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; @@ -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); } } diff --git a/api-rest/src/test/java/io/kaoto/backend/api/rest/resource/v1/OpenApiToRestDslResourceTest.java b/api-rest/src/test/java/io/kaoto/backend/api/rest/resource/v1/OpenApiToRestDslResourceTest.java index c0586fca0..895ad84bb 100644 --- a/api-rest/src/test/java/io/kaoto/backend/api/rest/resource/v1/OpenApiToRestDslResourceTest.java +++ b/api-rest/src/test/java/io/kaoto/backend/api/rest/resource/v1/OpenApiToRestDslResourceTest.java @@ -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 { @@ -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); } diff --git a/api/src/main/java/io/kaoto/backend/api/resource/v1/DeploymentsResource.java b/api/src/main/java/io/kaoto/backend/api/resource/v1/DeploymentsResource.java index a35df4b77..fb6bca851 100644 --- a/api/src/main/java/io/kaoto/backend/api/resource/v1/DeploymentsResource.java +++ b/api/src/main/java/io/kaoto/backend/api/resource/v1/DeploymentsResource.java @@ -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; @@ -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 parsers; @@ -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 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" diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/KamelHelper.java b/camel-support/src/main/java/io/kaoto/backend/camel/KamelHelper.java new file mode 100644 index 000000000..97c1d5f39 --- /dev/null +++ b/camel-support/src/main/java/io/kaoto/backend/camel/KamelHelper.java @@ -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 + } +} diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/KamelPopulator.java b/camel-support/src/main/java/io/kaoto/backend/camel/KamelPopulator.java index 7405e68ca..85393d892 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/KamelPopulator.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/KamelPopulator.java @@ -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; @@ -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(); } @@ -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); } } diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/metadata/parser/step/kamelet/KameletFileProcessor.java b/camel-support/src/main/java/io/kaoto/backend/camel/metadata/parser/step/kamelet/KameletFileProcessor.java index b42a9e720..f3b1e0e24 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/metadata/parser/step/kamelet/KameletFileProcessor.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/metadata/parser/step/kamelet/KameletFileProcessor.java @@ -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; @@ -46,9 +45,10 @@ public List 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); diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/camelroute/Integration.java b/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/camelroute/Integration.java index c38885eb9..a18f78345 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/camelroute/Integration.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/camelroute/Integration.java @@ -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; @@ -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; @@ -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()); } diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/kamelet/step/FlowStepDeserializer.java b/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/kamelet/step/FlowStepDeserializer.java index 100b6bd6d..ec60388ac 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/kamelet/step/FlowStepDeserializer.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/kamelet/step/FlowStepDeserializer.java @@ -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; @@ -23,7 +22,7 @@ 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()); } @@ -31,10 +30,10 @@ public Object deserialize( 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()); } } diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/kamelet/step/LoadBalanceDeserializer.java b/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/kamelet/step/LoadBalanceDeserializer.java index cf2cde8e6..fee90d3ef 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/kamelet/step/LoadBalanceDeserializer.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/model/deployment/kamelet/step/LoadBalanceDeserializer.java @@ -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); @@ -35,7 +34,7 @@ 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); } @@ -43,7 +42,7 @@ public Object deserialize(final JsonParser jsonParser, } else { step.getProperties().put(field.getKey(), - new ObjectMapper().readValue(field.getValue().toPrettyString(), Map.class)); + jsonParser.getCodec().treeToValue(field.getValue(), Map.class)); } } } catch (Exception e) { diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/Representer.java b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/Representer.java index e022dd306..9544bbfb8 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/Representer.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/Representer.java @@ -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 { @@ -20,8 +22,7 @@ protected Represent bean() { return new RepresentMap() { @Override public Node representData(final Object data) { - ObjectMapper mapper = new ObjectMapper(); - Map properties = mapper.convertValue(data, + Map properties = KamelHelper.JSON_MAPPER.convertValue(data, new TypeReference>() {}); return representMapping(getTag(data.getClass(), Tag.MAP), properties, diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/camelroute/IntegrationDeploymentGeneratorService.java b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/camelroute/IntegrationDeploymentGeneratorService.java index 6a30df55a..836ef8ea6 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/camelroute/IntegrationDeploymentGeneratorService.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/camelroute/IntegrationDeploymentGeneratorService.java @@ -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; @@ -93,8 +91,7 @@ public String parse(List> 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."); } diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/camelroute/IntegrationRepresenter.java b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/camelroute/IntegrationRepresenter.java index c6deeba63..91ad917c6 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/camelroute/IntegrationRepresenter.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/camelroute/IntegrationRepresenter.java @@ -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 { @@ -28,8 +30,7 @@ private void spec() { new RepresentMap() { @Override public Node representData(final Object data) { - ObjectMapper mapper = new ObjectMapper(); - Map properties = mapper.convertValue(data, + Map properties = KamelHelper.JSON_MAPPER.convertValue(data, new TypeReference>() {}); properties.put("flows", ((IntegrationSpec) data).get_flows()); properties.remove("_flows"); diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletBindingDeploymentGeneratorService.java b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletBindingDeploymentGeneratorService.java index 74abdee18..6f323bac9 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletBindingDeploymentGeneratorService.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletBindingDeploymentGeneratorService.java @@ -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; @@ -76,8 +74,7 @@ public String parse(final List 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(); } @@ -228,8 +225,7 @@ public List> 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."); } diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletDeploymentGeneratorService.java b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletDeploymentGeneratorService.java index c9f32ae68..8ba46e571 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletDeploymentGeneratorService.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletDeploymentGeneratorService.java @@ -14,15 +14,13 @@ import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.representer.Representer; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; - import io.fabric8.kubernetes.api.model.Pod; 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.kamelet.Kamelet; import io.kaoto.backend.camel.service.step.parser.kamelet.KameletStepParserService; import io.kaoto.backend.model.deployment.Deployment; @@ -111,8 +109,7 @@ public List> supportedCustomResources() { public CustomResource parse(final String input) { if (stepParserService.appliesTo(input)) { try { - ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); - return yamlMapper.readValue(input, Kamelet.class); + return KamelHelper.YAML_MAPPER.readValue(input, Kamelet.class); } catch (Exception e) { LOG.trace("Tried creating a kamelet and it didn't work."); } diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletRepresenter.java b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletRepresenter.java index 5d25e4fcf..cc7ce37f7 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletRepresenter.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/service/deployment/generator/kamelet/KameletRepresenter.java @@ -1,15 +1,27 @@ package io.kaoto.backend.camel.service.deployment.generator.kamelet; -import com.fasterxml.jackson.annotation.JsonInclude; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.camel.v1alpha1.KameletBindingSpec; +import org.apache.camel.v1alpha1.KameletSpec; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.introspector.BeanAccess; +import org.yaml.snakeyaml.introspector.Property; +import org.yaml.snakeyaml.nodes.Node; +import org.yaml.snakeyaml.nodes.NodeTuple; +import org.yaml.snakeyaml.nodes.Tag; + import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; + import io.fabric8.kubernetes.client.CustomResource; -import io.kaoto.backend.camel.service.deployment.generator.Representer; +import io.kaoto.backend.camel.KamelHelper; 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.kamelet.expression.Expression; import io.kaoto.backend.camel.model.deployment.kamelet.FlowStep; import io.kaoto.backend.camel.model.deployment.kamelet.KameletTemplate; +import io.kaoto.backend.camel.model.deployment.kamelet.expression.Expression; import io.kaoto.backend.camel.model.deployment.kamelet.expression.Script; import io.kaoto.backend.camel.model.deployment.kamelet.expression.ScriptExpression; import io.kaoto.backend.camel.model.deployment.kamelet.step.AggregateFlowStep; @@ -68,18 +80,7 @@ import io.kaoto.backend.camel.model.deployment.kamelet.step.choice.Choice; import io.kaoto.backend.camel.model.deployment.kamelet.step.choice.Otherwise; import io.kaoto.backend.camel.model.deployment.kamelet.step.choice.SuperChoice; -import org.apache.camel.v1alpha1.KameletBindingSpec; -import org.apache.camel.v1alpha1.KameletSpec; -import org.yaml.snakeyaml.DumperOptions; -import org.yaml.snakeyaml.introspector.BeanAccess; -import org.yaml.snakeyaml.introspector.Property; -import org.yaml.snakeyaml.nodes.Node; -import org.yaml.snakeyaml.nodes.NodeTuple; -import org.yaml.snakeyaml.nodes.Tag; - -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; +import io.kaoto.backend.camel.service.deployment.generator.Representer; public class KameletRepresenter extends Representer { @@ -115,9 +116,7 @@ private void customResource() { new RepresentMap() { @Override public Node representData(final Object data) { - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - Map properties = objectMapper.convertValue(data, + Map properties = KamelHelper.JSON_MAPPER.convertValue(data, new TypeReference>() {}); CustomResource cr = (CustomResource) data; @@ -166,8 +165,6 @@ public Node representData(final Object data) { private void spec() { //spec does not have the right order - final var objectMapper = new ObjectMapper(); - objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); this.multiRepresenters.put(KameletBindingSpec.class, new RepresentMap() { @Override @@ -181,7 +178,7 @@ public Node representData(final Object data) { } properties.put("sink", null); properties.putAll( - objectMapper.convertValue(data, new TypeReference>() {})); + KamelHelper.JSON_MAPPER.convertValue(data, new TypeReference>() {})); return representMapping(getTag(data.getClass(), Tag.MAP), properties, DumperOptions.FlowStyle.AUTO); } }); @@ -193,7 +190,9 @@ public Node representData(final Object data) { Map properties = new LinkedHashMap<>(); KameletSpec spec = (KameletSpec) data; properties.putAll( - new ObjectMapper().convertValue(data, new TypeReference>() {})); + KamelHelper.JSON_MAPPER.convertValue( + data, + new TypeReference>() {})); properties.put("template", spec.getTemplate()); return representMapping( getTag(data.getClass(), Tag.MAP), properties, DumperOptions.FlowStyle.AUTO); diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/camelroute/CamelRouteStepParserService.java b/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/camelroute/CamelRouteStepParserService.java index ff0108f74..b812d0dc8 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/camelroute/CamelRouteStepParserService.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/camelroute/CamelRouteStepParserService.java @@ -10,10 +10,9 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import io.kaoto.backend.api.service.step.parser.StepParserService; +import io.kaoto.backend.camel.KamelHelper; import io.kaoto.backend.camel.model.deployment.camelroute.CamelRoute; import io.kaoto.backend.camel.model.deployment.kamelet.FlowStep; import io.kaoto.backend.camel.model.deployment.rest.Rest; @@ -125,9 +124,9 @@ public boolean appliesTo(final String input) { private CamelRoute getCamelRoute(final String input) { try { - ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()) - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - return yamlMapper.readValue(input, CamelRoute.class); + return KamelHelper.YAML_MAPPER.readerFor(CamelRoute.class) + .without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) + .readValue(input); } catch (JsonProcessingException e) { //We don't care what happened, it is wrongly formatted and that's it LOG.trace("Error trying to parse camel route.", e); diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/camelroute/IntegrationStepParserService.java b/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/camelroute/IntegrationStepParserService.java index 40ef68952..a3bdd7681 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/camelroute/IntegrationStepParserService.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/camelroute/IntegrationStepParserService.java @@ -1,28 +1,30 @@ package io.kaoto.backend.camel.service.step.parser.camelroute; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; + import io.kaoto.backend.api.service.step.parser.StepParserService; -import io.kaoto.backend.camel.service.step.parser.kamelet.KameletStepParserService; +import io.kaoto.backend.camel.KamelHelper; import io.kaoto.backend.camel.model.deployment.camelroute.Integration; 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.kamelet.FlowStep; import io.kaoto.backend.camel.model.deployment.rest.Rest; +import io.kaoto.backend.camel.service.step.parser.kamelet.KameletStepParserService; import io.kaoto.backend.model.step.Step; import io.quarkus.runtime.annotations.RegisterForReflection; - import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * 🐱miniclass IntegrationStepParserService (StepParserService) @@ -31,8 +33,12 @@ @RegisterForReflection public class IntegrationStepParserService implements StepParserService { + private static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory()) + .registerModule(new SimpleModule().addDeserializer(Flow.class, new FlowDeserializer())); + private KameletStepParserService ksps; + @Override public ParseResult deepParse(final String input) { if (!appliesTo(input)) { @@ -43,8 +49,7 @@ public ParseResult deepParse(final String input) { ParseResult res = new ParseResult<>(); List steps = new ArrayList<>(); try { - ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); - Integration integration = yamlMapper.readValue(input, Integration.class); + Integration integration = KamelHelper.YAML_MAPPER.readValue(input, Integration.class); ksps.processMetadata(res, integration.getMetadata()); res.setParameters(new ArrayList<>()); @@ -91,11 +96,7 @@ public List> getParsedFlows(String input) { metadata.setParameters(new ArrayList<>()); answer.add(metadata); try { - ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); - var module = new SimpleModule(); - module.addDeserializer(Flow.class, new FlowDeserializer()); - yamlMapper.registerModule(module); - Integration integration = yamlMapper.readValue(input, Integration.class); + Integration integration = YAML_MAPPER.readValue(input, Integration.class); ksps.processMetadata(metadata, integration.getMetadata()); diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/kamelet/KameletBindingStepParserService.java b/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/kamelet/KameletBindingStepParserService.java index 2fe6ba32b..8ea800b81 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/kamelet/KameletBindingStepParserService.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/kamelet/KameletBindingStepParserService.java @@ -17,12 +17,11 @@ import org.jboss.logging.Logger; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import io.fabric8.kubernetes.api.model.ObjectMeta; import io.kaoto.backend.api.metadata.catalog.StepCatalog; 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.service.dsl.kamelet.KameletBindingDSLSpecification; @@ -58,8 +57,7 @@ public ParseResult deepParse(final String input) { List steps = new ArrayList<>(); Map md = new LinkedHashMap<>(); try { - ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); - KameletBinding binding = yamlMapper.readValue(input, KameletBinding.class); + KameletBinding binding = KamelHelper.YAML_MAPPER.readValue(input, KameletBinding.class); processMetadata(md, binding.getMetadata()); processSpec(steps, binding.getSpec()); diff --git a/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/kamelet/KameletStepParserService.java b/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/kamelet/KameletStepParserService.java index e2259f0ea..2da899507 100644 --- a/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/kamelet/KameletStepParserService.java +++ b/camel-support/src/main/java/io/kaoto/backend/camel/service/step/parser/kamelet/KameletStepParserService.java @@ -14,13 +14,12 @@ import org.jboss.logging.Logger; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import io.fabric8.kubernetes.api.model.AnyType; import io.fabric8.kubernetes.api.model.ObjectMeta; import io.kaoto.backend.api.metadata.catalog.StepCatalog; import io.kaoto.backend.api.service.step.parser.StepParserService; +import io.kaoto.backend.camel.KamelHelper; import io.kaoto.backend.camel.model.deployment.kamelet.FlowStep; import io.kaoto.backend.camel.model.deployment.kamelet.Kamelet; import io.kaoto.backend.camel.model.deployment.kamelet.KameletSpec; @@ -88,8 +87,7 @@ public ParseResult deepParse(final String input) { List steps = new ArrayList<>(); try { - ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); - Kamelet kamelet = yamlMapper.readValue(input, Kamelet.class); + Kamelet kamelet = KamelHelper.YAML_MAPPER.readValue(input, Kamelet.class); processMetadata(res, kamelet.getMetadata()); processSpec(steps, res, kamelet.getSpec()); diff --git a/camel-support/src/test/java/io/kaoto/backend/camel/model/deployment/kamelet/step/marshal/SerializationTest.java b/camel-support/src/test/java/io/kaoto/backend/camel/model/deployment/kamelet/step/marshal/SerializationTest.java index 2589721a7..91feeb468 100644 --- a/camel-support/src/test/java/io/kaoto/backend/camel/model/deployment/kamelet/step/marshal/SerializationTest.java +++ b/camel-support/src/test/java/io/kaoto/backend/camel/model/deployment/kamelet/step/marshal/SerializationTest.java @@ -1,19 +1,21 @@ package io.kaoto.backend.camel.model.deployment.kamelet.step.marshal; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import io.kaoto.backend.camel.service.deployment.generator.kamelet.KameletRepresenter; -import io.kaoto.backend.camel.model.deployment.kamelet.step.MarshalFlowStep; -import io.kaoto.backend.camel.model.deployment.kamelet.step.dataformat.DataFormat; -import io.quarkus.test.junit.QuarkusTest; +import java.util.HashMap; + import org.junit.jupiter.api.Test; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; -import java.util.HashMap; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectReader; + +import io.kaoto.backend.camel.KamelHelper; +import io.kaoto.backend.camel.model.deployment.kamelet.step.MarshalFlowStep; +import io.kaoto.backend.camel.model.deployment.kamelet.step.dataformat.DataFormat; +import io.kaoto.backend.camel.service.deployment.generator.kamelet.KameletRepresenter; +import io.quarkus.test.junit.QuarkusTest; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -27,19 +29,17 @@ void yaml() throws JsonProcessingException { Yaml yaml = new Yaml( new Constructor(new LoaderOptions()), new KameletRepresenter()); - ObjectMapper yamlMapper = - new ObjectMapper(new YAMLFactory()) - .configure( - DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, - false); + + ObjectReader reader = KamelHelper.YAML_MAPPER.readerFor(MarshalFlowStep.class) + .without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); var step = getMarshalFlowStep(); var yamlstring = yaml.dumpAsMap(step); - compareMarshalSteps(step, yamlMapper.readValue(yamlstring, MarshalFlowStep.class)); + compareMarshalSteps(step, reader.readValue(yamlstring)); step = getMarshalFlowStep2(); yamlstring = yaml.dumpAsMap(step); - compareMarshalSteps(step, yamlMapper.readValue(yamlstring, MarshalFlowStep.class)); + compareMarshalSteps(step, reader.readValue(yamlstring)); } private void compareMarshalSteps(final MarshalFlowStep step, diff --git a/catalog/src/main/java/io/kaoto/backend/api/metadata/catalog/AbstractCatalog.java b/catalog/src/main/java/io/kaoto/backend/api/metadata/catalog/AbstractCatalog.java index 894303980..dedaa3488 100644 --- a/catalog/src/main/java/io/kaoto/backend/api/metadata/catalog/AbstractCatalog.java +++ b/catalog/src/main/java/io/kaoto/backend/api/metadata/catalog/AbstractCatalog.java @@ -1,5 +1,11 @@ package io.kaoto.backend.api.metadata.catalog; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import org.jboss.logging.Logger; + import io.kaoto.backend.metadata.MetadataCatalog; import io.kaoto.backend.metadata.ParseCatalog; import io.kaoto.backend.metadata.catalog.InMemoryCatalog; @@ -7,11 +13,6 @@ import io.kaoto.backend.model.Metadata; import io.opentelemetry.instrumentation.annotations.WithSpan; import jakarta.annotation.PostConstruct; -import org.jboss.logging.Logger; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CompletableFuture; /** * 🐱class AbstractCatalog @@ -65,8 +66,10 @@ public CompletableFuture waitForWarmUp() { public void warmUpCatalog() { log.debug("Warming up catalog."); final var time = System.currentTimeMillis(); - final List> futureSteps = new ArrayList<>(); - loadParsers().stream().parallel().forEach(parser -> futureSteps.add(addCatalog(parser))); + final List> futureSteps = + loadParsers().stream().parallel() + .map(this::addCatalog) + .toList(); waitingForWarmUp = CompletableFuture.allOf(futureSteps.toArray(new CompletableFuture[0])); waitingForWarmUp