Skip to content

Commit

Permalink
Discriminator added to all Queries when Polymorphism is not enabled
Browse files Browse the repository at this point in the history
 fixes #2833
  • Loading branch information
evanchooly committed May 25, 2024
1 parent 23b0583 commit b8affdc
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 361 deletions.
11 changes: 8 additions & 3 deletions core/src/main/java/dev/morphia/mapping/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,14 +541,19 @@ public void updateQueryWithDiscriminators(EntityModel model, Document query) {
&& !query.containsKey("_id")
&& !query.containsKey(model.getDiscriminatorKey())) {
List<String> values = new ArrayList<>();
values.add(model.getDiscriminator());
List<EntityModel> classesMappedToCollection = getClassesMappedToCollection(model.getCollectionName());
if (classesMappedToCollection.size() > 1 || config.enablePolymorphicQueries()) {
values.add(model.getDiscriminator());
}
if (config.enablePolymorphicQueries()) {
for (EntityModel subtype : model.getSubtypes()) {
values.add(subtype.getDiscriminator());
}
}
query.put(model.getDiscriminatorKey(),
new Document("$in", values));
if (!values.isEmpty()) {
query.put(model.getDiscriminatorKey(),
new Document("$in", values));
}
}
}

Expand Down
13 changes: 5 additions & 8 deletions core/src/main/java/dev/morphia/query/LegacyQuery.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.morphia.query;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -24,6 +23,7 @@
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.internal.MorphiaInternal;
import dev.morphia.internal.MorphiaInternals.DriverVersion;
import dev.morphia.mapping.Mapper;
import dev.morphia.mapping.codec.pojo.EntityModel;
import dev.morphia.query.internal.MorphiaCursor;
import dev.morphia.query.internal.MorphiaKeyCursor;
Expand Down Expand Up @@ -435,19 +435,16 @@ public Document getSort() {
@MorphiaInternal
public Document toDocument() {
final Document query = getQueryDocument();
EntityModel model = datastore.getMapper().getEntityModel(getEntityClass());
Mapper mapper = datastore.getMapper();
EntityModel model = mapper.getEntityModel(getEntityClass());
Entity entityAnnotation = model.getEntityAnnotation();
if (entityAnnotation != null && entityAnnotation.useDiscriminator()
&& !query.containsKey("_id")
&& !query.containsKey(model.getDiscriminatorKey())) {

List<String> values = new ArrayList<>();
values.add(model.getDiscriminator());
for (EntityModel subtype : datastore.getMapper().getEntityModel(getEntityClass()).getSubtypes()) {
values.add(subtype.getDiscriminator());
if (mapper.isMappable(getEntityClass())) {
mapper.updateQueryWithDiscriminators(mapper.getEntityModel(getEntityClass()), query);
}
query.put(model.getDiscriminatorKey(),
new Document("$in", values));
}
return query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public void testOr() {
of(
new MergingDocument("$or", of(new Document("name", "A"), new Document("name", "B"))),
new MergingDocument("$or", of(new Document("name", "C"), new Document("name", "D")))));
System.out.println("document = " + document.toJson(JSON_WRITER_SETTINGS));
System.out.println("expected = " + expected.toJson(JSON_WRITER_SETTINGS));
assertDocumentEquals(document, expected);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright (C) 2010 Olafur Gauti Gudmundsson
<p/>
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
<p/>
http://www.apache.org/licenses/LICENSE-2.0
<p/>
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 dev.morphia.test.models;

import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;

import org.bson.types.ObjectId;

@Entity
public class TestDiscriminatorEntity {
@Id
public ObjectId id;
public String something;

public ObjectId getId() {
return id;
}

public void setId(ObjectId id) {
this.id = id;
}
}
1 change: 1 addition & 0 deletions core/src/test/java/dev/morphia/test/models/TestEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class TestEntity {
@Id
public ObjectId id;
public String something;

public ObjectId getId() {
return id;
Expand Down
Loading

0 comments on commit b8affdc

Please sign in to comment.