DATACOUCH-155 - Correctly escape property path.

Additionally to resolving a POJO attribute to JSON field name, the path conversion step will also escape each part of the doted path, in order to avoid problems with N1QL keywords.
This commit is contained in:
Simon Baslé
2015-07-31 17:47:39 +02:00
parent 2035a88dba
commit 60196095a6
4 changed files with 80 additions and 16 deletions

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2013-2015 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.couchbase.repository;
import static org.junit.Assert.assertTrue;
import java.util.Date;
import java.util.List;
import com.couchbase.client.java.Bucket;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.IntegrationTestApplicationConfig;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Simon Baslé
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = IntegrationTestApplicationConfig.class)
public class N1qlCrudRepositoryTests {
@Autowired
private Bucket client;
@Autowired
private CouchbaseTemplate template;
private PartyRepository partyRepository;
@Before
public void setup() throws Exception {
partyRepository = new CouchbaseRepositoryFactory(template).getRepository(PartyRepository.class);
}
@Test
public void shouldSaveObjectWithN1qlKeywordField() {
Party party = new Party("partyHasKeyword", "party", "desc is a N1QL keyword", new Date(), 40);
partyRepository.save(party);
List<Party> parties = partyRepository.findAllByDescriptionNotNull();
assertTrue(client.exists("partyHasKeyword"));
assertTrue(parties.contains(party));
}
}

View File

@@ -17,4 +17,6 @@ public interface PartyRepository extends CouchbaseRepository<Party, String> {
@View(designDocument = "party", viewName = "byDate")
List<Party> findFirst3ByEventDateGreaterThanEqual(Date targetDate);
List<Party> findAllByDescriptionNotNull();
}

View File

@@ -16,9 +16,7 @@
package org.springframework.data.couchbase.core.mapping;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.context.PersistentPropertyPath;
/**
* Represents a property part of an entity that needs to be persisted.
@@ -33,17 +31,4 @@ public interface CouchbasePersistentProperty extends PersistentProperty<Couchbas
* The field name can be different from the actual property name by using a custom annotation.
*/
String getFieldName();
/**
* A converter that can be used to extract the {@link #getFieldName() fieldName}, eg. when one wants
* a path from {@link PersistentPropertyPath#toDotPath(Converter)} made of field names.
*/
Converter<? super CouchbasePersistentProperty,String> FIELD_NAME = new Converter<CouchbasePersistentProperty, String>() {
@Override
public String convert(CouchbasePersistentProperty source) {
return source.getFieldName();
}
};
}

View File

@@ -32,6 +32,7 @@ import com.couchbase.client.java.query.dsl.path.LimitPath;
import com.couchbase.client.java.query.dsl.path.OrderByPath;
import com.couchbase.client.java.query.dsl.path.WherePath;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
import org.springframework.data.domain.Sort;
@@ -146,7 +147,7 @@ public class N1qlQueryCreator extends AbstractQueryCreator<LimitPath, Expression
ConvertingIterator parameterValues = new ConvertingIterator(iterator, converter);
//get the whole doted path with fieldNames instead of potentially wrong propNames
String fieldNamePath = path.toDotPath(CouchbasePersistentProperty.FIELD_NAME);
String fieldNamePath = path.toDotPath(FIELD_NAME_ESCAPED);
//deal with ignore case
boolean ignoreCase = false;
@@ -297,4 +298,15 @@ public class N1qlQueryCreator extends AbstractQueryCreator<LimitPath, Expression
return JsonArray.from(values);
}
/**
* A converter that can be used to extract the {@link CouchbasePersistentProperty#getFieldName() fieldName},
* eg. when one wants a path from {@link PersistentPropertyPath#toDotPath(Converter)} made of escaped field names.
*/
Converter<? super CouchbasePersistentProperty,String> FIELD_NAME_ESCAPED = new Converter<CouchbasePersistentProperty, String>() {
@Override
public String convert(CouchbasePersistentProperty source) {
return "`" + source.getFieldName() + "`";
}
};
}