-
Notifications
You must be signed in to change notification settings - Fork 192
DefaultCouchbaseTypeMapper uses TypeAlias annotation if present. #1120
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
Merged
mikereiche
merged 1 commit into
master
from
datacouch_1119_default_mapper_recognizes_type_alias_annotation
Apr 6, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
.../java/org/springframework/data/couchbase/core/convert/TypeAwareTypeInformationMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2012-2021 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.couchbase.core.convert; | ||
|
||
import org.springframework.data.annotation.TypeAlias; | ||
import org.springframework.data.convert.SimpleTypeInformationMapper; | ||
import org.springframework.data.mapping.Alias; | ||
import org.springframework.data.util.TypeInformation; | ||
|
||
/** | ||
* TypeAwareTypeInformationMapper - leverages @TypeAlias | ||
* | ||
* @author Michael Reiche | ||
*/ | ||
public class TypeAwareTypeInformationMapper extends SimpleTypeInformationMapper { | ||
|
||
@Override | ||
public Alias createAliasFor(TypeInformation<?> type) { | ||
TypeAlias[] typeAlias = type.getType().getAnnotationsByType(TypeAlias.class); | ||
|
||
if (typeAlias.length == 1) { | ||
return Alias.of(typeAlias[0].value()); | ||
} | ||
|
||
return super.createAliasFor(type); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,6 @@ | |
import java.util.concurrent.Future; | ||
import java.util.stream.Collectors; | ||
|
||
import com.couchbase.client.java.query.QueryScanConsistency; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
@@ -43,6 +42,8 @@ | |
import org.springframework.data.couchbase.CouchbaseClientFactory; | ||
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration; | ||
import org.springframework.data.couchbase.core.CouchbaseTemplate; | ||
import org.springframework.data.couchbase.core.query.N1QLExpression; | ||
import org.springframework.data.couchbase.core.query.Query; | ||
import org.springframework.data.couchbase.core.query.QueryCriteria; | ||
import org.springframework.data.couchbase.domain.Address; | ||
import org.springframework.data.couchbase.domain.Airport; | ||
|
@@ -67,6 +68,7 @@ | |
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import com.couchbase.client.core.error.IndexExistsException; | ||
import com.couchbase.client.java.query.QueryScanConsistency; | ||
|
||
/** | ||
* Repository tests | ||
|
@@ -168,6 +170,20 @@ void findBySimpleProperty() { | |
} | ||
} | ||
|
||
@Test | ||
void findByTypeAlias() { | ||
Airport vie = null; | ||
try { | ||
vie = new Airport("airports::vie", "vie", "loww"); | ||
vie = airportRepository.save(vie); | ||
List<Airport> airports = couchbaseTemplate.findByQuery(Airport.class) | ||
.matching(new Query(QueryCriteria.where(N1QLExpression.x("_class")).is("airport"))).all(); | ||
assertFalse(airports.isEmpty(), "should have found aiport"); | ||
} finally { | ||
airportRepository.delete(vie); | ||
} | ||
} | ||
|
||
@Test | ||
void findByEnum() { | ||
Airport vie = null; | ||
|
@@ -180,6 +196,7 @@ void findByEnum() { | |
airportRepository.delete(vie); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCas() { | ||
User user = new User("1", "Dave", "Wilson"); | ||
|
@@ -200,6 +217,7 @@ void count() { | |
airportRepository.saveAll( | ||
Arrays.stream(iatas).map((iata) -> new Airport("airports::" + iata, iata, iata.toLowerCase(Locale.ROOT))) | ||
.collect(Collectors.toSet())); | ||
couchbaseTemplate.findByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have already been present. |
||
Long count = airportRepository.countFancyExpression(asList("JFK"), asList("jfk"), false); | ||
assertEquals(1, count); | ||
|
||
|
@@ -332,14 +350,15 @@ void deleteAllById() { | |
void couchbaseRepositoryQuery() throws Exception { | ||
User user = new User("1", "Dave", "Wilson"); | ||
userRepository.save(user); | ||
couchbaseTemplate.findByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).matching(QueryCriteria.where("firstname").is("Dave").and("`1`").is("`1`")).all(); | ||
couchbaseTemplate.findByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS) | ||
.matching(QueryCriteria.where("firstname").is("Dave").and("`1`").is("`1`")).all(); | ||
String input = "findByFirstname"; | ||
Method method = UserRepository.class.getMethod(input, String.class); | ||
CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, | ||
new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(), | ||
couchbaseTemplate.getConverter().getMappingContext()); | ||
CouchbaseRepositoryQuery query = new CouchbaseRepositoryQuery(couchbaseTemplate, queryMethod, null); | ||
List<User> users = (List<User>)query.execute(new String[] { "Dave" }); | ||
List<User> users = (List<User>) query.execute(new String[] { "Dave" }); | ||
assertEquals(user, users.get(0)); | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This had just been using the classname. It needs to use TypeAlias annotation if present.