Skip to content

Commit b735256

Browse files
committed
Allow duplicate annotations in classes for use in POJO codec
JAVA-3320
1 parent f78425f commit b735256

File tree

6 files changed

+202
-0
lines changed

6 files changed

+202
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public List<Annotation> getReadAnnotations() {
6161

6262
public PropertyMetadata<T> addReadAnnotation(final Annotation annotation) {
6363
if (readAnnotations.containsKey(annotation.annotationType())) {
64+
if (annotation.equals(readAnnotations.get(annotation.annotationType()))) {
65+
return this;
66+
}
6467
throw new CodecConfigurationException(format("Read annotation %s for '%s' already exists in %s", annotation.annotationType(),
6568
name, declaringClassName));
6669
}
@@ -74,6 +77,9 @@ public List<Annotation> getWriteAnnotations() {
7477

7578
public PropertyMetadata<T> addWriteAnnotation(final Annotation annotation) {
7679
if (writeAnnotations.containsKey(annotation.annotationType())) {
80+
if (annotation.equals(writeAnnotations.get(annotation.annotationType()))) {
81+
return this;
82+
}
7783
throw new CodecConfigurationException(format("Write annotation %s for '%s' already exists in %s", annotation.annotationType(),
7884
name, declaringClassName));
7985
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import org.bson.codecs.configuration.CodecConfigurationException;
2020
import org.bson.codecs.pojo.entities.SimpleModel;
2121
import org.bson.codecs.pojo.entities.conventions.AnnotationBsonPropertyIdModel;
22+
import org.bson.codecs.pojo.entities.conventions.AnnotationCollision;
2223
import org.bson.codecs.pojo.entities.conventions.AnnotationDefaultsModel;
2324
import org.bson.codecs.pojo.entities.conventions.AnnotationNameCollision;
2425
import org.bson.codecs.pojo.entities.conventions.AnnotationWithObjectIdModel;
26+
import org.bson.codecs.pojo.entities.conventions.AnnotationWriteCollision;
2527
import org.bson.codecs.pojo.entities.conventions.CreatorInvalidConstructorModel;
2628
import org.bson.codecs.pojo.entities.conventions.CreatorInvalidMethodModel;
2729
import org.bson.codecs.pojo.entities.conventions.CreatorInvalidMethodReturnTypeModel;
@@ -137,6 +139,16 @@ public InstanceCreator<SimpleModel> create() {
137139
assertNull(idPropertyModel.useDiscriminator());
138140
}
139141

142+
@Test(expected = CodecConfigurationException.class)
143+
public void testAnnotationCollision() {
144+
ClassModel.builder(AnnotationCollision.class).conventions(DEFAULT_CONVENTIONS).build();
145+
}
146+
147+
@Test(expected = CodecConfigurationException.class)
148+
public void testAnnotationWriteCollision() {
149+
ClassModel.builder(AnnotationWriteCollision.class).conventions(DEFAULT_CONVENTIONS).build();
150+
}
151+
140152
@Test(expected = CodecConfigurationException.class)
141153
public void testAnnotationNameCollision() {
142154
ClassModel.builder(AnnotationNameCollision.class)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import org.bson.codecs.pojo.entities.conventions.CreatorMethodModel;
8282
import org.bson.codecs.pojo.entities.conventions.CreatorNoArgsConstructorModel;
8383
import org.bson.codecs.pojo.entities.conventions.CreatorNoArgsMethodModel;
84+
import org.bson.codecs.pojo.entities.DuplicateAnnotationAllowedModel;
8485
import org.bson.codecs.pojo.entities.conventions.InterfaceModel;
8586
import org.bson.codecs.pojo.entities.conventions.InterfaceModelImplA;
8687
import org.bson.codecs.pojo.entities.conventions.InterfaceModelImplB;
@@ -421,6 +422,11 @@ private static List<TestData> testCases() {
421422
+ "'left': {'level': 'left-1', 'left': {'level': 'left-2'}},"
422423
+ "'right': {'level': 'right-1'}}"));
423424

425+
data.add(new TestData("DuplicateAnnotationAllowedModel",
426+
new DuplicateAnnotationAllowedModel("abc"),
427+
getPojoCodecProviderBuilder(DuplicateAnnotationAllowedModel.class).conventions(Conventions.DEFAULT_CONVENTIONS),
428+
"{'_id': 'abc'}"));
429+
424430
return data;
425431
}
426432

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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;
18+
19+
import org.bson.codecs.pojo.annotations.BsonIgnore;
20+
import org.bson.codecs.pojo.annotations.BsonProperty;
21+
22+
import javax.annotation.Nullable;
23+
24+
public class DuplicateAnnotationAllowedModel {
25+
26+
@Nullable
27+
private String id;
28+
29+
@BsonIgnore
30+
private String ignoredString;
31+
32+
@BsonProperty("property")
33+
private String propertyString;
34+
35+
public DuplicateAnnotationAllowedModel() {
36+
}
37+
38+
public DuplicateAnnotationAllowedModel(final String id) {
39+
this.id = id;
40+
}
41+
42+
@Nullable
43+
public String getId() {
44+
return id;
45+
}
46+
47+
public void setId(@Nullable final String id) {
48+
this.id = id;
49+
}
50+
51+
@BsonIgnore
52+
public String getIgnoredString() {
53+
return ignoredString;
54+
}
55+
56+
@BsonIgnore
57+
public void setIgnoredString(final String ignoredString) {
58+
this.ignoredString = ignoredString;
59+
}
60+
61+
@BsonProperty("property")
62+
public String getPropertyString() {
63+
return propertyString;
64+
}
65+
66+
@BsonProperty("property")
67+
public void setPropertyString(final String propertyString) {
68+
this.propertyString = propertyString;
69+
}
70+
71+
@Override
72+
public boolean equals(final Object o) {
73+
if (this == o) {
74+
return true;
75+
}
76+
if (o == null || getClass() != o.getClass()) {
77+
return false;
78+
}
79+
80+
DuplicateAnnotationAllowedModel that = (DuplicateAnnotationAllowedModel) o;
81+
82+
return (id != null ? id.equals(that.id) : that.id == null);
83+
}
84+
85+
@Override
86+
public int hashCode() {
87+
return id != null ? id.hashCode() : 0;
88+
}
89+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.BsonProperty;
20+
21+
public final class AnnotationCollision {
22+
23+
public String id;
24+
25+
@BsonProperty("color")
26+
private String color;
27+
28+
public String getId() {
29+
return id;
30+
}
31+
32+
public void setId(final String id) {
33+
this.id = id;
34+
}
35+
36+
@BsonProperty("theme")
37+
public String getColor() {
38+
return color;
39+
}
40+
41+
public void setColor(final String color) {
42+
this.color = color;
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.BsonProperty;
20+
21+
public final class AnnotationWriteCollision {
22+
23+
public String id;
24+
25+
@BsonProperty("color")
26+
private String color;
27+
28+
29+
public String getId() {
30+
return id;
31+
}
32+
33+
public void setId(final String id) {
34+
this.id = id;
35+
}
36+
37+
public String getColor() {
38+
return color;
39+
}
40+
41+
@BsonProperty("theme")
42+
public void setColor(final String color) {
43+
this.color = color;
44+
}
45+
}

0 commit comments

Comments
 (0)