Skip to content

Commit c44eb9b

Browse files
GH-1712 - Coerce constructor parameter the same way as OGM handles properties.
This closes #1712.
1 parent 88e6c30 commit c44eb9b

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

spring-data-neo4j/src/main/java/org/springframework/data/neo4j/conversion/Neo4jOgmEntityInstantiatorAdapter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121

2222
import org.neo4j.ogm.metadata.reflect.EntityAccessManager;
23+
import org.neo4j.ogm.session.Utils;
2324
import org.springframework.core.convert.ConversionService;
2425
import org.springframework.data.convert.EntityInstantiator;
2526
import org.springframework.data.mapping.PreferredConstructor;
@@ -109,7 +110,8 @@ private Object extractParameterValue(PreferredConstructor.Parameter parameter) {
109110
? EntityAccessManager.merge(collectionType, value, new Object[] {}, elementType)
110111
: EntityAccessManager.merge(collectionType, value, Collections.EMPTY_LIST, elementType);
111112
}
112-
return value;
113+
114+
return Utils.coerceTypes(parameter.getType().getType(), value);
113115
}
114116
}
115117
}

spring-data-neo4j/src/test/java/org/springframework/data/neo4j/conversion/Neo4jOgmEntityInstantiatorAdapterTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.neo4j.harness.ServerControls;
3535
import org.neo4j.harness.TestServerBuilders;
3636
import org.neo4j.ogm.drivers.bolt.driver.BoltDriver;
37+
import org.neo4j.ogm.session.Session;
3738
import org.neo4j.ogm.session.SessionFactory;
3839
import org.neo4j.procedure.Context;
3940
import org.neo4j.procedure.Name;
@@ -42,6 +43,7 @@
4243
import org.springframework.context.annotation.Bean;
4344
import org.springframework.context.annotation.ComponentScan;
4445
import org.springframework.context.annotation.Configuration;
46+
import org.springframework.data.neo4j.conversion.ogm618.CoercedNumericInCtor;
4547
import org.springframework.data.neo4j.conversion.ogm618.MyNode;
4648
import org.springframework.data.neo4j.conversion.ogm618.MyNodeRepository;
4749
import org.springframework.data.neo4j.conversion.ogm618.ResultHolder;
@@ -71,13 +73,28 @@ public static void initializeNeo4j() {
7173

7274
serverControls = TestServerBuilders.newInProcessBuilder()
7375
.withProcedure(Neo4jOgmEntityInstantiatorAdapterTests.ListReturningThing.class)
74-
.withFixture("CREATE (m:MyNode{name: 'All the', things: []})").newServer();
76+
.withFixture("CREATE (m:MyNode{name: 'All the', things: []})")
77+
.withFixture("CREATE (m:CoercedNumericInCtor{name: 'Whatever', lfdnr: 4711})")
78+
.newServer();
7579
boltURI = serverControls.boltURI();
7680
}
7781

7882
@Autowired
7983
private MyNodeRepository myNodeRepository;
8084

85+
@Autowired
86+
private Session session;
87+
88+
@Test // GH-1712
89+
public void longVsIntMustNotRelyOnConverter() {
90+
91+
Optional<CoercedNumericInCtor> optionalNode = session.loadAll(CoercedNumericInCtor.class).stream().findFirst();
92+
assertThat(optionalNode).hasValueSatisfying(object -> {
93+
assertThat(object.getLfdnr()).isEqualTo(4711);
94+
assertThat(object.getName()).isEqualTo("Whatever");
95+
});
96+
}
97+
8198
@Test
8299
public void ctorShouldHandleEmptyArrayFromAttributes() {
83100

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2011-2021 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.conversion.ogm618;
17+
18+
import org.neo4j.ogm.annotation.GeneratedValue;
19+
import org.neo4j.ogm.annotation.Id;
20+
import org.neo4j.ogm.annotation.NodeEntity;
21+
22+
/**
23+
* @author Michael J. Simons
24+
*/
25+
@NodeEntity
26+
public class CoercedNumericInCtor {
27+
28+
@Id @GeneratedValue
29+
private Long id;
30+
31+
private String name;
32+
33+
private Integer lfdnr;
34+
35+
public CoercedNumericInCtor(String name, Integer lfdnr) {
36+
this.name = name;
37+
this.lfdnr = lfdnr;
38+
}
39+
40+
public Long getId() {
41+
return id;
42+
}
43+
44+
public String getName() {
45+
return name;
46+
}
47+
48+
public Integer getLfdnr() {
49+
return lfdnr;
50+
}
51+
}

0 commit comments

Comments
 (0)