Skip to content

Commit

Permalink
fix: Removing properties from (un)marshal EIP
Browse files Browse the repository at this point in the history
Because frontend does not handle arrays yet.

Fixes KaotoIO#595
  • Loading branch information
Delawen committed Apr 3, 2023
1 parent c2726ed commit 5b699f4
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ void deepParse() throws IOException {

@ParameterizedTest
@ValueSource(strings = {"route.yaml", "route2-complex-expressions.yaml",
"route3-complex-expressions.yaml", "route-ids.yaml", "route4-pathparams.yaml", "route5-placeholders.yaml"})
"route3-complex-expressions.yaml", "route-ids.yaml", "route4-pathparams.yaml", "route5-placeholders.yaml",
"route6-un-marshal.yaml"})
void deepParseParametrized(String file) throws IOException {
var route = new String(this.getClass().getResourceAsStream(file).readAllBytes(),
StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- from:
uri: timer:null
steps:
- marshal:
data-format:
json:
library: Gson
- unmarshal:
data-format:
json:
property: something
another: whatever
- unmarshal:
xml:
class: classInJava.class
20 changes: 2 additions & 18 deletions kamelet-support/src/main/java/io/kaoto/backend/KamelPopulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import io.kaoto.backend.model.deployment.kamelet.step.ValidateFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.WireTapFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.dataformat.DataFormat;
import io.kaoto.backend.model.parameter.ArrayParameter;
import io.kaoto.backend.model.parameter.Parameter;
import io.kaoto.backend.model.parameter.StringParameter;
import io.kaoto.backend.model.step.Branch;
Expand Down Expand Up @@ -559,24 +558,9 @@ private FlowStep getUnmarshalStep(final Step step) {
}

private void assignParameters(final Step step, final MarshalFlowStep marshal) {
marshal.setDataFormat(new DataFormat());
marshal.getDataFormat().setProperties(new HashMap<>());

for (Parameter p : step.getParameters()) {
if (null == p.getValue()) {
continue;
}

if ("dataformat".equalsIgnoreCase(p.getId())) {
marshal.getDataFormat().setFormat(p.getValue().toString());
} else if ("properties".equalsIgnoreCase(p.getId())) {
for (var param : ((ArrayParameter) p).getValue()) {
if (param instanceof Object[] array) {
marshal.getDataFormat().getProperties().put(array[0].toString(), array[1].toString());
} else if (param instanceof List list) {
marshal.getDataFormat().getProperties().put(list.get(0).toString(), list.get(1).toString());
}
}
if (null != p.getValue() && "dataformat".equalsIgnoreCase(p.getId())) {
marshal.setDataFormat(new DataFormat(p.getValue()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public Map<String, Object> getRepresenterProperties() {
Map<String, Object> step = new HashMap<>();
Map<String, Object> properties = new HashMap<>();
step.put("marshal", properties);
properties.put(dataFormat.getFormat(), dataFormat.getProperties());
if (dataFormat != null) {
properties.put(dataFormat.getFormat(), dataFormat.getProperties());
}
return step;
}

Expand All @@ -48,15 +50,7 @@ public Step getStep(final StepCatalog catalog, final KameletStepParserService ka
protected void assignParameters(final Step res) {
for (var param : res.getParameters()) {
if ("dataformat".equalsIgnoreCase(param.getId())) {
param.setValue(this.getDataFormat().getFormat());
} else if ("properties".equalsIgnoreCase(param.getId())
&& this.getDataFormat().getProperties() != null) {
Object[] array = new Object[this.getDataFormat().getProperties().size()];
int i = 0;
for (var entry : this.getDataFormat().getProperties().entrySet()) {
array[i++] = new Object[]{entry.getKey(), entry.getValue()};
}
param.setValue(array);
param.setValue(this.getDataFormat());
}
}
}
Expand All @@ -66,8 +60,7 @@ public DataFormat getDataFormat() {
return dataFormat;
}

public void setDataFormat(
final DataFormat dataFormat) {
public void setDataFormat(final DataFormat dataFormat) {
this.dataFormat = dataFormat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import java.util.HashMap;
import java.util.Map;

@JsonDeserialize(
using = MarshalDeserializer.class
)
@JsonDeserialize(using = MarshalDeserializer.class)
public class UnmarshalFlowStep extends MarshalFlowStep {
@Serial
private static final long serialVersionUID = 5841231681362382129L;
Expand All @@ -22,7 +20,9 @@ public Map<String, Object> getRepresenterProperties() {
Map<String, Object> step = new HashMap<>();
Map<String, Object> properties = new HashMap<>();
step.put("unmarshal", properties);
properties.put(getDataFormat().getFormat(), getDataFormat().getProperties());
if (getDataFormat() != null) {
properties.put(getDataFormat().getFormat(), getDataFormat().getProperties());
}
return step;
}

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

import java.io.Serial;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class DataFormat implements Serializable {
Expand All @@ -10,7 +11,26 @@ public class DataFormat implements Serializable {
private static final long serialVersionUID = 239784528129L;

private String format;
private Map<String, String> properties;
private Map<String, Object> properties;

public DataFormat() {
//Needed for serialization
}

public DataFormat(Object dataformat) {
this.setProperties(new HashMap<>());
if (dataformat instanceof DataFormat df) {
this.setFormat(df.getFormat());
this.setProperties(df.getProperties());
} else if (dataformat instanceof Map map) {
this.setFormat(String.valueOf(map.getOrDefault("format", "")));

final var potentialProperties = map.getOrDefault("properties", null);
if (potentialProperties instanceof Map) {
this.setProperties((Map<String, Object>) potentialProperties);
}
}
}

public String getFormat() {
return format;
Expand All @@ -20,12 +40,11 @@ public void setFormat(final String format) {
this.format = format;
}

public Map<String, String> getProperties() {
public Map<String, Object> getProperties() {
return properties;
}

public void setProperties(
final Map<String, String> properties) {
public void setProperties(final Map<String, Object> properties) {
this.properties = properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ValueNode;
import io.kaoto.backend.model.deployment.kamelet.step.MarshalFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.UnmarshalFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.dataformat.DataFormat;
import org.jboss.logging.Logger;

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

public class MarshalDeserializer extends JsonDeserializer {
private final Logger log = Logger.getLogger(MarshalDeserializer.class);
Expand All @@ -33,13 +35,8 @@ public Object deserialize(final JsonParser jsonParser,

var field = fields.next();
dataFormat.setFormat(field.getKey());
dataFormat.setProperties(new HashMap<>());
field.getValue().fields().forEachRemaining(e -> {
dataFormat.getProperties().put(
e.getKey(),
e.getValue().asText());
});

dataFormat.setProperties(new LinkedHashMap<>());
extractProperties(dataFormat.getProperties(), field);
}
if (fields.hasNext()) {
log.error("Found a second data format on this marshal? "
Expand All @@ -52,4 +49,23 @@ public Object deserialize(final JsonParser jsonParser,

return step;
}

private static void extractProperties(final Map<String, Object> properties,
final Map.Entry<String, JsonNode> field) {
if (field.getValue() instanceof ValueNode v) {
properties.put(field.getKey(), v.asText());
} else {
field.getValue().fields().forEachRemaining(e -> {
if (e.getValue() instanceof ValueNode v) {
properties.put(e.getKey(), v.asText());
} else {
Map<String, Object> innerProperties = new LinkedHashMap<>();
JsonNode node = e.getValue();
node.fields().forEachRemaining(
stringJsonNodeEntry -> extractProperties(innerProperties, stringJsonNodeEntry));
properties.put(e.getKey(), innerProperties);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void writeDataFormat(final JsonGenerator gen,
if (df.getProperties() != null) {
gen.writeStartObject();
for (var property : df.getProperties().entrySet()) {
gen.writeStringField(property.getKey(), property.getValue());
gen.writeStringField(property.getKey(), property.getValue().toString());
}
gen.writeEndObject();
}
Expand Down

0 comments on commit 5b699f4

Please sign in to comment.