Skip to content

Commit 69b8487

Browse files
committed
Fixed #370
1 parent 0b38913 commit 69b8487

File tree

5 files changed

+20
-25
lines changed

5 files changed

+20
-25
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Version: 2.3.1 (xx-Dec-2013)
1212
(reported by jknack@github)
1313
#367: Make `TypeNameIdResolver` call `TypeResolver` for resolving base type
1414
(suggested by Ben F)
15+
#370: Fail to add Object Id for POJO with no properties
16+
(reported by jh3141@github)
1517
- Fix for [jackson-module-afterburner#38]: need to remove @JacksonStdImpl from
1618
`RawSerializer`, to avoid accidental removal of proper handling.
1719

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ public boolean hasProperties() {
170170
public JsonSerializer<?> build()
171171
{
172172
BeanPropertyWriter[] properties;
173-
// No properties or any getter? No real serializer; caller gets to handle
173+
// No properties, any getter or object id writer?
174+
// No real serializer; caller gets to handle
174175
if (_properties == null || _properties.isEmpty()) {
175-
if (_anyGetter == null) {
176+
if (_anyGetter == null && _objectIdWriter == null) {
176177
return null;
177178
}
178179
properties = NO_PROPERTIES;
179180
} else {
180181
properties = _properties.toArray(new BeanPropertyWriter[_properties.size()]);
181-
182182
}
183183
return new BeanSerializer(_beanDesc.getType(), this,
184184
properties, _filteredProperties);

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider prov
435435
}
436436

437437
JsonSerializer<Object> ser = (JsonSerializer<Object>) builder.build();
438-
438+
439439
/* However, after all modifications: no properties, no serializer
440440
* (note; as per [JACKSON-670], check was moved later on from an earlier location)
441441
*/

src/test/java/com/fasterxml/jackson/databind/struct/TestObjectIdSerialization.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.fasterxml.jackson.annotation.JsonIdentityReference;
55
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
66
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
7-
87
import com.fasterxml.jackson.databind.*;
98

109
/**
@@ -146,7 +145,11 @@ static class Broken
146145
public int value;
147146
public int customId;
148147
}
149-
148+
149+
// [Issue#370]
150+
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
151+
public static class EmptyObject { }
152+
150153
/*
151154
/*****************************************************
152155
/* Unit tests, external id serialization
@@ -155,7 +158,7 @@ static class Broken
155158

156159
private final static String EXP_SIMPLE_INT_CLASS = "{\"id\":1,\"value\":13,\"next\":1}";
157160

158-
private final ObjectMapper MAPPER = new ObjectMapper();
161+
private final ObjectMapper MAPPER = objectMapper();
159162

160163
public void testSimpleSerializationClass() throws Exception
161164
{
@@ -187,6 +190,14 @@ public void testSimpleSerializationProperty() throws Exception
187190
assertEquals(EXP_SIMPLE_INT_PROP, json);
188191
}
189192

193+
// [Issue#370]
194+
public void testEmptyObjectWithId() throws Exception
195+
{
196+
final ObjectMapper mapper = new ObjectMapper();
197+
String json = mapper.writeValueAsString(new EmptyObject());
198+
assertEquals(aposToQuotes("{'@id':1}"), json);
199+
}
200+
190201
/*
191202
/*****************************************************
192203
/* Unit tests, custom (property) id serialization

src/test/java/com/fasterxml/jackson/failing/TestObjectId370.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)