Skip to content

PojoCodec BsonId error message #1071

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
Jan 27, 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 @@ -166,6 +166,12 @@ private <T> void processCreatorAnnotation(final ClassModelBuilder<T> classModelB
PropertyModelBuilder<?> propertyModelBuilder = null;

if (isIdProperty) {
if (classModelBuilder.getIdPropertyName() == null) {
throw new CodecConfigurationException("A @BsonId annotation has been used with @BsonCreator "
+ "but there is no known Id property.\n"
+ "Please either use the @BsonProperty annotation in the creator or "
+ "annotate the corresponding property in the class with the @BsonId.");
}
propertyModelBuilder = classModelBuilder.getProperty(classModelBuilder.getIdPropertyName());
} else {
BsonProperty bsonProperty = properties.get(i);
Expand Down
7 changes: 7 additions & 0 deletions bson/src/test/unit/org/bson/codecs/pojo/ConventionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.bson.codecs.pojo.entities.conventions.AnnotationWithObjectIdModel;
import org.bson.codecs.pojo.entities.conventions.AnnotationWriteCollision;
import org.bson.codecs.pojo.entities.conventions.BsonIgnoreDuplicatePropertyMultipleTypes;
import org.bson.codecs.pojo.entities.conventions.CreatorConstructorNoKnownIdModel;
import org.bson.codecs.pojo.entities.conventions.CreatorInvalidConstructorModel;
import org.bson.codecs.pojo.entities.conventions.CreatorInvalidMethodModel;
import org.bson.codecs.pojo.entities.conventions.CreatorInvalidMethodReturnTypeModel;
Expand Down Expand Up @@ -226,6 +227,12 @@ public void testCreatorInvalidTypeMethodModel() {
.conventions(singletonList(ANNOTATION_CONVENTION)).build();
}

@Test(expected = CodecConfigurationException.class)
public void testCreatorConstructorNoKnownIdModel() {
ClassModel.builder(CreatorConstructorNoKnownIdModel.class)
.conventions(singletonList(ANNOTATION_CONVENTION)).build();
}

@Test(expected = CodecConfigurationException.class)
public void testBsonIgnoreDuplicatePropertyMultipleTypesModel() {
ClassModel.builder(BsonIgnoreDuplicatePropertyMultipleTypes.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.pojo.entities.conventions;

import org.bson.codecs.pojo.annotations.BsonCreator;
import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.codecs.pojo.annotations.BsonProperty;

import java.util.Objects;

public class CreatorConstructorNoKnownIdModel {
private final String stringField;
private final long longField;

@BsonCreator
public CreatorConstructorNoKnownIdModel(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the difference between this and the similar

  @BsonCreator
  public CreatorConstructorIdModel(@BsonId final String id, 
                                   @BsonProperty("integersField") final List<Integer> integerField,
                                   @BsonProperty("longField") final long longField)

just that in this existing one the property is named id and the BsonId annotation is ignored? Is that why this one doesn't throw an exception?

I wonder why we allowed ElementType.PARAMETER as a target for BsonId in the first place... This is the only place I see it used as a parameter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreatorConstructorIdModel has a public getId() method. The PojoCodec considers either id or _id properties as the id property. So in that case it can map to id.

I believe parameter support was added via a PR in JAVA-2599.

To be fair this is an edge case scenario and prevents a possible NPE.

@BsonId final String stringField,
@BsonProperty("longField") final long longField) {
this.stringField = stringField;
this.longField = longField;
}

public String getStringField() {
return stringField;
}

public long getLongField() {
return longField;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final CreatorConstructorNoKnownIdModel that = (CreatorConstructorNoKnownIdModel) o;
return longField == that.longField && Objects.equals(stringField, that.stringField);
}

@Override
public int hashCode() {
return Objects.hash(stringField, longField);
}

@Override
public String toString() {
return "CreatorConstructorNoKnownIdModel{"
+ ", stringField='" + stringField + '\''
+ ", longField=" + longField
+ '}';
}
}