Browse Source
allow resolution of list of dbrefs. Resolve dbref of dbref. cycles gonna be a problem here. remove outdated code carried forwardissue/2496
8 changed files with 453 additions and 19 deletions
@ -0,0 +1,95 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2023 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 |
||||||
|
* |
||||||
|
* https://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; |
||||||
|
|
||||||
|
import reactor.core.publisher.Flux; |
||||||
|
import reactor.core.publisher.Mono; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
import java.util.Map.Entry; |
||||||
|
|
||||||
|
import org.bson.Document; |
||||||
|
import org.springframework.data.mongodb.core.convert.ReactiveDbRefResolver; |
||||||
|
|
||||||
|
import com.mongodb.DBRef; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Christoph Strobl |
||||||
|
* @since 4.1 |
||||||
|
*/ |
||||||
|
class ReactiveValueResolver { |
||||||
|
|
||||||
|
static Mono<Document> prepareDbRefResolution(Mono<Document> root, ReactiveDbRefResolver dbRefResolver) { |
||||||
|
return root.flatMap(source -> { |
||||||
|
for (Entry<String, Object> entry : source.entrySet()) { |
||||||
|
Object value = entry.getValue(); |
||||||
|
if (value instanceof DBRef dbRef) { |
||||||
|
return prepareDbRefResolution(dbRefResolver.initFetch(dbRef).defaultIfEmpty(new Document()) |
||||||
|
.flatMap(it -> prepareDbRefResolution(Mono.just(it), dbRefResolver)).map(resolved -> { |
||||||
|
source.put(entry.getKey(), resolved.isEmpty() ? null : resolved); |
||||||
|
return source; |
||||||
|
}), dbRefResolver); |
||||||
|
} |
||||||
|
if (value instanceof Document nested) { |
||||||
|
return prepareDbRefResolution(Mono.just(nested), dbRefResolver).map(it -> { |
||||||
|
source.put(entry.getKey(), it); |
||||||
|
return source; |
||||||
|
}); |
||||||
|
} |
||||||
|
if (value instanceof List<?> list) { |
||||||
|
return Flux.fromIterable(list).concatMap(it -> { |
||||||
|
if (it instanceof DBRef dbRef) { |
||||||
|
return prepareDbRefResolution(dbRefResolver.initFetch(dbRef), dbRefResolver); |
||||||
|
} |
||||||
|
if (it instanceof Document document) { |
||||||
|
return prepareDbRefResolution(Mono.just(document), dbRefResolver); |
||||||
|
} |
||||||
|
return Mono.just(it); |
||||||
|
}).collectList().map(resolved -> { |
||||||
|
source.put(entry.getKey(), resolved.isEmpty() ? null : resolved); |
||||||
|
return source; |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
return Mono.just(source); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
public Mono<Document> resolveValues(Mono<Document> document) { |
||||||
|
|
||||||
|
return document.flatMap(source -> { |
||||||
|
for (Entry<String, Object> entry : source.entrySet()) { |
||||||
|
Object val = entry.getValue(); |
||||||
|
if (val instanceof Mono<?> valueMono) { |
||||||
|
return valueMono.flatMap(value -> { |
||||||
|
source.put(entry.getKey(), value); |
||||||
|
return resolveValues(Mono.just(source)); |
||||||
|
}); |
||||||
|
} |
||||||
|
if (entry.getValue()instanceof Document nested) { |
||||||
|
return resolveValues(Mono.just(nested)).map(it -> { |
||||||
|
source.put(entry.getKey(), it); |
||||||
|
return source; |
||||||
|
}); |
||||||
|
} |
||||||
|
if (entry.getValue() instanceof List<?>) { |
||||||
|
// do traverse list
|
||||||
|
} |
||||||
|
} |
||||||
|
return Mono.just(source); |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,81 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2023 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 |
||||||
|
* |
||||||
|
* https://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.convert; |
||||||
|
|
||||||
|
import reactor.core.publisher.Mono; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import org.bson.Document; |
||||||
|
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory; |
||||||
|
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; |
||||||
|
import org.springframework.lang.Nullable; |
||||||
|
import org.springframework.util.StringUtils; |
||||||
|
|
||||||
|
import com.mongodb.DBRef; |
||||||
|
import com.mongodb.reactivestreams.client.MongoDatabase; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Christoph Strobl |
||||||
|
* @since 4.1 |
||||||
|
*/ |
||||||
|
public class DefaultReactiveDbRefResolver implements ReactiveDbRefResolver { |
||||||
|
|
||||||
|
ReactiveMongoDatabaseFactory dbFactory; |
||||||
|
|
||||||
|
public DefaultReactiveDbRefResolver(ReactiveMongoDatabaseFactory dbFactory) { |
||||||
|
this.dbFactory = dbFactory; |
||||||
|
} |
||||||
|
|
||||||
|
@Nullable |
||||||
|
@Override |
||||||
|
public Mono<Object> resolveDbRef(MongoPersistentProperty property, @Nullable DBRef dbref, |
||||||
|
DbRefResolverCallback callback, DbRefProxyHandler proxyHandler) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
@Nullable |
||||||
|
@Override |
||||||
|
public Document fetch(DBRef dbRef) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public List<Document> bulkFetch(List<DBRef> dbRefs) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
|
||||||
|
@Nullable |
||||||
|
@Override |
||||||
|
public Mono<Document> initFetch(DBRef dbRef) { |
||||||
|
|
||||||
|
Mono<MongoDatabase> mongoDatabase = StringUtils.hasText(dbRef.getDatabaseName()) |
||||||
|
? dbFactory.getMongoDatabase(dbRef.getDatabaseName()) |
||||||
|
: dbFactory.getMongoDatabase(); |
||||||
|
return mongoDatabase |
||||||
|
.flatMap(db -> Mono.from(db.getCollection(dbRef.getCollectionName()).find(new Document("_id", dbRef.getId())))); |
||||||
|
} |
||||||
|
|
||||||
|
@Nullable |
||||||
|
@Override |
||||||
|
public Mono<Object> resolveReference(MongoPersistentProperty property, Object source, |
||||||
|
ReferenceLookupDelegate referenceLookupDelegate, MongoEntityReader entityReader) { |
||||||
|
if (source instanceof DBRef dbRef) { |
||||||
|
|
||||||
|
} |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,38 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2023 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 |
||||||
|
* |
||||||
|
* https://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.convert; |
||||||
|
|
||||||
|
import com.mongodb.DBRef; |
||||||
|
import org.bson.Document; |
||||||
|
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; |
||||||
|
import org.springframework.lang.Nullable; |
||||||
|
import reactor.core.publisher.Mono; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Christoph Strobl |
||||||
|
* @since 4.1 |
||||||
|
*/ |
||||||
|
public interface ReactiveDbRefResolver extends DbRefResolver { |
||||||
|
|
||||||
|
@Nullable |
||||||
|
default Mono<Document> initFetch(DBRef dbRef) { |
||||||
|
return Mono.justOrEmpty(fetch(dbRef)); |
||||||
|
} |
||||||
|
|
||||||
|
Mono<Object> resolveReference(MongoPersistentProperty property, Object source, ReferenceLookupDelegate referenceLookupDelegate, MongoEntityReader entityReader); |
||||||
|
|
||||||
|
Mono<Object> resolveDbRef(MongoPersistentProperty property, @Nullable DBRef dbref, DbRefResolverCallback callback, DbRefProxyHandler proxyHandler); |
||||||
|
} |
||||||
@ -0,0 +1,181 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2023 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 |
||||||
|
* |
||||||
|
* https://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; |
||||||
|
|
||||||
|
import lombok.EqualsAndHashCode; |
||||||
|
import lombok.ToString; |
||||||
|
import reactor.test.StepVerifier; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
import org.junit.jupiter.api.extension.ExtendWith; |
||||||
|
import org.springframework.data.mongodb.core.mapping.DBRef; |
||||||
|
import org.springframework.data.mongodb.core.query.Criteria; |
||||||
|
import org.springframework.data.mongodb.test.util.Assertions; |
||||||
|
import org.springframework.data.mongodb.test.util.Client; |
||||||
|
import org.springframework.data.mongodb.test.util.MongoClientExtension; |
||||||
|
|
||||||
|
import com.mongodb.reactivestreams.client.MongoClient; |
||||||
|
import com.mongodb.reactivestreams.client.MongoClients; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Christoph Strobl |
||||||
|
*/ |
||||||
|
@ExtendWith(MongoClientExtension.class) |
||||||
|
public class ReactiveDbRefTests { |
||||||
|
|
||||||
|
private static final String DB_NAME = "reactive-dbref-tests"; |
||||||
|
private static @Client MongoClient client; |
||||||
|
|
||||||
|
ReactiveMongoTemplate template = new ReactiveMongoTemplate(MongoClients.create(), DB_NAME); |
||||||
|
MongoTemplate syncTemplate = new MongoTemplate(com.mongodb.client.MongoClients.create(), DB_NAME); |
||||||
|
|
||||||
|
@Test // GH-2496
|
||||||
|
void loadDbRef() { |
||||||
|
|
||||||
|
Bar barSource = new Bar(); |
||||||
|
barSource.id = "bar-1"; |
||||||
|
barSource.value = "bar-1-value"; |
||||||
|
syncTemplate.save(barSource); |
||||||
|
|
||||||
|
Foo fooSource = new Foo(); |
||||||
|
fooSource.id = "foo-1"; |
||||||
|
fooSource.name = "foo-1-name"; |
||||||
|
fooSource.bar = barSource; |
||||||
|
syncTemplate.save(fooSource); |
||||||
|
|
||||||
|
template.query(Foo.class).matching(Criteria.where("id").is(fooSource.id)).first().as(StepVerifier::create) |
||||||
|
.consumeNextWith(foo -> { |
||||||
|
Assertions.assertThat(foo.bar).isEqualTo(barSource); |
||||||
|
}).verifyComplete(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test // GH-2496
|
||||||
|
void loadListOFDbRef() { |
||||||
|
|
||||||
|
Bar bar1Source = new Bar(); |
||||||
|
bar1Source.id = "bar-1"; |
||||||
|
bar1Source.value = "bar-1-value"; |
||||||
|
syncTemplate.save(bar1Source); |
||||||
|
|
||||||
|
Bar bar2Source = new Bar(); |
||||||
|
bar2Source.id = "bar-1"; |
||||||
|
bar2Source.value = "bar-1-value"; |
||||||
|
syncTemplate.save(bar2Source); |
||||||
|
|
||||||
|
Foo fooSource = new Foo(); |
||||||
|
fooSource.id = "foo-1"; |
||||||
|
fooSource.name = "foo-1-name"; |
||||||
|
fooSource.bars = List.of(bar1Source, bar2Source); |
||||||
|
syncTemplate.save(fooSource); |
||||||
|
|
||||||
|
template.query(Foo.class).matching(Criteria.where("id").is(fooSource.id)).first().as(StepVerifier::create) |
||||||
|
.consumeNextWith(foo -> { |
||||||
|
Assertions.assertThat(foo.bars).containsExactly(bar1Source, bar2Source); |
||||||
|
}).verifyComplete(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test // GH-2496
|
||||||
|
void loadDbRefHoldingJetAnotherOne() { |
||||||
|
|
||||||
|
Roo rooSource = new Roo(); |
||||||
|
rooSource.id = "roo-1"; |
||||||
|
rooSource.name = "roo-the-kangaroo"; |
||||||
|
syncTemplate.save(rooSource); |
||||||
|
|
||||||
|
Bar barSource = new Bar(); |
||||||
|
barSource.id = "bar-1"; |
||||||
|
barSource.value = "bar-1-value"; |
||||||
|
barSource.roo = rooSource; |
||||||
|
syncTemplate.save(barSource); |
||||||
|
|
||||||
|
Foo fooSource = new Foo(); |
||||||
|
fooSource.id = "foo-1"; |
||||||
|
fooSource.name = "foo-1-name"; |
||||||
|
fooSource.bar = barSource; |
||||||
|
syncTemplate.save(fooSource); |
||||||
|
|
||||||
|
template.query(Foo.class).matching(Criteria.where("id").is(fooSource.id)).first().as(StepVerifier::create) |
||||||
|
.consumeNextWith(foo -> { |
||||||
|
Assertions.assertThat(foo.bar).isEqualTo(barSource); |
||||||
|
Assertions.assertThat(foo.bar.roo).isEqualTo(rooSource); |
||||||
|
}).verifyComplete(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test // GH-2496
|
||||||
|
void loadListOfDbRefHoldingJetAnotherOne() { |
||||||
|
|
||||||
|
Roo rooSource = new Roo(); |
||||||
|
rooSource.id = "roo-1"; |
||||||
|
rooSource.name = "roo-the-kangaroo"; |
||||||
|
syncTemplate.save(rooSource); |
||||||
|
|
||||||
|
Bar bar1Source = new Bar(); |
||||||
|
bar1Source.id = "bar-1"; |
||||||
|
bar1Source.value = "bar-1-value"; |
||||||
|
bar1Source.roo = rooSource; |
||||||
|
syncTemplate.save(bar1Source); |
||||||
|
|
||||||
|
Bar bar2Source = new Bar(); |
||||||
|
bar2Source.id = "bar-2"; |
||||||
|
bar2Source.value = "bar-2-value"; |
||||||
|
syncTemplate.save(bar2Source); |
||||||
|
|
||||||
|
Foo fooSource = new Foo(); |
||||||
|
fooSource.id = "foo-1"; |
||||||
|
fooSource.name = "foo-1-name"; |
||||||
|
fooSource.bars = List.of(bar1Source, bar2Source); |
||||||
|
syncTemplate.save(fooSource); |
||||||
|
|
||||||
|
template.query(Foo.class).matching(Criteria.where("id").is(fooSource.id)).first().as(StepVerifier::create) |
||||||
|
.consumeNextWith(foo -> { |
||||||
|
Assertions.assertThat(foo.bars).containsExactly(bar1Source, bar2Source); |
||||||
|
}).verifyComplete(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@ToString |
||||||
|
static class Foo { |
||||||
|
String id; |
||||||
|
String name; |
||||||
|
|
||||||
|
@DBRef //
|
||||||
|
Bar bar; |
||||||
|
|
||||||
|
@DBRef //
|
||||||
|
List<Bar> bars; |
||||||
|
} |
||||||
|
|
||||||
|
@ToString |
||||||
|
@EqualsAndHashCode |
||||||
|
static class Bar { |
||||||
|
String id; |
||||||
|
String value; |
||||||
|
|
||||||
|
@DBRef Roo roo; |
||||||
|
} |
||||||
|
|
||||||
|
@ToString |
||||||
|
@EqualsAndHashCode |
||||||
|
static class Roo { |
||||||
|
String id; |
||||||
|
String name; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2023 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; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Christoph Strobl |
||||||
|
*/ |
||||||
|
public class ReactiveValueResolverUnitTests { |
||||||
|
|
||||||
|
// TODO: lots of tests
|
||||||
|
} |
||||||
Loading…
Reference in new issue