Skip to content

Commit

Permalink
fix JAVA-5764
Browse files Browse the repository at this point in the history
  • Loading branch information
raffaeleflorio committed Jan 16, 2025
1 parent e022dbd commit cc9996c
Showing 1 changed file with 19 additions and 5 deletions.
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

0 comments on commit cc9996c

Please sign in to comment.