Skip to content

Commit bcf15c0

Browse files
Fix duplicate field name serialization with @BsonDiscriminator and getter (#1610)
JAVA-5764 * adds test about single discriminator serialization * improves PojoCodecDiscriminatorTest by replacing the encodesTo test with the roundTrip one * adds readName test about single discriminator serialization * moves the duplicate discriminator keys check in ConventionAnnotationImpl * Update bson/src/test/unit/org/bson/codecs/pojo/entities/DiscriminatorWithGetterModel.java * Update bson/src/test/unit/org/bson/codecs/pojo/entities/DiscriminatorWithProperty.java --------- Co-authored-by: Ross Lawley <[email protected]>
1 parent 33719ef commit bcf15c0

File tree

7 files changed

+229
-6
lines changed

7 files changed

+229
-6
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.bson.codecs.pojo.annotations.BsonIgnore;
2626
import org.bson.codecs.pojo.annotations.BsonProperty;
2727
import org.bson.codecs.pojo.annotations.BsonRepresentation;
28+
import org.bson.diagnostics.Logger;
29+
import org.bson.diagnostics.Loggers;
2830

2931
import java.lang.annotation.Annotation;
3032
import java.lang.reflect.Constructor;
@@ -41,6 +43,8 @@
4143

4244
final class ConventionAnnotationImpl implements Convention {
4345

46+
private static final Logger LOGGER = Loggers.getLogger("ConventionAnnotation");
47+
4448
@Override
4549
public void apply(final ClassModelBuilder<?> classModelBuilder) {
4650
for (final Annotation annotation : classModelBuilder.getAnnotations()) {
@@ -240,6 +244,15 @@ private void cleanPropertyBuilders(final ClassModelBuilder<?> classModelBuilder)
240244
if (!propertyModelBuilder.isReadable() && !propertyModelBuilder.isWritable()) {
241245
propertiesToRemove.add(propertyModelBuilder.getName());
242246
}
247+
if (classModelBuilder.useDiscriminator() && propertyModelBuilder.getReadName().equals(classModelBuilder.getDiscriminatorKey())) {
248+
propertiesToRemove.add(propertyModelBuilder.getName());
249+
LOGGER.warn(
250+
format(
251+
"Removed the property '%s' from the model because the discriminator has the same key",
252+
classModelBuilder.getDiscriminatorKey()
253+
)
254+
);
255+
}
243256
}
244257
for (String propertyName : propertiesToRemove) {
245258
classModelBuilder.removeProperty(propertyName);

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,10 @@ public void encode(final BsonWriter writer, final T value, final EncoderContext
7676
writer.writeStartDocument();
7777

7878
encodeIdProperty(writer, value, encoderContext, classModel.getIdPropertyModelHolder());
79-
80-
if (classModel.useDiscriminator()) {
81-
writer.writeString(classModel.getDiscriminatorKey(), classModel.getDiscriminator());
82-
}
79+
encodeDiscriminatorProperty(writer);
8380

8481
for (PropertyModel<?> propertyModel : classModel.getPropertyModels()) {
85-
if (propertyModel.equals(classModel.getIdPropertyModel())) {
82+
if (idProperty(propertyModel)) {
8683
continue;
8784
}
8885
encodeProperty(writer, value, encoderContext, propertyModel);
@@ -140,6 +137,16 @@ private <S> void encodeIdProperty(final BsonWriter writer, final T instance, fin
140137
}
141138
}
142139

140+
private boolean idProperty(final PropertyModel<?> propertyModel) {
141+
return propertyModel.equals(classModel.getIdPropertyModel());
142+
}
143+
144+
private void encodeDiscriminatorProperty(final BsonWriter writer) {
145+
if (classModel.useDiscriminator()) {
146+
writer.writeString(classModel.getDiscriminatorKey(), classModel.getDiscriminator());
147+
}
148+
}
149+
143150
private <S> void encodeProperty(final BsonWriter writer, final T instance, final EncoderContext encoderContext,
144151
final PropertyModel<S> propertyModel) {
145152
if (propertyModel != null && propertyModel.isReadable()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private <T> PojoCodec<T> createCodec(final Class<T> clazz, final CodecRegistry r
8080
} else if (automatic || (clazz.getPackage() != null && packages.contains(clazz.getPackage().getName()))) {
8181
try {
8282
classModel = createClassModel(clazz, conventions);
83-
if (clazz.isInterface() || !classModel.getPropertyModels().isEmpty()) {
83+
if (clazz.isInterface() || !classModel.getPropertyModels().isEmpty() || classModel.useDiscriminator()) {
8484
discriminatorLookup.addClassModel(classModel);
8585
return new AutomaticPojoCodec<>(createCodec(classModel, registry, propertyCodecProviders,
8686
discriminatorLookup));
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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;
18+
19+
import org.bson.codecs.pojo.entities.DiscriminatorModel;
20+
import org.bson.codecs.pojo.entities.DiscriminatorWithGetterModel;
21+
import org.bson.codecs.pojo.entities.DiscriminatorWithProperty;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
25+
26+
public final class PojoCodecDiscriminatorTest extends PojoTestCase {
27+
28+
@Test
29+
public void testDiscriminatorEncodedOnceWhenItIsAlsoAGetter() {
30+
byte[] encodedDiscriminatorModel = encode(
31+
getCodec(DiscriminatorModel.class),
32+
new DiscriminatorModel(),
33+
false
34+
).toByteArray();
35+
byte[] encodedDiscriminatorWithGetter = encode(
36+
getCodec(DiscriminatorWithGetterModel.class),
37+
new DiscriminatorWithGetterModel(),
38+
false
39+
).toByteArray();
40+
assertArrayEquals(encodedDiscriminatorModel, encodedDiscriminatorWithGetter);
41+
}
42+
43+
@Test
44+
public void testDiscriminatorRoundTripWhenItIsAlsoAGetter() {
45+
roundTrip(
46+
new DiscriminatorWithGetterModel(),
47+
"{discriminatorKey:'discriminatorValue'}"
48+
);
49+
}
50+
51+
@Test
52+
public void testDiscriminatorEncodedOnceWhenItIsAlsoAProperty() {
53+
byte[] encodedDiscriminatorModel = encode(
54+
getCodec(DiscriminatorModel.class),
55+
new DiscriminatorModel(),
56+
false
57+
).toByteArray();
58+
byte[] encodedDiscriminatorWithProperty = encode(
59+
getCodec(DiscriminatorWithProperty.class),
60+
new DiscriminatorWithProperty(),
61+
false
62+
).toByteArray();
63+
assertArrayEquals(encodedDiscriminatorModel, encodedDiscriminatorWithProperty);
64+
}
65+
66+
@Test
67+
public void testDiscriminatorRoundTripWhenItIsAlsoAProperty() {
68+
roundTrip(
69+
new DiscriminatorWithProperty(),
70+
"{discriminatorKey:'discriminatorValue'}"
71+
);
72+
}
73+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.BsonDiscriminator;
20+
21+
@BsonDiscriminator(key = "discriminatorKey", value = "discriminatorValue")
22+
public class DiscriminatorModel {
23+
24+
public DiscriminatorModel() {
25+
}
26+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.BsonDiscriminator;
20+
21+
import java.util.Objects;
22+
23+
@BsonDiscriminator(key = "discriminatorKey", value = "discriminatorValue")
24+
public class DiscriminatorWithGetterModel {
25+
26+
public DiscriminatorWithGetterModel() {
27+
}
28+
29+
public String getDiscriminatorKey() {
30+
return "discriminatorValue";
31+
}
32+
33+
@Override
34+
public boolean equals(final Object o) {
35+
if (o == null || getClass() != o.getClass()) {
36+
return false;
37+
}
38+
final DiscriminatorWithGetterModel that = (DiscriminatorWithGetterModel) o;
39+
return Objects.equals(getDiscriminatorKey(), that.getDiscriminatorKey());
40+
}
41+
42+
@Override
43+
public int hashCode() {
44+
return Objects.hashCode(getDiscriminatorKey());
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "DiscriminatorWithGetterModel{}";
50+
}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.BsonDiscriminator;
20+
import org.bson.codecs.pojo.annotations.BsonProperty;
21+
22+
import java.util.Objects;
23+
24+
@BsonDiscriminator(key = "discriminatorKey", value = "discriminatorValue")
25+
public class DiscriminatorWithProperty {
26+
27+
public DiscriminatorWithProperty() {
28+
}
29+
30+
@BsonProperty("discriminatorKey")
31+
public String getDiscriminator() {
32+
return "discriminatorValue";
33+
}
34+
35+
@Override
36+
public boolean equals(final Object o) {
37+
if (o == null || getClass() != o.getClass()){
38+
return false;
39+
}
40+
final DiscriminatorWithProperty that = (DiscriminatorWithProperty) o;
41+
return Objects.equals(getDiscriminator(), that.getDiscriminator());
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
return Objects.hashCode(getDiscriminator());
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "DiscriminatorWithProperty{}";
52+
}
53+
}

0 commit comments

Comments
 (0)