Skip to content

Commit

Permalink
[Fix #3809] Allowing additional properties on user input (#3810)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado authored Dec 10, 2024
1 parent 8dde7b8 commit af492cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ public JsonNodeModel(Object workflowdata) {
this(null, workflowdata);
}

public JsonNodeModel(Object workflowdata, Map<String, Object> additionalProperties) {
this(null, workflowdata, additionalProperties);
}

public JsonNodeModel(String id, Object workflowdata) {
this(id, workflowdata, Collections.emptyMap());
}

public JsonNodeModel(String id, Object workflowdata, Map<String, Object> additionalProperties) {
this.id = id;
if (workflowdata instanceof JsonNode) {
this.workflowdata = (JsonNode) workflowdata;
Expand All @@ -59,6 +67,7 @@ public JsonNodeModel(String id, Object workflowdata) {
this.workflowdata = workflowdata == null ? mapper.createObjectNode() : mapper.convertValue(workflowdata, JsonNode.class);
}
this.input = this.workflowdata.deepCopy();
this.additionalProperties = additionalProperties == null ? Collections.emptyMap() : additionalProperties;
}

public String getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,42 @@
*/
package org.kie.kogito.serverless.workflow.models;

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

import org.kie.kogito.MapInput;
import org.kie.kogito.MapInputId;
import org.kie.kogito.MapOutput;
import org.kie.kogito.MappableToModel;
import org.kie.kogito.Model;
import org.kie.kogito.jackson.utils.ObjectMapperFactory;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class JsonNodeModelInput implements Model, MapInput, MapInputId, MapOutput, MappableToModel<JsonNodeModel> {

private Object workflowdata;
private Map<String, Object> additionalProperties;

public Object getWorkflowdata() {
return workflowdata;
}

@JsonAnySetter
public void setData(String key, JsonNode value) {
if (workflowdata == null) {
workflowdata = ObjectMapperFactory.listenerAware().createObjectNode();
}
if (workflowdata instanceof ObjectNode) {
((ObjectNode) workflowdata).set(key, value);
if (additionalProperties == null) {
additionalProperties = new LinkedHashMap<>();
}
additionalProperties.put(key, value);
}

@Override
public JsonNodeModel toModel() {
return new JsonNodeModel(workflowdata);
if (workflowdata == null) {
workflowdata = additionalProperties;
additionalProperties = Collections.emptyMap();
}
return new JsonNodeModel(workflowdata, additionalProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void testExpressionRest() {
.contentType(ContentType.JSON)
.accept(ContentType.JSON)
.header("pepe", "pepa")
.body("{\"workflowdata\":{\"numbers\":[{\"x\":2, \"y\": 1},{\"x\":4, \"y\": 3}]}}").when()
.body("{\"workflowdata\":{\"numbers\":[{\"x\":2, \"y\": 1},{\"x\":4, \"y\": 3}]}, \"randomAdditionalProperty\":\"Im ignored in runtimes but will be visible on data index\"}").when()
.post("/expression")
.then()
.statusCode(201)
Expand Down

0 comments on commit af492cf

Please sign in to comment.