dxa-web-application-java icon indicating copy to clipboard operation
dxa-web-application-java copied to clipboard

Not possible to use Spring to override the default RichTextDataConverter

Open willprice76 opened this issue 7 years ago • 1 comments

In order to work around issue #93 we attempted to define our own CustomRichTextDataConverter with the @Primary and @Component annotations. However, due to the way in which the DXA 2.0 loads the data converters into the GenericSemanticModelDataConverter (using @Autowired on the setConverters method) our custom converter is always overridden by the default.

The setConverters method should reverse sort the set before applying the converters, ensuring that lower priority converters are set first and then overridden by any higher priority converters later.

willprice76 avatar Apr 10 '18 20:04 willprice76

Dirty hack while waiting for this fix is to add an onApplicationEvent for context refresh in your custom RichTextDataConvertor:

@Component
@Primary
public class MyRichTextDataConverter extends RichTextDataConverter implements ApplicationListener<ContextRefreshedEvent> {

    public void onApplicationEvent(ContextRefreshedEvent event) {
        GenericSemanticModelDataConverter dataConverter = getContext().getBean(GenericSemanticModelDataConverter.class);
        Set<SemanticModelConverter<?>> converters = new HashSet<>();
        converters.add(this);
        dataConverter.setConverters(converters);
    }
}

willprice76 avatar Sep 06 '18 12:09 willprice76