Skip to content

Commit b770404

Browse files
committed
GH-2505 - Respect simple type and conversion service on DTO save.
Closes #2505
1 parent 963046d commit b770404

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.PreferredConstructor;
3030
import org.springframework.data.mapping.PreferredConstructor.Parameter;
3131
import org.springframework.data.mapping.model.ParameterValueProvider;
32+
import org.springframework.data.neo4j.core.convert.Neo4jConversionService;
3233
import org.springframework.data.util.ClassTypeInformation;
3334
import org.springframework.data.util.ReflectionUtils;
3435
import org.springframework.lang.Nullable;
@@ -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
@@ -57,6 +57,7 @@
5757
import org.springframework.data.neo4j.integration.issues.gh2415.BaseNodeEntity;
5858
import org.springframework.data.neo4j.integration.issues.gh2415.NodeEntity;
5959
import org.springframework.data.neo4j.integration.issues.gh2415.NodeWithDefinedCredentials;
60+
import org.springframework.data.neo4j.integration.shared.common.EntityWithPrimitiveConstructorArguments;
6061
import org.springframework.data.neo4j.integration.shared.common.Person;
6162
import org.springframework.data.neo4j.integration.shared.common.PersonWithAllConstructor;
6263
import org.springframework.data.neo4j.integration.shared.common.PersonWithAssignedId;
@@ -391,6 +392,15 @@ void saveProjectionShouldWork() {
391392
assertThat(person.getAddress()).isNotNull();
392393
}
393394

395+
@Test // GH-2505
396+
void savePrimitivesShouldWork() {
397+
EntityWithPrimitiveConstructorArguments entity = new EntityWithPrimitiveConstructorArguments(true, 42);
398+
EntityWithPrimitiveConstructorArguments savedEntity = neo4jTemplate.save(EntityWithPrimitiveConstructorArguments.class).one(entity);
399+
400+
assertThat(savedEntity.someIntValue).isEqualTo(entity.someIntValue);
401+
assertThat(savedEntity.someBooleanValue).isEqualTo(entity.someBooleanValue);
402+
}
403+
394404
@Test // GH-2215
395405
void saveAllProjectionShouldWork() {
396406

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)