Skip to content

Using @JsonValue in embedded collection values causes an exception [DATAREST-1564] #1926

@spring-projects-issues

Description

@spring-projects-issues

alienisty opened DATAREST-1564 and commented

Following the update of spring-hateoas 1.1.2, the resolution of https://github.com/spring-projects/spring-hateoas/issues/1354 seems to have caused the regression.

Following a discussion with Oliver Drotbohm on the ticket spring-projects/spring-hateoas#1371, he thinks the problem should be fixed in spring data rest.

Attached is a minimal reproduction project for an end to end demonstration using spring-data-rest.

A quicker demonstration of the core problem can be done with the following code.

void serialisation() throws Exception {
  var value = new Value("test");
  var model = EntityModel.of(value);
  var string = objectMapper.writeValueAsString(model);
  System.out.println(string);
}
static class Value {
  @JsonValue
  String theValue;
    Value(String value) {
    this.theValue = value;
  }
}

Which causes the following exception when run with spring-hateoas 1.1.2:

com.fasterxml.jackson.core.JsonGenerationException: Can not write a string, expecting field name (context: Object)
	at com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:2151)
	at com.fasterxml.jackson.core.json.JsonGeneratorImpl._reportCantWriteValueExpectName(JsonGeneratorImpl.java:233)
	at com.fasterxml.jackson.core.json.JsonGeneratorImpl._verifyPrettyValueWrite(JsonGeneratorImpl.java:223)
	at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._verifyValueWrite(WriterBasedJsonGenerator.java:917)
	at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeString(WriterBasedJsonGenerator.java:400)
	at com.fasterxml.jackson.databind.ser.std.StringSerializer.serialize(StringSerializer.java:41)
	at com.fasterxml.jackson.databind.ser.std.JsonValueSerializer.serialize(JsonValueSerializer.java:181)
	at org.springframework.hateoas.EntityModel$MapSuppressingUnwrappingSerializer.serialize(EntityModel.java:218)
	at com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter.serializeAsField(UnwrappingBeanPropertyWriter.java:127)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
	at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4407)
	at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3661)
...

Attachments:

1 votes, 2 watchers

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions