Skip to content

Commit 2e785ba

Browse files
committed
GH-2505 - Respect simple type and conversion service on DTO save.
Closes #2505
1 parent bd41a64 commit 2e785ba

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/main/java/org/springframework/data/neo4j/core/mapping/EntityFromDtoInstantiatingConverter.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.data.mapping.PersistentProperty;
3030
import org.springframework.data.mapping.PersistentPropertyAccessor;
3131
import org.springframework.data.mapping.model.ParameterValueProvider;
32+
import org.springframework.data.neo4j.core.convert.Neo4jConversionService;
3233
import org.springframework.data.neo4j.core.schema.TargetNode;
3334
import org.springframework.data.util.ClassTypeInformation;
3435
import org.springframework.data.util.ReflectionUtils;
@@ -131,9 +132,14 @@ Object getPropertyValueFor(PersistentProperty<?> targetProperty, PersistentEntit
131132
}
132133

133134
if (propertyValue != null && !targetPropertyType.isInstance(propertyValue)) {
134-
EntityFromDtoInstantiatingConverter<?> nestedConverter = converterCache.computeIfAbsent(targetProperty.getType(),
135-
t -> new EntityFromDtoInstantiatingConverter<>(t, context));
136-
return nestedConverter.convert(propertyValue);
135+
Neo4jConversionService conversionService = context.getConversionService();
136+
if (conversionService.isSimpleType(targetPropertyType)) {
137+
return conversionService.convert(propertyValue, targetPropertyType);
138+
} else {
139+
EntityFromDtoInstantiatingConverter<?> nestedConverter = converterCache.computeIfAbsent(targetProperty.getType(),
140+
t -> new EntityFromDtoInstantiatingConverter<>(t, context));
141+
return nestedConverter.convert(propertyValue);
142+
}
137143
}
138144

139145
return propertyValue;

src/test/java/org/springframework/data/neo4j/integration/imperative/Neo4jTemplateIT.java

+10
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.springframework.data.neo4j.integration.issues.gh2415.BaseNodeEntity;
6161
import org.springframework.data.neo4j.integration.issues.gh2415.NodeEntity;
6262
import org.springframework.data.neo4j.integration.issues.gh2415.NodeWithDefinedCredentials;
63+
import org.springframework.data.neo4j.integration.shared.common.EntityWithPrimitiveConstructorArguments;
6364
import org.springframework.data.neo4j.integration.shared.common.Person;
6465
import org.springframework.data.neo4j.integration.shared.common.PersonWithAllConstructor;
6566
import org.springframework.data.neo4j.integration.shared.common.PersonWithAssignedId;
@@ -404,6 +405,15 @@ void saveProjectionShouldWork() {
404405
assertThat(person.getAddress()).isNotNull();
405406
}
406407

408+
@Test // GH-2505
409+
void savePrimitivesShouldWork() {
410+
EntityWithPrimitiveConstructorArguments entity = new EntityWithPrimitiveConstructorArguments(true, 42);
411+
EntityWithPrimitiveConstructorArguments savedEntity = neo4jTemplate.save(EntityWithPrimitiveConstructorArguments.class).one(entity);
412+
413+
assertThat(savedEntity.someIntValue).isEqualTo(entity.someIntValue);
414+
assertThat(savedEntity.someBooleanValue).isEqualTo(entity.someBooleanValue);
415+
}
416+
407417
@Test // GH-2215
408418
void saveAllProjectionShouldWork() {
409419

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2011-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.shared.common;
17+
18+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
19+
import org.springframework.data.neo4j.core.schema.Id;
20+
import org.springframework.data.neo4j.core.schema.Node;
21+
22+
/**
23+
* Reproducer class for Gh-2505
24+
*/
25+
@Node
26+
public class EntityWithPrimitiveConstructorArguments {
27+
@Id @GeneratedValue public Long id;
28+
29+
public final boolean someBooleanValue;
30+
public final int someIntValue;
31+
32+
public EntityWithPrimitiveConstructorArguments(boolean someBooleanValue, int someIntValue) {
33+
this.someBooleanValue = someBooleanValue;
34+
this.someIntValue = someIntValue;
35+
}
36+
}

0 commit comments

Comments
 (0)