oscerd commented on code in PR #26590: URL: https://github.com/apache/camel/pull/26590#discussion_r4060222992
########## components/camel-elasticsearch/src/test/java/org/apache/camel/component/es/converter/ElasticsearchActionRequestConverterTest.java: ########## @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.es.converter; + +import java.util.Map; + +import co.elastic.clients.elasticsearch.core.IndexRequest; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.component.es.ElasticsearchConstants; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.DefaultExchange; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ElasticsearchActionRequestConverterTest { + + private CamelContext context; + + @BeforeEach + void setUp() { + context = new DefaultCamelContext(); + } + + @AfterEach + void tearDown() { + context.stop(); + } + + private IndexRequest.Builder<Object> preBuiltIndexBuilder() { + return new IndexRequest.Builder<>().index("idx").id("original").document(Map.of("k", "v")); + } + + @Test + void preBuiltIndexBuilderKeepsItsIdWhenHeaderAbsent() throws Exception { + Exchange exchange = new DefaultExchange(context); + + IndexRequest.Builder<?> result + = ElasticsearchActionRequestConverter.toIndexRequestBuilder(preBuiltIndexBuilder(), exchange); + + // no CamelIndexId header -> the caller's id must be preserved, not overwritten with null + assertEquals("original", result.build().id()); + } + + @Test + void preBuiltIndexBuilderHeaderOverridesId() throws Exception { + Exchange exchange = new DefaultExchange(context); + exchange.getIn().setHeader(ElasticsearchConstants.PARAM_INDEX_ID, "fromHeader"); + + IndexRequest.Builder<?> result + = ElasticsearchActionRequestConverter.toIndexRequestBuilder(preBuiltIndexBuilder(), exchange); + + assertEquals("fromHeader", result.build().id()); + } +} Review Comment: Already in the branch as `preBuiltUpdateBuilderKeepsItsIdWhenHeaderAbsent`, essentially as you wrote it. The fix it pins: ```java String id = exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_ID, String.class); if (id != null) { updateReqBuilder.id(id); } return updateReqBuilder; ``` You are right that it can silently regress — the previous code passed the header through unconditionally, so a null cleared a caller-supplied id, and nothing would have noticed. _Claude Code on behalf of @oscerd_ ########## components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/converter/OpensearchActionRequestConverterTest.java: ########## @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.opensearch.converter; + +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.component.opensearch.OpensearchConstants; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.DefaultExchange; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.opensearch.client.opensearch.core.IndexRequest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class OpensearchActionRequestConverterTest { + + private CamelContext context; + + @BeforeEach + void setUp() { + context = new DefaultCamelContext(); + } + + @AfterEach + void tearDown() { + context.stop(); + } + + private IndexRequest.Builder<Object> preBuiltIndexBuilder() { + return new IndexRequest.Builder<>().index("idx").id("original").document(Map.of("k", "v")); + } + + @Test + void preBuiltIndexBuilderKeepsItsIdWhenHeaderAbsent() throws Exception { + Exchange exchange = new DefaultExchange(context); + + IndexRequest.Builder<?> result + = OpensearchActionRequestConverter.toIndexRequestBuilder(preBuiltIndexBuilder(), exchange); + + // no CamelOpensearchIndexId header -> the caller's id must be preserved, not overwritten with null + assertEquals("original", result.build().id()); + } + + @Test + void preBuiltIndexBuilderHeaderOverridesId() throws Exception { + Exchange exchange = new DefaultExchange(context); + exchange.getIn().setHeader(OpensearchConstants.PARAM_INDEX_ID, "fromHeader"); + + IndexRequest.Builder<?> result + = OpensearchActionRequestConverter.toIndexRequestBuilder(preBuiltIndexBuilder(), exchange); + + assertEquals("fromHeader", result.build().id()); + } +} Review Comment: The symmetric OpenSearch test is there too, same name and same shape. Both components carry `preBuiltIndexBuilderKeepsItsIdWhenHeaderAbsent`, `preBuiltIndexBuilderHeaderOverridesId` and `preBuiltUpdateBuilderKeepsItsIdWhenHeaderAbsent`, so the index and update paths are pinned in both directions — id preserved when the header is absent, overridden when it is present. _Claude Code on behalf of @oscerd_ ########## components/camel-elasticsearch/src/test/java/org/apache/camel/component/es/converter/ElasticsearchActionRequestConverterTest.java: ########## @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.es.converter; + +import java.util.Map; + +import co.elastic.clients.elasticsearch.core.IndexRequest; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.component.es.ElasticsearchConstants; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.DefaultExchange; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ElasticsearchActionRequestConverterTest { + + private CamelContext context; + + @BeforeEach + void setUp() { + context = new DefaultCamelContext(); + } + + @AfterEach + void tearDown() { + context.stop(); + } + + private IndexRequest.Builder<Object> preBuiltIndexBuilder() { + return new IndexRequest.Builder<>().index("idx").id("original").document(Map.of("k", "v")); + } + + @Test + void preBuiltIndexBuilderKeepsItsIdWhenHeaderAbsent() throws Exception { + Exchange exchange = new DefaultExchange(context); + + IndexRequest.Builder<?> result + = ElasticsearchActionRequestConverter.toIndexRequestBuilder(preBuiltIndexBuilder(), exchange); + + // no CamelIndexId header -> the caller's id must be preserved, not overwritten with null + assertEquals("original", result.build().id()); + } + + @Test + void preBuiltIndexBuilderHeaderOverridesId() throws Exception { + Exchange exchange = new DefaultExchange(context); + exchange.getIn().setHeader(ElasticsearchConstants.PARAM_INDEX_ID, "fromHeader"); + + IndexRequest.Builder<?> result + = ElasticsearchActionRequestConverter.toIndexRequestBuilder(preBuiltIndexBuilder(), exchange); + + assertEquals("fromHeader", result.build().id()); + } +} Review Comment: No action on this one — it reads as a placeholder ("Test comment") rather than a finding. Flagging rather than silently resolving it, in case something was meant to land here. _Claude Code on behalf of @oscerd_ -- 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]
