This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-kamelets.git
The following commit(s) were added to refs/heads/main by this push:
new e922f758e Fix #3088: extract-field-action gives a text value as plain
text, and an object as JSON (#3253)
e922f758e is described below
commit e922f758eae8507b28f22afc8e10896d46c8cfe3
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 19 10:56:19 2026 +0100
Fix #3088: extract-field-action gives a text value as plain text, and an
object as JSON (#3253)
The type of the value decides the body, not the Content-Type of the
incoming message: a text, a number or a boolean comes out as the plain
value with Content-Type text/plain, an object or an array as JSON text
with Content-Type application/json. A missing or null field gives an
empty body and removes Content-Type. With headerOutput the body and its
Content-Type are left as they were. trimField is deprecated and ignored.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
kamelets/extract-field-action.kamelet.yaml | 76 +++++++++++++++++-----
.../main/resources/META-INF/kamelets-index.json | 2 +-
.../kamelets/extract-field-action.kamelet.yaml | 76 +++++++++++++++++-----
...tract-field-action-object-route.citrus.it.yaml} | 14 ++--
.../extract-field-action-route.citrus.it.yaml | 6 +-
.../transformation/extract-field-action-route.yaml | 2 +-
6 files changed, 135 insertions(+), 41 deletions(-)
diff --git a/kamelets/extract-field-action.kamelet.yaml
b/kamelets/extract-field-action.kamelet.yaml
index dab1c9e15..e0dddf317 100644
--- a/kamelets/extract-field-action.kamelet.yaml
+++ b/kamelets/extract-field-action.kamelet.yaml
@@ -37,9 +37,9 @@ spec:
Input: a JSON object, as JSON text or as parsed JSON (a Map or a Jackson
`JsonNode`). The field must be at the top level of the object. A JSON array, or
a body that is not JSON, fails.
- Output: the body is the value of the field. When the `Content-Type`
header is `application/json`, it is JSON text, so a text value keeps its quotes
(`"Ann"`) unless `trimField` is true; otherwise it is the value itself, such as
the text `Ann`. When `headerOutput` is true, the value goes into the header
`CamelKameletsExtractFieldName`, or the header named by `headerOutputName`, and
the body is left as it was.
+ Output: the body is the value of the field. A text, a number or a
boolean is the plain value as text, such as `Ann` or `42`, with `Content-Type:
text/plain`. An object or an array is JSON text, such as `{"name":"Ann"}`, with
`Content-Type: application/json`. When `headerOutput` is true, the value goes
into the header `CamelKameletsExtractFieldName`, or the header named by
`headerOutputName`, as it is in the JSON: a text, a number, a boolean, or a
Java `Map` or `List` for an object [...]
- Good to know: when the object does not have the field, the body or the
header is empty (the JSON text `null` when the `Content-Type` header is
`application/json`); the message does not fail.
+ Good to know: when the object does not have the field, or the field is
`null`, the body is empty and the `Content-Type` header is removed, or with
`headerOutput` the header is empty; the message does not fail. The
`Content-Type` of the incoming message does not change how the body is read.
required:
- field
properties:
@@ -66,19 +66,18 @@ spec:
default: false
trimField:
title: Trim Field
- description: If true, the body is the plain value, such as `Ann`,
instead of JSON text such as `"Ann"`. It changes something only when the
`Content-Type` header is `application/json`, and that header is then left as it
is.
+ description: Deprecated and ignored. A text value now always comes out
as the plain value, such as `Ann`, and an object or an array as JSON text. Kept
so that a configuration that sets it still starts.
type: boolean
default: false
+ deprecated: true
type: object
dataTypes:
- in:
+ out:
headers:
Content-Type:
title: Content Type
- description: When it is `application/json`, the body is read as JSON
text and the value is written as JSON text, unless `trimField` is true.
+ description: '`text/plain` when the value is a text, a number or a
boolean, and `application/json` when it is an object or an array. Removed when
the field is missing or `null`. Left as it was when `headerOutput` is true.'
type: string
- out:
- headers:
CamelKameletsExtractFieldName:
title: Extracted Field
description: The value of the field, when `headerOutput` is true and
`headerOutputName` is not set.
@@ -100,39 +99,86 @@ spec:
from:
uri: kamelet:source
steps:
+ # does the value replace the body, or go into a header
+ - setProperty:
+ name: extractFieldToBody
+ expression:
+ constant:
+ expression: "true"
- choice:
when:
- expression:
simple:
- expression: "${header[Content-Type]} == 'application/json'"
+ expression: "'{{headerOutput}}' == 'true'"
steps:
- setProperty:
- name: deserialized
+ name: extractFieldToBody
expression:
constant:
- expression: "true"
- - unmarshal:
- json:
- unmarshalType: tools.jackson.databind.JsonNode
+ expression: "false"
+ - choice:
+ when:
+ - expression:
+ simple:
+ expression: "'{{strictHeaderCheck}}' == 'true'
&& '{{headerOutputName}}' == 'none' && ${header.CamelKameletsExtractFieldName}
!= null"
+ steps:
+ - setProperty:
+ name: extractFieldToBody
+ expression:
+ constant:
+ expression: "true"
+ - expression:
+ simple:
+ expression: "'{{strictHeaderCheck}}' == 'true'
&& '{{headerOutputName}}' != 'none' && ${header[{{headerOutputName}}]} != null"
+ steps:
+ - setProperty:
+ name: extractFieldToBody
+ expression:
+ constant:
+ expression: "true"
- process:
ref: "{{extractField}}"
+ - removeProperty:
+ name: trimField
- choice:
when:
- expression:
simple:
- expression: "${exchangeProperty[trimField]} == 'false'"
+ expression: "${exchangeProperty.extractFieldToBody} ==
'true'"
steps:
- choice:
when:
+ # the field is missing, or null
+ - expression:
+ simple:
+ expression: "${body} == null"
+ steps:
+ - removeHeader:
+ name: Content-Type
+ # an object or an array stays JSON
- expression:
simple:
- expression: "${exchangeProperty[deserialized]}
== 'true'"
+ expression: "${body} is 'java.util.Map' ||
${body} is 'java.util.List'"
steps:
- marshal:
json:
unmarshalType:
tools.jackson.databind.JsonNode
+ - convertBodyTo:
+ type: String
- setHeader:
name: Content-Type
expression:
constant:
expression: application/json
+ # a text, a number or a boolean is the plain value
+ otherwise:
+ steps:
+ - convertBodyTo:
+ type: String
+ - setHeader:
+ name: Content-Type
+ expression:
+ constant:
+ expression: text/plain
+ - removeProperty:
+ name: extractFieldToBody
diff --git
a/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
b/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
index 580c10f71..9eaf095fa 100644
--- a/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
+++ b/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
@@ -735,7 +735,7 @@
"supportLevel" : "Stable",
"title" : "Extract Field Action",
"summary" : "Replaces the body of each message that is a JSON object with
the value of one of its fields, or copies that value into a header.",
- "description" : "Replaces the body of each message that is a JSON object
with the value of one of its fields, or copies that value into a header.\n\nUse
it to keep only the part of a message you need, such as the `payload` of an
event or the `email` of a customer, or, with `headerOutput`, to have that value
in a header for a later step while the body stays as it is. To do the opposite,
and wrap a body in a field, use `hoist-field-action`.\n\nInput: a JSON object,
as JSON text or as p [...]
+ "description" : "Replaces the body of each message that is a JSON object
with the value of one of its fields, or copies that value into a header.\n\nUse
it to keep only the part of a message you need, such as the `payload` of an
event or the `email` of a customer, or, with `headerOutput`, to have that value
in a header for a later step while the body stays as it is. To do the opposite,
and wrap a body in a field, use `hoist-field-action`.\n\nInput: a JSON object,
as JSON text or as p [...]
"group" : "Actions",
"namespace" : "Transformation",
"provider" : "Apache Software Foundation"
diff --git
a/library/camel-kamelets/src/main/resources/kamelets/extract-field-action.kamelet.yaml
b/library/camel-kamelets/src/main/resources/kamelets/extract-field-action.kamelet.yaml
index dab1c9e15..e0dddf317 100644
---
a/library/camel-kamelets/src/main/resources/kamelets/extract-field-action.kamelet.yaml
+++
b/library/camel-kamelets/src/main/resources/kamelets/extract-field-action.kamelet.yaml
@@ -37,9 +37,9 @@ spec:
Input: a JSON object, as JSON text or as parsed JSON (a Map or a Jackson
`JsonNode`). The field must be at the top level of the object. A JSON array, or
a body that is not JSON, fails.
- Output: the body is the value of the field. When the `Content-Type`
header is `application/json`, it is JSON text, so a text value keeps its quotes
(`"Ann"`) unless `trimField` is true; otherwise it is the value itself, such as
the text `Ann`. When `headerOutput` is true, the value goes into the header
`CamelKameletsExtractFieldName`, or the header named by `headerOutputName`, and
the body is left as it was.
+ Output: the body is the value of the field. A text, a number or a
boolean is the plain value as text, such as `Ann` or `42`, with `Content-Type:
text/plain`. An object or an array is JSON text, such as `{"name":"Ann"}`, with
`Content-Type: application/json`. When `headerOutput` is true, the value goes
into the header `CamelKameletsExtractFieldName`, or the header named by
`headerOutputName`, as it is in the JSON: a text, a number, a boolean, or a
Java `Map` or `List` for an object [...]
- Good to know: when the object does not have the field, the body or the
header is empty (the JSON text `null` when the `Content-Type` header is
`application/json`); the message does not fail.
+ Good to know: when the object does not have the field, or the field is
`null`, the body is empty and the `Content-Type` header is removed, or with
`headerOutput` the header is empty; the message does not fail. The
`Content-Type` of the incoming message does not change how the body is read.
required:
- field
properties:
@@ -66,19 +66,18 @@ spec:
default: false
trimField:
title: Trim Field
- description: If true, the body is the plain value, such as `Ann`,
instead of JSON text such as `"Ann"`. It changes something only when the
`Content-Type` header is `application/json`, and that header is then left as it
is.
+ description: Deprecated and ignored. A text value now always comes out
as the plain value, such as `Ann`, and an object or an array as JSON text. Kept
so that a configuration that sets it still starts.
type: boolean
default: false
+ deprecated: true
type: object
dataTypes:
- in:
+ out:
headers:
Content-Type:
title: Content Type
- description: When it is `application/json`, the body is read as JSON
text and the value is written as JSON text, unless `trimField` is true.
+ description: '`text/plain` when the value is a text, a number or a
boolean, and `application/json` when it is an object or an array. Removed when
the field is missing or `null`. Left as it was when `headerOutput` is true.'
type: string
- out:
- headers:
CamelKameletsExtractFieldName:
title: Extracted Field
description: The value of the field, when `headerOutput` is true and
`headerOutputName` is not set.
@@ -100,39 +99,86 @@ spec:
from:
uri: kamelet:source
steps:
+ # does the value replace the body, or go into a header
+ - setProperty:
+ name: extractFieldToBody
+ expression:
+ constant:
+ expression: "true"
- choice:
when:
- expression:
simple:
- expression: "${header[Content-Type]} == 'application/json'"
+ expression: "'{{headerOutput}}' == 'true'"
steps:
- setProperty:
- name: deserialized
+ name: extractFieldToBody
expression:
constant:
- expression: "true"
- - unmarshal:
- json:
- unmarshalType: tools.jackson.databind.JsonNode
+ expression: "false"
+ - choice:
+ when:
+ - expression:
+ simple:
+ expression: "'{{strictHeaderCheck}}' == 'true'
&& '{{headerOutputName}}' == 'none' && ${header.CamelKameletsExtractFieldName}
!= null"
+ steps:
+ - setProperty:
+ name: extractFieldToBody
+ expression:
+ constant:
+ expression: "true"
+ - expression:
+ simple:
+ expression: "'{{strictHeaderCheck}}' == 'true'
&& '{{headerOutputName}}' != 'none' && ${header[{{headerOutputName}}]} != null"
+ steps:
+ - setProperty:
+ name: extractFieldToBody
+ expression:
+ constant:
+ expression: "true"
- process:
ref: "{{extractField}}"
+ - removeProperty:
+ name: trimField
- choice:
when:
- expression:
simple:
- expression: "${exchangeProperty[trimField]} == 'false'"
+ expression: "${exchangeProperty.extractFieldToBody} ==
'true'"
steps:
- choice:
when:
+ # the field is missing, or null
+ - expression:
+ simple:
+ expression: "${body} == null"
+ steps:
+ - removeHeader:
+ name: Content-Type
+ # an object or an array stays JSON
- expression:
simple:
- expression: "${exchangeProperty[deserialized]}
== 'true'"
+ expression: "${body} is 'java.util.Map' ||
${body} is 'java.util.List'"
steps:
- marshal:
json:
unmarshalType:
tools.jackson.databind.JsonNode
+ - convertBodyTo:
+ type: String
- setHeader:
name: Content-Type
expression:
constant:
expression: application/json
+ # a text, a number or a boolean is the plain value
+ otherwise:
+ steps:
+ - convertBodyTo:
+ type: String
+ - setHeader:
+ name: Content-Type
+ expression:
+ constant:
+ expression: text/plain
+ - removeProperty:
+ name: extractFieldToBody
diff --git
a/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-route.citrus.it.yaml
b/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-object-route.citrus.it.yaml
similarity index 88%
copy from
tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-route.citrus.it.yaml
copy to
tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-object-route.citrus.it.yaml
index d5db614dd..490f7763f 100644
---
a/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-route.citrus.it.yaml
+++
b/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-object-route.citrus.it.yaml
@@ -15,12 +15,10 @@
# limitations under the License.
# ---------------------------------------------------------------------------
-name: extract-field-action-route-test
+name: extract-field-action-object-route-test
variables:
- name: "field.name"
- value: "subject"
- - name: "field.value"
- value: "Camel rocks!"
+ value: "customer"
actions:
- createVariables:
variables:
@@ -41,7 +39,7 @@ actions:
value: "${field.name}"
- name: "input"
value: >-
- { \"id\": \"citrus:randomUUID()\", \"${field.name}\":
\"${field.value}\" }
+ { \"id\": \"citrus:randomUUID()\", \"${field.name}\": {
\"name\": \"Ann\", \"vip\": true } }
# Verify Http request
- http:
@@ -49,9 +47,11 @@ actions:
receiveRequest:
POST:
path: "/result"
+ headers:
+ - name: Content-Type
+ value: "@startsWith('application/json')@"
body:
- data: |
- "${field.value}"
+ data: '{"name":"Ann","vip":true}'
- http:
server: "httpServer"
diff --git
a/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-route.citrus.it.yaml
b/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-route.citrus.it.yaml
index d5db614dd..adbabfeef 100644
---
a/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-route.citrus.it.yaml
+++
b/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-route.citrus.it.yaml
@@ -49,9 +49,11 @@ actions:
receiveRequest:
POST:
path: "/result"
+ headers:
+ - name: Content-Type
+ value: "@startsWith('text/plain')@"
body:
- data: |
- "${field.value}"
+ data: "${field.value}"
- http:
server: "httpServer"
diff --git
a/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-route.yaml
b/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-route.yaml
index 326306410..248ec3f1e 100644
---
a/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-route.yaml
+++
b/tests/camel-kamelets-itest/src/test/resources/transformation/extract-field-action-route.yaml
@@ -19,7 +19,7 @@
from:
uri: "kamelet:timer-source"
parameters:
- period: 10000
+ repeatCount: 1
contentType: "application/json"
message: "{{input}}"
steps: