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

   Fixes [CAMEL-26180](https://issues.apache.org/jira/browse/CAMEL-26180).
   
   ### 1. A tag value that is not a Map fails with a raw ClassCastException
   
   **Wrong:** `Plc4XEndpoint.buildPlcWriteRequest` cast the value of each tag 
to a `Map` without a check. A body such as `{t1: 42}` failed with a 
`ClassCastException`, and an empty Map (`{t1: {}}`) with a 
`NoSuchElementException`.
   
   **Changed:** each tag value is checked. A value that is not a `Map` of 
exactly one `String` address now fails the exchange with a 
`PlcInvalidTagException` that names the tag and what it got, such as `Tag t1: 
the value must be a Map of one address of the tag to the value to write, such 
as {"%DB1:DINT": 21}, but a java.lang.Integer is invalid`. The message for a 
body that is not a `Map` at all now also says what it got (it read `The body 
must contain a Map<String,Map<String,Object> invalid` before).
   
   ### 2. A value Map with several addresses writes only the first
   
   **Wrong:** only `keySet().iterator().next()` of the inner Map was written; 
the other addresses of the tag were ignored without notice (and with a 
`HashMap`, which one is written was not even defined).
   
   **Changed:** a tag with several addresses now fails with a 
`PlcInvalidTagException` and nothing is written. Adding each entry is not 
possible: a write request holds one address per tag name, so the extra 
addresses would need invented tag names. The doc says to use one tag per 
address.
   
   ### 3. The polling consumer throws out of receive() for an unknown tag
   
   **Wrong:** `PlcReadResponse.getObject` throws a `PlcRuntimeException` for a 
tag whose response code is not `OK` (checked in the PLC4X 1.0.0 
`DefaultPlcReadResponse`), and `Plc4XPollingConsumer.doReceive` did not catch 
it, so it escaped from `receive()`. The same went for an invalid tag address, 
which fails when the read request is built.
   
   **Changed:** `PlcRuntimeException` is caught with `PlcConnectionException` 
and set on the returned exchange, as for the other read failures. The 
event-driven consumer already reports it to its exception handler, and the 
triggered consumer checks the response codes before it takes the values, so 
they are not changed.
   
   ### 4. A trigger written with parentheses fails, and the trigger syntax is 
not documented
   
   **Not changed: no longer applies on main.** CAMEL-25617 (#28387, PLC4X 
1.0.0) removed the PLC4X scraper and the code that wrapped the trigger in 
`(...)==(true)`. The `trigger` option is now a plain tag address read in the 
same request as the tags, and the component doc describes triggered reading 
with an example (`trigger: "coil:1"`). A trigger in parentheses is not a valid 
address, and like any other invalid address it fails each poll with a clear 
error reported to the exception handler. Stripping parentheses would only add a 
second, undocumented syntax, so I left it.
   
   ### Tests
   
   - `Plc4XEndpointTest`: a valid write request adds the address and value of 
each tag; a tag that is not a Map, a Map with several addresses (nothing is 
built), an empty Map, and an address that is not a String each fail with 
`PlcInvalidTagException`.
   - `Plc4XFailureTest`: a body that is not a Map fails with a message naming 
its type; the polling consumer fails the exchange for a tag that cannot be read 
and for an invalid tag address.
   - All 7 new tests failed before the fix (the two polling consumer tests with 
the exception thrown out of `receive()`). The module suite passes: 51 tests.
   
   ### Docs and generated files
   
   - `plc4x-component.adoc`: the producer section now describes the body shape, 
what fails, and has a YAML example; the polling consumer section says a failed 
read fails the exchange.
   - The `producerIn` `@BodyTypes` text now says a tag with no or several 
addresses fails; `plc4x.json` and the catalog copies of the JSON and the doc 
are regenerated.
   - Upgrade guide 4.24: new `camel-plc4x` entry, since a tag with several 
addresses now fails instead of writing one of them, and the exception types 
change.
   
   _Claude Code on behalf of Claus Ibsen (davsclaus)_
   
   🤖 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