Skip to content

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
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors
* 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.
Expand All @@ -16,17 +16,21 @@

package org.springframework.data.couchbase.core.convert;

import java.util.Collections;

import org.springframework.data.convert.DefaultTypeMapper;
import org.springframework.data.convert.TypeAliasAccessor;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.mapping.Alias;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.util.TypeInformation;

/**
* The Couchbase Type Mapper.
*
* @author Michael Nitschinger
* @author Mark Paluch
* @author Michael Reiche
*/
public class DefaultCouchbaseTypeMapper extends DefaultTypeMapper<CouchbaseDocument> implements CouchbaseTypeMapper {

Expand All @@ -43,7 +47,8 @@ public class DefaultCouchbaseTypeMapper extends DefaultTypeMapper<CouchbaseDocum
* @param typeKey the typeKey to use.
*/
public DefaultCouchbaseTypeMapper(final String typeKey) {
super(new CouchbaseDocumentTypeAliasAccessor(typeKey));
super(new CouchbaseDocumentTypeAliasAccessor(typeKey), (MappingContext) null,
Collections.singletonList(new TypeAwareTypeInformationMapper()));
this.typeKey = typeKey;
}

Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.data.couchbase.core.query.QueryCriteria;
import org.springframework.data.couchbase.core.query.StringQuery;
import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.Alias;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.repository.core.NamedQueries;
Expand All @@ -36,6 +37,8 @@
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
import org.springframework.data.repository.query.parser.Part;
import org.springframework.data.repository.query.parser.PartTree;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import com.couchbase.client.java.json.JsonArray;
Expand Down Expand Up @@ -81,8 +84,15 @@ public StringN1qlQueryCreator(final ParameterAccessor accessor, CouchbaseQueryMe
} else {
throw new IllegalArgumentException("query has no inline Query or named Query not found");
}
Class javaType = getType();
String typeValue = javaType.getName();
TypeInformation<?> typeInfo = ClassTypeInformation.from(javaType);
Alias alias = couchbaseConverter.getTypeAlias(typeInfo);
if (alias != null && alias.isPresent()) {
typeValue = alias.toString();
}
this.queryParser = new StringBasedN1qlQueryParser(queryString, queryMethod, bucketName, couchbaseConverter,
getTypeField(), getTypeValue(), accessor, spelExpressionParser, evaluationContextProvider);
getTypeField(), typeValue, accessor, spelExpressionParser, evaluationContextProvider);
Copy link
Collaborator Author

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.

this.parser = spelExpressionParser;
this.parsedExpression = this.queryParser.parsedExpression;
}
Expand All @@ -95,8 +105,8 @@ protected String getTypeField() {
return couchbaseConverter.getTypeKey();
}

protected String getTypeValue() {
return getQueryMethod().getEntityInformation().getJavaType().getName();
protected Class getType() {
return getQueryMethod().getEntityInformation().getJavaType();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.couchbase.core.mapping.Document;

/**
Expand All @@ -27,6 +28,7 @@
* @author Michael Reiche
*/
@Document
@TypeAlias("airport")
public class Airport extends ComparableEntity {
@Id String id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -180,6 +196,7 @@ void findByEnum() {
airportRepository.delete(vie);
}
}

@Test
public void testCas() {
User user = new User("1", "Dave", "Wilson");
Expand All @@ -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();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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);

Expand Down Expand Up @@ -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));
}

Expand Down