Claus Ibsen created CAMEL-27676:
-----------------------------------

             Summary: camel-mongodb-gridfs: writeConcern/readPreference NPE, 
closes registry MongoClient, op enums
                 Key: CAMEL-27676
                 URL: https://issues.apache.org/jira/browse/CAMEL-27676
             Project: Camel
          Issue Type: Bug
          Components: camel-mongodb-gridfs
            Reporter: Claus Ibsen
             Fix For: 4.24.0


Found while improving the option descriptions of camel-mongodb-gridfs for 
CAMEL-23832 (PR (commit 
https://github.com/apache/camel/commit/193b7b30c2bc3852c7cee324b165d939a195b076)).
These are separate problems of the component; the descriptions commit only 
documents the current behavior.

h4. 1. Setting writeConcern or readPreference fails the endpoint with a 
NullPointerException
{{GridFsEndpoint.doInit()}} calls {{setWriteReadOptionsOnConnection()}}, which 
calls {{db.withWriteConcern(writeConcern)}} and 
{{db.withReadPreference(readPreference)}}. At that time {{db}} is still 
{{null}}: it is set only later, in {{initializeConnection()}}, which 
{{createProducer()}} and {{createConsumer()}} call. So an endpoint with 
{{writeConcern}} or {{readPreference}} fails to start with a 
{{NullPointerException}}.
Even without the exception the options would have no effect, because 
{{initializeConnection()}} replaces {{db}} with 
{{mongoConnection.getDatabase(database)}} and creates the bucket from it.
Proposed change: apply the write concern and the read preference in 
{{initializeConnection()}}, after {{getDatabase(database)}} and before 
{{GridFSBuckets.create(db, ...)}}, and add a test for each option.

h4. 2. The endpoint closes the MongoClient bean that it does not own
{{GridFsEndpoint.doShutdown()}} calls {{mongoConnection.close()}} on the client 
that {{doInit()}} looked up from the registry by the {{connectionBean}} name. 
The client belongs to the application (a Spring or Quarkus bean, or a bean 
bound by the user) and can be shared with other endpoints, other components or 
application code, which then fail with "state should be: open" when one gridfs 
endpoint shuts down (for example when its route is removed).
{{camel-mongodb}} closes only the client it created 
({{MongoDbEndpoint.doStop()}}).
Proposed change: do not close the client looked up from the registry.

h4. 3. The operation option has no enums and no defaultValue in the metadata
{{GridFsEndpoint.operation}} is {{@UriParam(label = "producer")}} without 
{{enums}} and {{defaultValue}}, while {{GridFsProducer.process()}} supports 
only {{create}}, {{remove}}, {{findOne}}, {{listAll}} and {{count}}, and uses 
{{create}} when neither the option nor the {{CamelGridFsOperation}} header is 
set. Tools and the catalog therefore cannot offer or validate the values.
Proposed change: add {{enums = "create,remove,findOne,listAll,count"}} and 
{{defaultValue = "create"}}.

_Claude Code on behalf of davsclaus_



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to