Skip to content

DATAMONGO-1768 - Allow ignoring type restriction when issuing QBE. #496

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

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1768-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand All @@ -27,7 +27,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>2.0.0.BUILD-SNAPSHOT</springdata.commons>
<springdata.commons>2.0.0.DATACMNS-1140-SNAPSHOT</springdata.commons>
<mongo>3.4.2</mongo>
<mongo.reactivestreams>1.5.0</mongo.reactivestreams>
<jmh.version>1.19</jmh.version>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1768-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1768-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1768-SNAPSHOT</version>
</dependency>

<!-- reactive -->
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1768-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1768-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
import org.springframework.data.mongodb.core.query.MongoRegexCreator;
import org.springframework.data.mongodb.core.query.MongoRegexCreator.MatchMode;
import org.springframework.data.mongodb.core.query.SerializationUtils;
import org.springframework.data.mongodb.core.query.UntypedExampleMatcher;
import org.springframework.data.support.ExampleMatcherAccessor;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -91,7 +93,7 @@ public Document getMappedExample(Example<?> example, MongoPersistentEntity<?> en

Document reference = (Document) converter.convertToMongoType(example.getProbe());

if (entity.getIdProperty() != null) {
if (entity.getIdProperty() != null && ClassUtils.isAssignable(entity.getType(), example.getProbeType())) {

Object identifier = entity.getIdentifierAccessor(example.getProbe()).getIdentifier();
if (identifier == null) {
Expand All @@ -107,9 +109,7 @@ public Document getMappedExample(Example<?> example, MongoPersistentEntity<?> en
: new Document(SerializationUtils.flattenMap(reference));
Document result = example.getMatcher().isAllMatching() ? flattened : orConcatenate(flattened);

this.converter.getTypeMapper().writeTypeRestrictions(result, getTypesToMatch(example));

return result;
return updateTypeRestrictions(result, example);
}

private static Document orConcatenate(Document source) {
Expand Down Expand Up @@ -288,4 +288,43 @@ private static MatchMode toMatchMode(StringMatcher matcher) {
return MatchMode.DEFAULT;
}
}

private Document updateTypeRestrictions(Document query, Example example) {

Document result = new Document();

if (isTypeRestricting(example)) {

result.putAll(query);
this.converter.getTypeMapper().writeTypeRestrictions(result, getTypesToMatch(example));
return result;
}

for (Map.Entry<String, Object> entry : query.entrySet()) {
if (!this.converter.getTypeMapper().isTypeKey(entry.getKey())) {
result.put(entry.getKey(), entry.getValue());
}
}

return result;
}

private boolean isTypeRestricting(Example example) {

if (example.getMatcher() instanceof UntypedExampleMatcher) {
return false;
}

if (example.getMatcher().getIgnoredPaths().isEmpty()) {
return true;
}

for (String path : example.getMatcher().getIgnoredPaths()) {
if (this.converter.getTypeMapper().isTypeKey(path)) {
return false;
}
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
/*
* Copyright 2017 the original author or authors.
*
* 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.springframework.data.mongodb.core.query;

import lombok.EqualsAndHashCode;

import java.util.Set;

import org.springframework.data.domain.ExampleMatcher;
import org.springframework.util.Assert;

/**
* {@link ExampleMatcher} implementation for query by example (QBE). Unlike plain {@link ExampleMatcher} this untyped
* counterpart does not enforce a strict type match when executing the query. This allows to use totally unrelated
* example documents as references for querying collections as long as the used field/property names match.
*
* @author Christoph Strobl
* @since 2.0
*/
@EqualsAndHashCode
public class UntypedExampleMatcher implements ExampleMatcher {

private final ExampleMatcher delegate;

/**
* Creates new {@link UntypedExampleMatcher}.
*
* @param delegate must not be {@literal null}.
*/
private UntypedExampleMatcher(ExampleMatcher delegate) {

Assert.notNull(delegate, "Delegate must not be null!");
this.delegate = delegate;
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#matching()
*/
public static UntypedExampleMatcher matching() {
return new UntypedExampleMatcher(ExampleMatcher.matching());
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#matchingAny()
*/
public static UntypedExampleMatcher matchingAny() {
return new UntypedExampleMatcher(ExampleMatcher.matchingAny());
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#matchingAll()
*/
public static UntypedExampleMatcher matchingAll() {
return new UntypedExampleMatcher(ExampleMatcher.matchingAll());
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#withIgnorePaths(java.lang.String...)
*/
public UntypedExampleMatcher withIgnorePaths(String... ignoredPaths) {
return new UntypedExampleMatcher(delegate.withIgnorePaths(ignoredPaths));
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#withStringMatcher(java.lang.String)
*/
public UntypedExampleMatcher withStringMatcher(StringMatcher defaultStringMatcher) {
return new UntypedExampleMatcher(delegate.withStringMatcher(defaultStringMatcher));
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#withIgnoreCase()
*/
public UntypedExampleMatcher withIgnoreCase() {
return new UntypedExampleMatcher(delegate.withIgnoreCase());
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#withIgnoreCase(boolean)
*/
public UntypedExampleMatcher withIgnoreCase(boolean defaultIgnoreCase) {
return new UntypedExampleMatcher(delegate.withIgnoreCase(defaultIgnoreCase));
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#withMatcher(java.lang.String, org.springframework.data.domain.ExampleMatcher.MatcherConfigurer)
*/
public UntypedExampleMatcher withMatcher(String propertyPath,
MatcherConfigurer<GenericPropertyMatcher> matcherConfigurer) {
return new UntypedExampleMatcher(delegate.withMatcher(propertyPath, matcherConfigurer));
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#withMatcher(java.lang.String, org.springframework.data.domain.ExampleMatcher.GenericPropertyMatcher)
*/
public UntypedExampleMatcher withMatcher(String propertyPath, GenericPropertyMatcher genericPropertyMatcher) {
return new UntypedExampleMatcher(delegate.withMatcher(propertyPath, genericPropertyMatcher));
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#withTransformer(java.lang.String, org.springframework.data.domain.ExampleMatcher.PropertyValueTransformer)
*/
public UntypedExampleMatcher withTransformer(String propertyPath, PropertyValueTransformer propertyValueTransformer) {
return new UntypedExampleMatcher(delegate.withTransformer(propertyPath, propertyValueTransformer));
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#withIgnoreCase(java.lang.String...)
*/
public UntypedExampleMatcher withIgnoreCase(String... propertyPaths) {
return new UntypedExampleMatcher(delegate.withIgnoreCase(propertyPaths));
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#withIncludeNullValues()
*/
public UntypedExampleMatcher withIncludeNullValues() {
return new UntypedExampleMatcher(delegate.withIncludeNullValues());
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#withIgnoreNullValues()
*/
public UntypedExampleMatcher withIgnoreNullValues() {
return new UntypedExampleMatcher(delegate.withIgnoreNullValues());
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#withNullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler)
*/
public UntypedExampleMatcher withNullHandler(NullHandler nullHandler) {
return new UntypedExampleMatcher(delegate.withNullHandler(nullHandler));
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#getNullHandler()
*/
public NullHandler getNullHandler() {
return delegate.getNullHandler();
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#getDefaultStringMatcher()
*/
public StringMatcher getDefaultStringMatcher() {
return delegate.getDefaultStringMatcher();
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#isIgnoreCaseEnabled()
*/
public boolean isIgnoreCaseEnabled() {
return delegate.isIgnoreCaseEnabled();
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#isIgnoredPath()
*/
public boolean isIgnoredPath(String path) {
return delegate.isIgnoredPath(path);
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#getIgnoredPaths()
*/
public Set<String> getIgnoredPaths() {
return delegate.getIgnoredPaths();
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#getPropertySpecifiers()
*/
public PropertySpecifiers getPropertySpecifiers() {
return delegate.getPropertySpecifiers();
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#isAllMatching()
*/
public boolean isAllMatching() {
return delegate.isAllMatching();
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#isAnyMatching()
*/
public boolean isAnyMatching() {
return delegate.isAnyMatching();
}

/*
* (non-Javadoc)
* @see org.springframework.data.domain.ExampleMatcher#getMatchMode()
*/
public MatchMode getMatchMode() {
return delegate.getMatchMode();
}

}
Loading