Skip to content

Error serializing EntityModel when content is an empty bean and the ObjectMapper is configured to not to fail on those #1686

Closed

Description

@EarthCitizen

When the content of EntityModel is a class with no defined fields, the serialization fails. As can be seen from the example below, the ObjectMapper does not have a problem with this object when sent directly.

The expectation is that if the ObjectMapper does not have a problem with the content when sent directly, then it should not have a problem when it is in the content field of the EntityModel.

    @Bean
    public Jackson2ObjectMapperBuilder objectMapperBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
                .serializationInclusion(JsonInclude.Include.NON_NULL)
                .failOnUnknownProperties(false)
                .failOnEmptyBeans(false)
                .indentOutput(false);
        return builder;
    }
public class Widget {}
@SpringBootTest
public class MapperTest {
    @Autowired
    ObjectMapper objectMapper;

    @Test
    public void widget() throws JsonProcessingException {
        System.out.println("=======================");
        System.out.println(objectMapper.writeValueAsString(new Widget()));
        System.out.println(objectMapper.writeValueAsString(EntityModel.of(new Widget())));
        System.out.println("=======================");
    }
}
=======================
{}

Can not start an object, expecting field name (context: Object)
com.fasterxml.jackson.core.JsonGenerationException: Can not start an object, expecting field name (context: Object)
	at app//com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:2657)
	at app//com.fasterxml.jackson.core.json.JsonGeneratorImpl._reportCantWriteValueExpectName(JsonGeneratorImpl.java:248)
	at app//com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._verifyValueWrite(WriterBasedJsonGenerator.java:950)
	at app//com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeStartObject(WriterBasedJsonGenerator.java:311)
	at app//com.fasterxml.jackson.core.JsonGenerator.writeStartObject(JsonGenerator.java:1008)
	at app//com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:36)
	at app//org.springframework.hateoas.EntityModel$MapSuppressingUnwrappingSerializer.serialize(EntityModel.java:226)
	at app//com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter.serializeAsField(UnwrappingBeanPropertyWriter.java:127)
	at app//com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)
	at app//com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
	at app//com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
	at app//com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
	at app//com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4487)
	at app//com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3742)
...

Activity

  1. self-assigned this
    on Oct 19, 2021
  2. added
    in: core
    on Oct 19, 2021
  3. added this to the 1.4 GA milestone on Oct 19, 2021
  4. changed the title [-]Error Serializing EntityModel When Content Object Contains No Fields[/-] [+]Error serializing `EntityModel` when content is an empty bean and the ObjectMapper is configured to not to fail on those[/+] on Oct 19, 2021
  5. added a commit that references this issue on Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions