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

Discriminator added to all Queries when Polymorphism is not enabled #3053

Merged
merged 2 commits into from
Jun 12, 2024
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
14 changes: 11 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,22 @@ 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());
// the discriminator is added here if size > 1 because, unless polymorphic queries are enabled, historically Morphia has only
// returned the type given to the query. adding the discriminator value here ensures that only the query type is returned
// unless polymorphic queries are explicitly enabled.
if (classesMappedToCollection.size() > 1 || config.enablePolymorphicQueries()) {
evanchooly marked this conversation as resolved.
Show resolved Hide resolved
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
Loading