Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA-5203] Support stored nulls for non-primitive fields in Java records #1223

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions bson-record-codec/src/main/org/bson/codecs/record/RecordCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private static final class ComponentModel {
private final Codec<?> codec;
private final int index;
private final String fieldName;
private final boolean isNullable;

private ComponentModel(final List<Type> typeParameters, final RecordComponent component, final CodecRegistry codecRegistry,
final int index) {
Expand All @@ -70,6 +71,7 @@ private ComponentModel(final List<Type> typeParameters, final RecordComponent co
this.codec = computeCodec(typeParameters, component, codecRegistry);
this.index = index;
this.fieldName = computeFieldName(component);
this.isNullable = checkFieldIsNullable(component);
}

String getComponentName() {
Expand Down Expand Up @@ -156,6 +158,14 @@ private static String computeFieldName(final RecordComponent component) {
return component.getName();
}

private static boolean checkFieldIsNullable(final RecordComponent component) {
jyemin marked this conversation as resolved.
Show resolved Hide resolved
try {
return component.getDeclaringRecord().getDeclaredField(component.getName()).isAnnotationPresent(Nullable.class);
} catch (NoSuchFieldException e) {
throw new AssertionError(format("Unexpectedly missing the declared field for recordComponent %s", component), e);
}
}

private static <T extends Annotation> boolean isAnnotationPresentOnField(final RecordComponent component,
final Class<T> annotation) {
try {
Expand Down Expand Up @@ -275,6 +285,8 @@ public T decode(final BsonReader reader, final DecoderContext decoderContext) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Found property not present in the ClassModel: %s", fieldName));
}
} else if (reader.getCurrentBsonType() == BsonType.NULL && componentModel.isNullable) {
jyemin marked this conversation as resolved.
Show resolved Hide resolved
reader.readNull();
} else {
constructorArguments[componentModel.index] = decoderContext.decodeWithChildContext(componentModel.codec, reader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.bson.BsonDocumentWriter;
import org.bson.BsonDouble;
import org.bson.BsonInt32;
import org.bson.BsonNull;
import org.bson.BsonObjectId;
import org.bson.BsonString;
import org.bson.codecs.DecoderContext;
Expand Down Expand Up @@ -49,6 +50,7 @@
import org.bson.codecs.record.samples.TestRecordWithMapOfRecords;
import org.bson.codecs.record.samples.TestRecordWithNestedParameterized;
import org.bson.codecs.record.samples.TestRecordWithNestedParameterizedRecord;
import org.bson.codecs.record.samples.TestRecordWithNullableAnnotation;
import org.bson.codecs.record.samples.TestRecordWithParameterizedRecord;
import org.bson.codecs.record.samples.TestRecordWithPojoAnnotations;
import org.bson.codecs.record.samples.TestSelfReferentialHolderRecord;
Expand Down Expand Up @@ -325,6 +327,22 @@ public void testRecordWithNulls() {
assertEquals(testRecord, decoded);
}

@Test
public void testRecordWithStoredNulls() {
var codec = createRecordCodec(TestRecordWithNullableAnnotation.class, Bson.DEFAULT_CODEC_REGISTRY);
var identifier = new ObjectId();
var testRecord = new TestRecordWithNullableAnnotation(identifier, null);

var document = new BsonDocument("_id", new BsonObjectId(identifier))
.append("name", new BsonNull());

// when
var decoded = codec.decode(new BsonDocumentReader(document), DecoderContext.builder().build());

// then
assertEquals(testRecord, decoded);
}

@Test
public void testRecordWithExtraData() {
var codec = createRecordCodec(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.bson.codecs.record.samples;

import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.types.ObjectId;

import javax.annotation.Nullable;

public record TestRecordWithNullableAnnotation (@BsonId ObjectId id, @Nullable String name) {
}