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

fix JAVA-5764 #1610

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 19 additions & 5 deletions bson/src/main/org/bson/codecs/pojo/PojoCodecImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,10 @@ public void encode(final BsonWriter writer, final T value, final EncoderContext
writer.writeStartDocument();

encodeIdProperty(writer, value, encoderContext, classModel.getIdPropertyModelHolder());

if (classModel.useDiscriminator()) {
writer.writeString(classModel.getDiscriminatorKey(), classModel.getDiscriminator());
}
encodeDiscriminatorProperty(writer);

for (PropertyModel<?> propertyModel : classModel.getPropertyModels()) {
if (propertyModel.equals(classModel.getIdPropertyModel())) {
if (idProperty(propertyModel) || discriminatorProperty(propertyModel)) {
continue;
}
encodeProperty(writer, value, encoderContext, propertyModel);
Expand Down Expand Up @@ -140,6 +137,23 @@ private <S> void encodeIdProperty(final BsonWriter writer, final T instance, fin
}
}

private boolean idProperty(final PropertyModel<?> propertyModel) {
return propertyModel.equals(classModel.getIdPropertyModel());
}

private void encodeDiscriminatorProperty(final BsonWriter writer) {
if (classModel.useDiscriminator()) {
writer.writeString(classModel.getDiscriminatorKey(), classModel.getDiscriminator());
}
}

private boolean discriminatorProperty(final PropertyModel<?> propertyModel) {
if (classModel.useDiscriminator()) {
return propertyModel.getReadName().equals(classModel.getDiscriminatorKey());
}
return false;
}

private <S> void encodeProperty(final BsonWriter writer, final T instance, final EncoderContext encoderContext,
final PropertyModel<S> propertyModel) {
if (propertyModel != null && propertyModel.isReadable()) {
Expand Down