[ 
https://issues.apache.org/jira/browse/CAMEL-10542?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15847140#comment-15847140
 ] 

ASF GitHub Bot commented on CAMEL-10542:
----------------------------------------

GitHub user lburgazzoli opened a pull request:

    https://github.com/apache/camel/pull/1436

    CAMEL-10542: DataFormat from registry is used for every dataformat 
operation (marshal/unmarshal)

    I'm opening this PR for review as there are some changes on CamelContext 
level so I'd like to gather some feedback.
    
    As today the DataFormat resolution works as follow:
    1. search int the registry if a DataFormat instance exists for a given name
    2. if not found in registry create a new one from the resources 
(META-INF/services/...)
    
    This my cause issues as per CAMEL-10542
    
    The proposed solution introduces a new DataFormatFactory and a new method 
to DataFormatResolver to have a way to explicit request for a new instance so 
it looks like:
    
    ```java
    public interface DataFormatResolver {
        // Resolve using registry then fallback to createDataFormat
        DataFormat resolveDataFormat(String name, CamelContext context);
    
        // Resolve factory using registry or fallback to resources
        DataFormat createDataFormat(String name, CamelContext context);
    } 
    ```
    
    Model's DataFormatDefinition is now using createDataFormat except for 
CustomDataFormat which indeed is supposed to eventually share the same data 
format. 
    
    I still need to implement some stuffs but the basic concept is here.
    Any feedback would be really appreciated.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/lburgazzoli/apache-camel CAMEL-10542

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/camel/pull/1436.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #1436
    
----

----


> DataFormat from registry is used for every dataformat operation 
> (marshal/unmarshal)
> -----------------------------------------------------------------------------------
>
>                 Key: CAMEL-10542
>                 URL: https://issues.apache.org/jira/browse/CAMEL-10542
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-core
>            Reporter: Luca Burgazzoli
>            Assignee: Luca Burgazzoli
>             Fix For: 2.18.3, 2.19.0
>
>
> While working on an issue related to spring-boot I found out that if a data 
> format is registered in camel registry with the same name as the one camel 
> looks-up with the help of DefaultDataFormatResolver, this object is then 
> re-configured for each data format definition so one definition may override 
> previous configuration with an undefined behavior.
> So assume you have an xml route definitions as:
> {code:xml}
> <routes xmlns="http://camel.apache.org/schema/spring";>
>   <route>
>     <from uri="direct:unmarshal"/>
>     <unmarshal>
>       <csv delimiter=";" headerDisabled="true"/>
>     </unmarshal>
>   </route>
>   <route>
>     <from uri="direct:marshal"/>
>     <marshal>
>       <csv headerDisabled="true" quoteDisabled="true"/>
>     </marshal>
>   </route>
> </routes>
> {code}
> And some code like:
> {code:java}
> InputStream is = getClass().getResourceAsStream("...");
> SimpleRegistry reg = new SimpleRegistry();
> reg.put("csv-dataformat", new CsvDataFormat());
> DefaultCamelContext ctx = new DefaultCamelContext(reg);
> ctx.addRouteDefinitions(ctx.loadRoutesDefinition(is).getRoutes());
> ctx.start();
> ProducerTemplate template = ctx.createProducerTemplate();
> String result = template.requestBody(
>     "direct:marshal",
>     Arrays.asList(Arrays.asList( "A1", "B1", "C1" )),
>     String.class);
> assertEquals("A1,B1,C1", result);
> ctx.stop
> {code}
> Then this test fails with:
> {code}
> Expected :A1,B1,C1
> Actual   :A1;B1;C1
> {code}
> It fails because the object added to the SimpleRegistry is shared among the 
> two csv dataformats  so it is configured to have delimiter = ';' 
> For spring-boot this causes some issues as it registers data formats beans as 
> part of its auto-configuration magic thus if you do not set your own instance 
> of data format, any data format operation like marshal/unmarshal may not work 
> as expected. 
> - for spring-boot a solution would be to annotate auto configured data format 
> beans with prototype scope.
> - a more generic solution would be to make DataFormat Cloneable and clone the 
> bean found in the registry



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to