BaseRepresenter.multiRepresenters default multiRepresenter for class = null throws NPE

Issue #341 resolved
Former user created an issue

In class BaseRepresenter Method representData there is a strategy implemented, finding the suitable Representer for data Object passed in. If there is no Representer registered in multiRepresenters map, a get with key=null is performed on multiRepresenters map. This should be the way a default Representer can be integrated, which should be used, if no suitable Representer is found. Unfortunatelly this strategy breaks, because of BaseRepresenter.representData line 91 . This loop over the multiRepresenters.keySet gets also the null as a key and uses it unchecked for repr.isInstance() check causing the NullPointerException. An additional check for null on repr will fix this behavior and lets the code reach the intended default Representer lookup.

SnakeYAML 1.17

Regards Jörg Michelberger

Comments (5)

  1. Jörg Michelberger

    I put a lot of Interface based Representers in multiRepresenters map and the implementation suggests a null registered multiRepresenter will work, so I pushed my DefaultRepresenter to the multiRepresenters map and got the NPE. A default Representer handling is a bit different, its nothing with defined class or isInstance contract. It does not fit to representers and/or multiRepresenters. More comparable to the handling like the nullRepresenter. I understand after looking at the code, that pushing it to representers map will do the job of register my DefaultRepresenter as a fallback if no other Representer is found in registrations and I try to do so, but in the implementation of BaseRepresenter this should be more intuitive, that the intended strategy (first null registered multirepresenters, than null registered representers) works, or the handling for a default Representer should be like nullRepresenter.

    As a suggestion: Presenters Maps, nullPresenter, defaultPresenter with private access in BaseRepresenter and public methods to set nullPresenter and defaultPresenter and add representers and multiRepresenters, where it is possible to check the additional Representers for types, duplicates ... But that will break the API of BaseRepresenter users. Maybe a smooth way between?

    Thanks for quick response.

    Regards Jörg Michelberger

  2. Log in to comment