GH-1712 - Coerce constructor parameter the same way as OGM handles properties.

This closes #1712.
This commit is contained in:
Michael Simons
2021-01-08 16:43:01 +01:00
parent 88e6c304ad
commit c44eb9bf25
3 changed files with 72 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ import java.util.Collections;
import java.util.Map;
import org.neo4j.ogm.metadata.reflect.EntityAccessManager;
import org.neo4j.ogm.session.Utils;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.convert.EntityInstantiator;
import org.springframework.data.mapping.PreferredConstructor;
@@ -109,7 +110,8 @@ public class Neo4jOgmEntityInstantiatorAdapter implements org.neo4j.ogm.session.
? EntityAccessManager.merge(collectionType, value, new Object[] {}, elementType)
: EntityAccessManager.merge(collectionType, value, Collections.EMPTY_LIST, elementType);
}
return value;
return Utils.coerceTypes(parameter.getType().getType(), value);
}
}
}

View File

@@ -34,6 +34,7 @@ import org.neo4j.graphdb.Node;
import org.neo4j.harness.ServerControls;
import org.neo4j.harness.TestServerBuilders;
import org.neo4j.ogm.drivers.bolt.driver.BoltDriver;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Name;
@@ -42,6 +43,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.conversion.ogm618.CoercedNumericInCtor;
import org.springframework.data.neo4j.conversion.ogm618.MyNode;
import org.springframework.data.neo4j.conversion.ogm618.MyNodeRepository;
import org.springframework.data.neo4j.conversion.ogm618.ResultHolder;
@@ -71,13 +73,28 @@ public class Neo4jOgmEntityInstantiatorAdapterTests {
serverControls = TestServerBuilders.newInProcessBuilder()
.withProcedure(Neo4jOgmEntityInstantiatorAdapterTests.ListReturningThing.class)
.withFixture("CREATE (m:MyNode{name: 'All the', things: []})").newServer();
.withFixture("CREATE (m:MyNode{name: 'All the', things: []})")
.withFixture("CREATE (m:CoercedNumericInCtor{name: 'Whatever', lfdnr: 4711})")
.newServer();
boltURI = serverControls.boltURI();
}
@Autowired
private MyNodeRepository myNodeRepository;
@Autowired
private Session session;
@Test // GH-1712
public void longVsIntMustNotRelyOnConverter() {
Optional<CoercedNumericInCtor> optionalNode = session.loadAll(CoercedNumericInCtor.class).stream().findFirst();
assertThat(optionalNode).hasValueSatisfying(object -> {
assertThat(object.getLfdnr()).isEqualTo(4711);
assertThat(object.getName()).isEqualTo("Whatever");
});
}
@Test
public void ctorShouldHandleEmptyArrayFromAttributes() {

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2011-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.neo4j.conversion.ogm618;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
/**
* @author Michael J. Simons
*/
@NodeEntity
public class CoercedNumericInCtor {
@Id @GeneratedValue
private Long id;
private String name;
private Integer lfdnr;
public CoercedNumericInCtor(String name, Integer lfdnr) {
this.name = name;
this.lfdnr = lfdnr;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Integer getLfdnr() {
return lfdnr;
}
}