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 3 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
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 = !component.getType().isPrimitive();
}

String getComponentName() {
Expand Down Expand Up @@ -275,6 +277,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.TestRecordWithNullableField;
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(TestRecordWithNullableField.class, Bson.DEFAULT_CODEC_REGISTRY);
var identifier = new ObjectId();
var testRecord = new TestRecordWithNullableField(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,23 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.bson.codecs.record.samples;

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

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