Skip to content

Remove deprecated record annotations #1249

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 1 commit into from
Nov 14, 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
44 changes: 20 additions & 24 deletions bson-record-codec/src/main/org/bson/codecs/record/RecordCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
import org.bson.codecs.RepresentationConfigurable;
import org.bson.codecs.configuration.CodecConfigurationException;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.record.annotations.BsonId;
import org.bson.codecs.record.annotations.BsonProperty;
import org.bson.codecs.record.annotations.BsonRepresentation;
import org.bson.codecs.pojo.annotations.BsonCreator;
import org.bson.codecs.pojo.annotations.BsonDiscriminator;
import org.bson.codecs.pojo.annotations.BsonExtraElements;
import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.codecs.pojo.annotations.BsonIgnore;
import org.bson.codecs.pojo.annotations.BsonProperty;
import org.bson.codecs.pojo.annotations.BsonRepresentation;
import org.bson.diagnostics.Logger;
import org.bson.diagnostics.Loggers;

Expand Down Expand Up @@ -84,7 +88,6 @@ Object getValue(final Record record) throws InvocationTargetException, IllegalAc
return component.getAccessor().invoke(record);
}

@SuppressWarnings("deprecation")
private static Codec<?> computeCodec(final List<Type> typeParameters, final RecordComponent component,
final CodecRegistry codecRegistry) {
var rawType = toWrapper(resolveComponentType(typeParameters, component));
Expand All @@ -94,11 +97,9 @@ private static Codec<?> computeCodec(final List<Type> typeParameters, final Reco
: codecRegistry.get(rawType);
BsonType bsonRepresentationType = null;

if (component.isAnnotationPresent(BsonRepresentation.class)) {
bsonRepresentationType = component.getAnnotation(BsonRepresentation.class).value();
} else if (isAnnotationPresentOnField(component, org.bson.codecs.pojo.annotations.BsonRepresentation.class)) {
if (isAnnotationPresentOnField(component, BsonRepresentation.class)) {
bsonRepresentationType = getAnnotationOnField(component,
org.bson.codecs.pojo.annotations.BsonRepresentation.class).value();
BsonRepresentation.class).value();
}
if (bsonRepresentationType != null) {
if (codec instanceof RepresentationConfigurable<?> representationConfigurable) {
Expand Down Expand Up @@ -142,16 +143,11 @@ private static int getIndexOfTypeParameter(final String typeParameterName, final
recordClass.getName(), typeParameterName));
}

@SuppressWarnings("deprecation")
private static String computeFieldName(final RecordComponent component) {
if (component.isAnnotationPresent(BsonId.class)) {
if (isAnnotationPresentOnField(component, BsonId.class)) {
return "_id";
} else if (isAnnotationPresentOnField(component, org.bson.codecs.pojo.annotations.BsonId.class)) {
return "_id";
} else if (component.isAnnotationPresent(BsonProperty.class)) {
return component.getAnnotation(BsonProperty.class).value();
} else if (isAnnotationPresentOnField(component, org.bson.codecs.pojo.annotations.BsonProperty.class)) {
return getAnnotationOnField(component, org.bson.codecs.pojo.annotations.BsonProperty.class).value();
} else if (isAnnotationPresentOnField(component, BsonProperty.class)) {
return getAnnotationOnField(component, BsonProperty.class).value();
}
return component.getName();
}
Expand Down Expand Up @@ -179,14 +175,14 @@ private static <T extends Annotation> T getAnnotationOnField(final RecordCompone
}

private static void validateAnnotations(final RecordComponent component, final int index) {
validateAnnotationNotPresentOnType(component.getDeclaringRecord(), org.bson.codecs.pojo.annotations.BsonDiscriminator.class);
validateAnnotationNotPresentOnConstructor(component.getDeclaringRecord(), org.bson.codecs.pojo.annotations.BsonCreator.class);
validateAnnotationNotPresentOnMethod(component.getDeclaringRecord(), org.bson.codecs.pojo.annotations.BsonCreator.class);
validateAnnotationNotPresentOnFieldOrAccessor(component, org.bson.codecs.pojo.annotations.BsonIgnore.class);
validateAnnotationNotPresentOnFieldOrAccessor(component, org.bson.codecs.pojo.annotations.BsonExtraElements.class);
validateAnnotationOnlyOnField(component, index, org.bson.codecs.pojo.annotations.BsonId.class);
validateAnnotationOnlyOnField(component, index, org.bson.codecs.pojo.annotations.BsonProperty.class);
validateAnnotationOnlyOnField(component, index, org.bson.codecs.pojo.annotations.BsonRepresentation.class);
validateAnnotationNotPresentOnType(component.getDeclaringRecord(), BsonDiscriminator.class);
validateAnnotationNotPresentOnConstructor(component.getDeclaringRecord(), BsonCreator.class);
validateAnnotationNotPresentOnMethod(component.getDeclaringRecord(), BsonCreator.class);
validateAnnotationNotPresentOnFieldOrAccessor(component, BsonIgnore.class);
validateAnnotationNotPresentOnFieldOrAccessor(component, BsonExtraElements.class);
validateAnnotationOnlyOnField(component, index, BsonId.class);
validateAnnotationOnlyOnField(component, index, BsonProperty.class);
validateAnnotationOnlyOnField(component, index, BsonRepresentation.class);
}

private static <T extends Annotation> void validateAnnotationNotPresentOnType(final Class<?> clazz,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.record.samples.TestRecordEmbedded;
import org.bson.codecs.record.samples.TestRecordParameterized;
import org.bson.codecs.record.samples.TestRecordWithDeprecatedAnnotations;
import org.bson.codecs.record.samples.TestRecordWithIllegalBsonCreatorOnConstructor;
import org.bson.codecs.record.samples.TestRecordWithIllegalBsonCreatorOnMethod;
import org.bson.codecs.record.samples.TestRecordWithIllegalBsonDiscriminatorOnRecord;
Expand Down Expand Up @@ -66,34 +65,6 @@

public class RecordCodecTest {

@Test
public void testRecordWithDeprecatedAnnotations() {
var codec = createRecordCodec(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
var identifier = new ObjectId();
var testRecord = new TestRecordWithDeprecatedAnnotations("Lucas", 14, List.of("soccer", "basketball"), identifier.toHexString());

var document = new BsonDocument();
var writer = new BsonDocumentWriter(document);

// when
codec.encode(writer, testRecord, EncoderContext.builder().build());

// then
assertEquals(
new BsonDocument("_id", new BsonObjectId(identifier))
.append("name", new BsonString("Lucas"))
.append("hobbies", new BsonArray(List.of(new BsonString("soccer"), new BsonString("basketball"))))
.append("a", new BsonInt32(14)),
document);
assertEquals("_id", document.getFirstKey());

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

// then
assertEquals(testRecord, decoded);
}

@Test
public void testRecordWithPojoAnnotations() {
var codec = createRecordCodec(TestRecordWithPojoAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
Expand Down Expand Up @@ -302,9 +273,9 @@ public void testRecordWithNestedParameterizedRecordWithDifferentlyOrderedTypePar

@Test
public void testRecordWithNulls() {
var codec = createRecordCodec(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
var codec = createRecordCodec(TestRecordWithPojoAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
var identifier = new ObjectId();
var testRecord = new TestRecordWithDeprecatedAnnotations(null, 14, null, identifier.toHexString());
var testRecord = new TestRecordWithPojoAnnotations(null, 14, null, identifier.toHexString());

var document = new BsonDocument();
var writer = new BsonDocumentWriter(document);
Expand All @@ -327,9 +298,9 @@ public void testRecordWithNulls() {

@Test
public void testRecordWithExtraData() {
var codec = createRecordCodec(TestRecordWithDeprecatedAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
var codec = createRecordCodec(TestRecordWithPojoAnnotations.class, Bson.DEFAULT_CODEC_REGISTRY);
var identifier = new ObjectId();
var testRecord = new TestRecordWithDeprecatedAnnotations("Felix", 13, List.of("rugby", "badminton"), identifier.toHexString());
var testRecord = new TestRecordWithPojoAnnotations("Felix", 13, List.of("rugby", "badminton"), identifier.toHexString());

var document = new BsonDocument("_id", new BsonObjectId(identifier))
.append("nationality", new BsonString("British"))
Expand Down

This file was deleted.