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 #3809] Allowing additional properties on user input #3810

Merged
merged 1 commit into from
Dec 10, 2024
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 @@ -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
Loading