Skip to content

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

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.bson.codecs.record;

import org.bson.BsonInvalidOperationException;
import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonWriter;
Expand Down Expand Up @@ -62,6 +63,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 +72,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 +278,11 @@ 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) {
if (!componentModel.isNullable) {
throw new BsonInvalidOperationException(format("Null value on primitive field: %s", componentModel.fieldName));
}
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,8 @@
import org.bson.BsonDocumentWriter;
import org.bson.BsonDouble;
import org.bson.BsonInt32;
import org.bson.BsonInvalidOperationException;
import org.bson.BsonNull;
import org.bson.BsonObjectId;
import org.bson.BsonString;
import org.bson.codecs.DecoderContext;
Expand Down Expand Up @@ -49,6 +51,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 +328,35 @@ 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, 42);

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

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

// then
assertEquals(testRecord, decoded);
}

@Test
public void testExceptionsWithStoredNullsOnPrimitiveField() {
var codec = createRecordCodec(TestRecordWithNullableField.class, Bson.DEFAULT_CODEC_REGISTRY);

var document = new BsonDocument("_id", new BsonObjectId(new ObjectId()))
.append("name", new BsonString("Felix"))
.append("age", new BsonNull());

assertThrows(BsonInvalidOperationException.class, () ->
codec.decode(new BsonDocumentReader(document), DecoderContext.builder().build()));
}

@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, int age) {
}