Skip to content

fix(object-mapping): try making record components accessible #1681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 60 additions & 14 deletions driver/src/main/java/org/neo4j/driver/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.neo4j.driver.internal.util.Extract.assertParameter;
import static org.neo4j.driver.internal.util.Iterables.newHashMapWithSize;

import java.lang.reflect.AccessibleObject;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -247,6 +248,7 @@ public static Value value(Object value) {

/**
* Returns an array of values from object vararg.
*
* @param input the object value(s)
* @return the array of values
*/
Expand All @@ -256,6 +258,7 @@ public static Value[] values(final Object... input) {

/**
* Returns a value from value vararg.
*
* @param input the value(s)
* @return the value
*/
Expand All @@ -265,6 +268,7 @@ public static Value value(Value... input) {

/**
* Returns a value from byte vararg.
*
* @param input the byte value(s)
* @return the value
*/
Expand All @@ -274,6 +278,7 @@ public static Value value(byte... input) {

/**
* Returns a value from string vararg.
*
* @param input the string value(s)
* @return the value
*/
Expand All @@ -286,6 +291,7 @@ public static Value value(String... input) {

/**
* Returns a value from boolean vararg.
*
* @param input the boolean value(s)
* @return the value
*/
Expand All @@ -298,6 +304,7 @@ public static Value value(boolean... input) {

/**
* Returns a value from char vararg.
*
* @param input the char value(s)
* @return the value
*/
Expand All @@ -310,6 +317,7 @@ public static Value value(char... input) {

/**
* Returns a value from long vararg.
*
* @param input the long value(s)
* @return the value
*/
Expand All @@ -322,6 +330,7 @@ public static Value value(long... input) {

/**
* Returns a value from short vararg.
*
* @param input the short value(s)
* @return the value
*/
Expand All @@ -331,8 +340,10 @@ public static Value value(short... input) {
.collect(Collectors.toCollection(() -> new ArrayList<>(input.length)));
return new ListValue(values);
}

/**
* Returns a value from int vararg.
*
* @param input the int value(s)
* @return the value
*/
Expand All @@ -342,8 +353,10 @@ public static Value value(int... input) {
.collect(Collectors.toCollection(() -> new ArrayList<>(input.length)));
return new ListValue(values);
}

/**
* Returns a value from double vararg.
*
* @param input the double value(s)
* @return the value
*/
Expand All @@ -356,6 +369,7 @@ public static Value value(double... input) {

/**
* Returns a value from float vararg.
*
* @param input the float value(s)
* @return the value
*/
Expand All @@ -368,6 +382,7 @@ public static Value value(float... input) {

/**
* Returns a value from list of objects.
*
* @param vals the list of objects
* @return the value
*/
Expand All @@ -379,6 +394,7 @@ public static Value value(List<Object> vals) {

/**
* Returns a value from iterable of objects.
*
* @param val the iterable of objects
* @return the value
*/
Expand All @@ -388,6 +404,7 @@ public static Value value(Iterable<Object> val) {

/**
* Returns a value from iterator of objects.
*
* @param val the iterator of objects
* @return the value
*/
Expand All @@ -401,6 +418,7 @@ public static Value value(Iterator<Object> val) {

/**
* Returns a value from stream of objects.
*
* @param stream the stream of objects
* @return the value
*/
Expand Down Expand Up @@ -463,14 +481,17 @@ public static Value value(Stream<Object> stream) {
* limitations on how those are supported by the database. Please read the Neo4j Cypher Manual for more up-to-date
* details. For example, at the time of writing, it is not possible to store maps as properties
* (see the following <a href="https://neo4j.com/docs/cypher-manual/current/values-and-types/property-structural-constructed/#constructed-types">page</a>).
* <p>
* If accessors for record components are not accessible, the driver will try to make them accessible using
* {@link AccessibleObject#trySetAccessible()} and will emit an error if this does not succeed.
*
* @param record the record to map
* @return the map value
* @throws ClientException when mapping fails
* @see TypeSystem#MAP()
* @see java.lang.Record
* @see java.lang.reflect.RecordComponent
* @see Property
* @throws ClientException when mapping fails
* @since 5.28.5
*/
@Preview(name = "Object mapping")
Expand All @@ -483,11 +504,16 @@ public static Value value(java.lang.Record record) {
var isVector = recordComponent.getAnnotation(org.neo4j.driver.mapping.Vector.class) != null;
Value value;
try {
var objectValue = recordComponent.getAccessor().invoke(record);
var accessor = recordComponent.getAccessor();
if (!accessor.canAccess(record) && !accessor.trySetAccessible()) {
throw new IllegalStateException(
"Failed to make record component '%s' accessible".formatted(recordComponent.getName()));
}
var objectValue = accessor.invoke(record);
value = (objectValue != null) ? isVector ? vector(objectValue) : value(objectValue) : null;
} catch (Throwable throwable) {
var message = "Failed to map '%s' property to value during mapping '%s' to map value"
.formatted(property, record.getClass().getCanonicalName());
.formatted(property, record.getClass().getName());
throw new ClientException(
GqlStatusError.UNKNOWN.getStatus(),
GqlStatusError.UNKNOWN.getStatusDescription(message),
Expand All @@ -505,6 +531,7 @@ public static Value value(java.lang.Record record) {

/**
* Returns a value from char.
*
* @param val the char value
* @return the value
*/
Expand All @@ -514,6 +541,7 @@ public static Value value(final char val) {

/**
* Returns a value from string.
*
* @param val the string value
* @return the value
*/
Expand All @@ -523,6 +551,7 @@ public static Value value(final String val) {

/**
* Returns a value from long.
*
* @param val the long value
* @return the value
*/
Expand All @@ -532,6 +561,7 @@ public static Value value(final long val) {

/**
* Returns a value from int.
*
* @param val the int value
* @return the value
*/
Expand All @@ -541,6 +571,7 @@ public static Value value(final int val) {

/**
* Returns a value from double.
*
* @param val the double value
* @return the value
*/
Expand All @@ -550,6 +581,7 @@ public static Value value(final double val) {

/**
* Returns a value from boolean.
*
* @param val the boolean value
* @return the value
*/
Expand All @@ -559,6 +591,7 @@ public static Value value(final boolean val) {

/**
* Returns a value from string to object map.
*
* @param val the string to object map
* @return the value
*/
Expand All @@ -572,6 +605,7 @@ public static Value value(final Map<String, Object> val) {

/**
* Returns a value from local date.
*
* @param localDate the local date value
* @return the value
*/
Expand All @@ -581,6 +615,7 @@ public static Value value(LocalDate localDate) {

/**
* Returns a value from offset time.
*
* @param offsetTime the offset time value
* @return the value
*/
Expand All @@ -590,6 +625,7 @@ public static Value value(OffsetTime offsetTime) {

/**
* Returns a value from local time.
*
* @param localTime the local time value
* @return the value
*/
Expand All @@ -599,6 +635,7 @@ public static Value value(LocalTime localTime) {

/**
* Returns a value from local date time.
*
* @param localDateTime the local date time value
* @return the value
*/
Expand All @@ -608,6 +645,7 @@ public static Value value(LocalDateTime localDateTime) {

/**
* Returns a value from offset date time.
*
* @param offsetDateTime the offset date time value
* @return the value
*/
Expand All @@ -617,6 +655,7 @@ public static Value value(OffsetDateTime offsetDateTime) {

/**
* Returns a value from zoned date time.
*
* @param zonedDateTime the zoned date time value
* @return the value
*/
Expand All @@ -626,6 +665,7 @@ public static Value value(ZonedDateTime zonedDateTime) {

/**
* Returns a value from period.
*
* @param period the period value
* @return the value
*/
Expand All @@ -635,6 +675,7 @@ public static Value value(Period period) {

/**
* Returns a value from duration.
*
* @param duration the duration value
* @return the value
*/
Expand All @@ -644,9 +685,10 @@ public static Value value(Duration duration) {

/**
* Returns a value from month, day, seconds and nanoseconds values.
* @param months the month value
* @param days the day value
* @param seconds the seconds value
*
* @param months the month value
* @param days the day value
* @param seconds the seconds value
* @param nanoseconds the nanoseconds value
* @return the value
*/
Expand All @@ -656,6 +698,7 @@ public static Value isoDuration(long months, long days, long seconds, int nanose

/**
* Returns a value from ISO duration.
*
* @param duration the ISO duration value
* @return the value
*/
Expand All @@ -665,9 +708,10 @@ private static Value value(IsoDuration duration) {

/**
* Returns a value from SRID, x and y values.
*
* @param srid the SRID value
* @param x the x value
* @param y the y value
* @param x the x value
* @param y the y value
* @return the value
*/
public static Value point(int srid, double x, double y) {
Expand All @@ -676,6 +720,7 @@ public static Value point(int srid, double x, double y) {

/**
* Returns a value from point.
*
* @param point the point value
* @return the value
*/
Expand All @@ -685,10 +730,11 @@ private static Value value(Point point) {

/**
* Returns a value from SRID, x ,y and z values.
*
* @param srid the SRID value
* @param x the x value
* @param y the y value
* @param z the z value
* @param x the x value
* @param y the y value
* @param z the z value
* @return the value
*/
public static Value point(int srid, double x, double y, double z) {
Expand Down Expand Up @@ -854,7 +900,7 @@ public static Function<Value, Map<String, Object>> ofMap() {
* the provided converter.
*
* @param valueConverter converter to use for the values of the map
* @param <T> the type of values in the returned map
* @param <T> the type of values in the returned map
* @return a function that returns {@link Value#asMap(Function)} of a {@link Value}
*/
public static <T> Function<Value, Map<String, T>> ofMap(final Function<Value, T> valueConverter) {
Expand All @@ -873,8 +919,8 @@ public static Function<Value, Entity> ofEntity() {
/**
* Converts values to {@link Long entity id}.
*
* @deprecated superseded by {@link #ofEntityElementId()}.
* @return a function that returns the id an entity {@link Value}
* @deprecated superseded by {@link #ofEntityElementId()}.
*/
@Deprecated
public static Function<Value, Long> ofEntityId() {
Expand Down Expand Up @@ -1002,7 +1048,7 @@ public static Function<Value, List<Object>> ofList() {
* Converts values to {@link List} of {@code T}.
*
* @param innerMap converter for the values inside the list
* @param <T> the type of values inside the list
* @param <T> the type of values inside the list
* @return a function that returns {@link Value#asList(Function)} of a {@link Value}
*/
public static <T> Function<Value, List<T>> ofList(final Function<Value, T> innerMap) {
Expand Down
Loading