saravanakumar1987 opened a new pull request, #30205:
URL: https://github.com/apache/camel/pull/30205

   # Description
   
   Fixes [CAMEL-27032](https://issues.apache.org/jira/browse/CAMEL-27032).
   
   ## What was wrong
   
   `putEvents` failed with `Field id is required` for a JSON text body and for 
a Map body without the key `id`, before the putEvents request was sent:
   
   - `AlibabaEventBridgeUtils.toCloudEvent` built the event of a JSON text body 
without an id.
   - `MapCloudEventValidator.validateAndBuild` set an id only from the key `id` 
of the Map, and only when that value was not blank.
   - The SDK generates none. In `com.aliyun:eventbridge-client` 1.3.14, 
`EventBuilder.build()` sets the id that it was given, and 
`EventBridgeClient.putEventsWithOptions` validates each event before it calls 
`doRequest` (`Common.validateModel`, `EventBridgeClient.java:224`). 
`CloudEvent.id` has `@Validation(required = true)`, so `TeaModel.validate` of 
`com.aliyun:tea` 1.4.3 throws a `ValidateException` (`TeaModel.java:276`, 
wrapped by its own catch in a second `ValidateException` at 
`TeaModel.java:290`), which `Common.validateModel` of `com.aliyun:tea-util` 
0.2.28 throws again as a `TeaUtilException` with the message `Field id is 
required`.
   
   So every JSON text body failed, although the first example in the Examples 
section of the doc page is a JSON text body, and every Map body without an id 
failed, although the doc page says that an omitted id is an auto-generated 
UUID. The tests of the module used only mock clients, whose `putEvents` does 
not run that validation.
   
   Nothing in the history shows that the id was left out on purpose: no version 
of the module generated one, and the commit that added the key `id` of a Map 
body (b9c1f530eb18, CAMEL-24373) also added that line of the doc page. So this 
change reverts no earlier decision. The defect is in 4.23.0, the first release 
of the component.
   
   ## The change
   
   - `AlibabaEventBridgeUtils.toCloudEvent` gives the event of a JSON text body 
a generated id, `UUID.randomUUID().toString()`.
   - `MapCloudEventValidator.validateAndBuild` gives the event of a Map body a 
generated id when it resolves no id from the key `id`: when the key is missing 
or its value is null, and, with `validateEventSpec` false, when its value is 
blank. A Map body with an id keeps it. With `validateEventSpec` true, the 
default, a blank id still fails with `Event 'id' cannot be blank when 
provided`, before the SDK is called.
   - A `CloudEvent` body is still sent as it is.
   - In a `List` body, each JSON text and each Map gets its own id.
   
   The id is a random UUID, as the doc page says and as `camel-azure-eventgrid` 
generates for a CloudEvent without an id, so that the events of a List body get 
distinct ids. It is generated each time the producer builds the events, so a 
redelivery of the exchange sends new ids. A Map body with the key `id`, or a 
CloudEvent body, keeps its id.
   
   The upgrade guide of 4.23 tells routes whose Map bodies have an empty `id` 
to set `validateEventSpec=false`. Before this change, such an event then failed 
with `Field id is required`; now it gets a generated id.
   
   No option, header or default value changes, and no generated file changes. 
The doc page is not changed: its row for the key `id` already says that an 
omitted id is an auto-generated UUID, and its "Cannot be blank if provided" is 
the check of `validateEventSpec`, which is on by default, as "Must be `1.0` if 
specified" of the key `specversion` is. There is no upgrade-guide entry: before 
this change, these bodies always failed.
   
   No text of `AlibabaEventBridgeEndpoint` or `AlibabaEventBridgeHeaders` needs 
to change for this fix, and neither file is changed, as the open 
apache/camel#30193 changes their texts.
   
   ## Tests
   
   The new `PutEventsEventIdTest` sends exchanges through a route to 
`alibaba-eventbridge:putEvents` with a real `EventBridgeClient`, wrapped in a 
Mockito spy whose `doRequest` is replaced. So `putEvents` validates the events 
as in production, and no request is sent:
   
   - `aJsonTextBodyGetsAnId` and `aMapBodyWithoutAnIdGetsAnId`: the event 
passes the validation of the SDK, and its id is a generated UUID.
   - `aMapBodyKeepsItsId`: the id of the Map is kept.
   - `eachEventOfABatchGetsAnIdOfItsOwn`: a List of two JSON texts and a Map 
gives three events with three different generated UUIDs.
   - `aBlankIdGetsAnIdWhenValidateEventSpecIsFalse`: a blank id is replaced by 
a generated UUID when the header turns `validateEventSpec` off.
   - `aBlankIdStillFailsWhenValidateEventSpecIsTrue`: a blank id still fails, 
and `doRequest` is not called.
   - `aCloudEventBodyIsSentAsItIs`: the CloudEvent of the body is the event 
that the SDK checks, with its own id.
   - `aCloudEventBodyWithoutAnIdIsNotGivenOne`: the CloudEvent keeps no id, the 
SDK still rejects it with `Field id is required`, and `doRequest` is not called.
   
   No test contacts a server.
   
   ## How it was verified
   
   - With the new test and without the fix (the two changed files restored from 
`main`), `PutEventsEventIdTest` ran 8 tests with 4 errors: 
`aJsonTextBodyGetsAnId`, `aMapBodyWithoutAnIdGetsAnId`, 
`eachEventOfABatchGetsAnIdOfItsOwn` and 
`aBlankIdGetsAnIdWhenValidateEventSpecIsFalse`, each a 
`CamelExecutionException` caused by `TeaUtilException: Field id is required`, 
thrown by `Common.validateModel` in `EventBridgeClient.putEventsWithOptions` 
(`EventBridgeClient.java:224`), which `EventBridgeClient.putEvents` called from 
`AlibabaEventBridgeProducer.putEvents`. The other 4 passed.
   - With the fix, the 8 tests pass.
   - With the fix, `mvn install -nsu` in the module ran all 44 tests of the 
module with no failure, error or rerun, and changed no generated file.
   
   ## Not changed
   
   - A CloudEvent body without an id still fails with `Field id is required`: 
the producer sends a CloudEvent as it is.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [x] I have run `mvn clean install -Dquickly -DskipTests` from the root 
folder on upstream `main` (27efa340d2df, the parent of this commit), and then, 
with the change, `mvn install -nsu` in 
`components/camel-alibaba/camel-alibaba-eventbridge`, which also runs its code 
generators, the formatter and the import sorter. It changed no generated file, 
so there are no auto-generated changes to commit.
   
   The commands that were run:
   
   ```
   # in components/camel-alibaba/camel-alibaba-eventbridge, with the new test, 
without the fix and then with the fix
   mvn -o -B test -Dtest=PutEventsEventIdTest 
-Dsurefire.failIfNoSpecifiedTests=false -Dsurefire.rerunFailingTestsCount=0
   # with the fix
   mvn install -nsu
   ```
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
   
   The change, the tests and this text were drafted with Claude Code 
(Anthropic), from the ticket, the sources of `com.aliyun:eventbridge-client` 
1.3.14 and `com.aliyun:tea` 1.4.3, and the bytecode of `com.aliyun:tea-util` 
0.2.28. The tests were run as described above. The change and the claims of 
this text were checked by independent AI reviews.
   
   _Claude Code on behalf of saravanakumar1987_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to