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

Enhance insert-field-action.kamelet #1635

Merged
merged 1 commit into from
Sep 18, 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
2 changes: 1 addition & 1 deletion kamelets/insert-field-action.kamelet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ spec:
description: |-
Adds a custom field with a simple language parsed value to the message in transit.

The extract field action expected an application/json content type.
The insert field action expected an application/json content type.

If for example you have an array like '{ "foo":"John", "bar":30 }' and your action has been configured with field as 'element' and value as 'hello', you'll get '{ "foo":"John", "bar":30, "element":"hello" }'
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.Processor;
import org.apache.camel.support.LanguageSupport;

public class InsertField implements Processor {

Expand Down Expand Up @@ -51,15 +52,22 @@ public void process(Exchange ex) throws InvalidPayloadException {
throw new InvalidPayloadException(ex, JsonNode.class);
}

String resolvedValue;
if (LanguageSupport.hasSimpleFunction(value)) {
resolvedValue = ex.getContext().resolveLanguage("simple").createExpression(value).evaluate(ex, String.class);
} else {
resolvedValue = value;
}

switch (body.getNodeType()) {
case ARRAY:
((ArrayNode) body).add(value);
((ArrayNode) body).add(resolvedValue);
break;
case OBJECT:
((ObjectNode) body).put(field, value);
((ObjectNode) body).put(field, resolvedValue);
break;
default:
((ObjectNode) body).put(field, value);
((ObjectNode) body).put(field, resolvedValue);
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ spec:
description: |-
Adds a custom field with a simple language parsed value to the message in transit.

The extract field action expected an application/json content type.
The insert field action expected an application/json content type.

If for example you have an array like '{ "foo":"John", "bar":30 }' and your action has been configured with field as 'element' and value as 'hello', you'll get '{ "foo":"John", "bar":30, "element":"hello" }'
required:
Expand Down
Loading