Skip to content

UnwrappingBeanPropertyWriter incorrectly assumes the found serializer is of type UnwrappingBeanSerializer #2060

@ptahchiev

Description

@ptahchiev

Hello,
I want to add a bunch of attributes to the resulting JSON file so I create my own StdSerializerlike this:

    class MarkupAwareStandardSerializer extends StdSerializer<AbstractEntityDefinition> {

        private StdSerializer serializer;

        public MarkupAwareStandardSerializer(StdSerializer src) {
            super(src);
            this.serializer = src;
        }

        @Override
        public void serialize(AbstractEntityDefinition bean, JsonGenerator gen, SerializerProvider provider) throws IOException {
            ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());

            if (servletRequestAttributes != null) {
                String filterId = servletRequestAttributes.getRequest().getParameter("filter");

                if (StringUtils.isNotEmpty(filterId) && filterId.equals("search")) {
                    List<ItemMarkup> itemMarkups = entityFacade.getEntityPropertyMarkups(bean.getClass());
                    gen.writeStartObject(bean);
                    for (ItemMarkup itemMarkup : itemMarkups) {
                        if (itemMarkup != null && itemMarkup.isPartOfSearchResult()) {
                            if (StringUtils.isNotEmpty(itemMarkup.getSearchResultExpression())) {
                                gen.writeStringField(itemMarkup.getName(),
                                                     parseExpression(itemMarkup.getSearchResultExpression(), new ExpressionContext(bean)));
                            } else {
                                try {
                                    provider.defaultSerializeField(itemMarkup.getName(), PropertyUtils.getProperty(bean, itemMarkup.getName()), gen);
                                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException nsmex) {
                                    LOG.warn(nsmex.getMessage());
                                }
                            }
                        }
                    }
                    gen.writeEndObject();
                } else {
                    serializer.unwrappingSerializer(null).serialize(bean, gen, provider);
                }
            } else {
                serializer.unwrappingSerializer(null).serialize(bean, gen, provider);
            }
        }

        @Override
        public boolean isUnwrappingSerializer() {
            return true;
        }
    }

Notice I have overriden the isUnwrappingSerializer() method to return true. But now I get this exception:

java.lang.ClassCastException: com.nemesis.platform.module.restservices.core.jackson.MarkupAwareSerializerModifier$MarkupAwareStandardSerializer cannot be cast to com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanSerializer
	at com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter._findAndAddDynamic(UnwrappingBeanPropertyWriter.java:214)
	at com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter.serializeAsField(UnwrappingBeanPropertyWriter.java:102)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
	at com.fasterxml.jackson.databind.SerializerProvider.defaultSerializeValue(SerializerProvider.java:1033)
	at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer.serialize(PersistentEntityJackson2Module.java:202)
	at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer.serialize(PersistentEntityJackson2Module.java:150)

And indeed here:
https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanPropertyWriter.java#L212

you are trying to cast the serializer to UnwrappingBeanSerializer which I don't think is correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions