Skip to content

Commit b0f05c8

Browse files
authored
PojoCodec BsonId error message (#1071)
Help users when using a BsonId in a constructor and there is no known Id property in the model. JAVA-4838
1 parent 2fcd943 commit b0f05c8

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

bson/src/main/org/bson/codecs/pojo/ConventionAnnotationImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ private <T> void processCreatorAnnotation(final ClassModelBuilder<T> classModelB
166166
PropertyModelBuilder<?> propertyModelBuilder = null;
167167

168168
if (isIdProperty) {
169+
if (classModelBuilder.getIdPropertyName() == null) {
170+
throw new CodecConfigurationException("A @BsonId annotation has been used with @BsonCreator "
171+
+ "but there is no known Id property.\n"
172+
+ "Please either use the @BsonProperty annotation in the creator or "
173+
+ "annotate the corresponding property in the class with the @BsonId.");
174+
}
169175
propertyModelBuilder = classModelBuilder.getProperty(classModelBuilder.getIdPropertyName());
170176
} else {
171177
BsonProperty bsonProperty = properties.get(i);

bson/src/test/unit/org/bson/codecs/pojo/ConventionsTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.bson.codecs.pojo.entities.conventions.AnnotationWithObjectIdModel;
3030
import org.bson.codecs.pojo.entities.conventions.AnnotationWriteCollision;
3131
import org.bson.codecs.pojo.entities.conventions.BsonIgnoreDuplicatePropertyMultipleTypes;
32+
import org.bson.codecs.pojo.entities.conventions.CreatorConstructorNoKnownIdModel;
3233
import org.bson.codecs.pojo.entities.conventions.CreatorInvalidConstructorModel;
3334
import org.bson.codecs.pojo.entities.conventions.CreatorInvalidMethodModel;
3435
import org.bson.codecs.pojo.entities.conventions.CreatorInvalidMethodReturnTypeModel;
@@ -226,6 +227,12 @@ public void testCreatorInvalidTypeMethodModel() {
226227
.conventions(singletonList(ANNOTATION_CONVENTION)).build();
227228
}
228229

230+
@Test(expected = CodecConfigurationException.class)
231+
public void testCreatorConstructorNoKnownIdModel() {
232+
ClassModel.builder(CreatorConstructorNoKnownIdModel.class)
233+
.conventions(singletonList(ANNOTATION_CONVENTION)).build();
234+
}
235+
229236
@Test(expected = CodecConfigurationException.class)
230237
public void testBsonIgnoreDuplicatePropertyMultipleTypesModel() {
231238
ClassModel.builder(BsonIgnoreDuplicatePropertyMultipleTypes.class)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.bson.codecs.pojo.entities.conventions;
18+
19+
import org.bson.codecs.pojo.annotations.BsonCreator;
20+
import org.bson.codecs.pojo.annotations.BsonId;
21+
import org.bson.codecs.pojo.annotations.BsonProperty;
22+
23+
import java.util.Objects;
24+
25+
public class CreatorConstructorNoKnownIdModel {
26+
private final String stringField;
27+
private final long longField;
28+
29+
@BsonCreator
30+
public CreatorConstructorNoKnownIdModel(
31+
@BsonId final String stringField,
32+
@BsonProperty("longField") final long longField) {
33+
this.stringField = stringField;
34+
this.longField = longField;
35+
}
36+
37+
public String getStringField() {
38+
return stringField;
39+
}
40+
41+
public long getLongField() {
42+
return longField;
43+
}
44+
45+
@Override
46+
public boolean equals(final Object o) {
47+
if (this == o) {
48+
return true;
49+
}
50+
if (o == null || getClass() != o.getClass()) {
51+
return false;
52+
}
53+
final CreatorConstructorNoKnownIdModel that = (CreatorConstructorNoKnownIdModel) o;
54+
return longField == that.longField && Objects.equals(stringField, that.stringField);
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return Objects.hash(stringField, longField);
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "CreatorConstructorNoKnownIdModel{"
65+
+ ", stringField='" + stringField + '\''
66+
+ ", longField=" + longField
67+
+ '}';
68+
}
69+
}

0 commit comments

Comments
 (0)