From c95953f04d3eaaaa411c7f98f3b73eaf6a5c26ed Mon Sep 17 00:00:00 2001 From: lvca Date: Tue, 18 Jun 2024 10:02:26 -0400 Subject: [PATCH] fix: getOrCreateProperty method now takes into account also `ofType` --- .../arcadedb/database/DocumentValidator.java | 103 ++++++++++-------- .../arcadedb/schema/LocalDocumentType.java | 2 +- 2 files changed, 56 insertions(+), 49 deletions(-) diff --git a/engine/src/main/java/com/arcadedb/database/DocumentValidator.java b/engine/src/main/java/com/arcadedb/database/DocumentValidator.java index 60e54f3478..02fa30db12 100644 --- a/engine/src/main/java/com/arcadedb/database/DocumentValidator.java +++ b/engine/src/main/java/com/arcadedb/database/DocumentValidator.java @@ -40,19 +40,19 @@ public static void validate(final MutableDocument document) throws ValidationExc public static void validateField(final MutableDocument document, final Property p) throws ValidationException { if (p.isMandatory() && !document.has(p.getName())) - throwValidationException(p, "is mandatory, but not found on record: " + document); + throwValidationException(document.getType(), p, "is mandatory, but not found on record: " + document); final Object fieldValue = document.get(p.getName()); if (fieldValue == null) { if (p.isNotNull() && document.has(p.getName())) // NULLITY - throwValidationException(p, "cannot be null, record: " + document); + throwValidationException(document.getType(), p, "cannot be null, record: " + document); } else { if (p.getRegexp() != null) // REGEXP if (!(fieldValue.toString()).matches(p.getRegexp())) - throwValidationException(p, + throwValidationException(document.getType(), p, "does not match the regular expression '" + p.getRegexp() + "'. Field value is: " + fieldValue + ", record: " + document); @@ -65,13 +65,14 @@ public static void validateField(final MutableDocument document, final Property switch (propertyType) { case LINK: { if (fieldValue instanceof EmbeddedDocument) - throwValidationException(p, "has been declared as LINK but an EMBEDDED document is used. Value: " + fieldValue); + throwValidationException(document.getType(), p, + "has been declared as LINK but an EMBEDDED document is used. Value: " + fieldValue); if (ofType != null) { final RID rid = ((Identifiable) fieldValue).getIdentity(); final DocumentType embSchemaType = document.getDatabase().getSchema().getTypeByBucketId(rid.getBucketId()); if (!embSchemaType.instanceOf(ofType)) - throwValidationException(p, + throwValidationException(document.getType(), p, "has been declared as LINK of '" + ofType + "' but a link to type '" + embSchemaType + "' is used. Value: " + fieldValue); } @@ -80,12 +81,13 @@ public static void validateField(final MutableDocument document, final Property case EMBEDDED: { if (!(fieldValue instanceof EmbeddedDocument)) - throwValidationException(p, "has been declared as EMBEDDED but an incompatible type is used. Value: " + fieldValue); + throwValidationException(document.getType(), p, + "has been declared as EMBEDDED but an incompatible type is used. Value: " + fieldValue); if (ofType != null) { final DocumentType embSchemaType = ((EmbeddedDocument) fieldValue).getType(); if (!embSchemaType.instanceOf(ofType)) - throwValidationException(p, + throwValidationException(document.getType(), p, "has been declared as EMBEDDED of '" + ofType + "' but a document of type '" + embSchemaType + "' is used. Value: " + fieldValue); } @@ -96,7 +98,8 @@ public static void validateField(final MutableDocument document, final Property case LIST: { if (!(fieldValue instanceof List)) - throwValidationException(p, "has been declared as LIST but an incompatible type is used. Value: " + fieldValue); + throwValidationException(document.getType(), p, + "has been declared as LIST but an incompatible type is used. Value: " + fieldValue); final Type embType = ofType != null ? Type.getTypeByName(ofType) : null; @@ -104,19 +107,21 @@ public static void validateField(final MutableDocument document, final Property if (ofType != null) { if (embType != null) { if (Type.getTypeByValue(item) != embType) - throwValidationException(p, + throwValidationException(document.getType(), p, "has been declared as LIST of '" + ofType + "' but a value of type '" + Type.getTypeByValue(item) + "' is used. Value: " + fieldValue); } else if (item instanceof EmbeddedDocument) { if (!((EmbeddedDocument) item).getType().instanceOf(ofType)) - throwValidationException(p, "has been declared as LIST of '" + ofType + "' but an embedded document of type '" - + ((EmbeddedDocument) item).getType().getName() + "' is used. Value: " + fieldValue); + throwValidationException(document.getType(), p, + "has been declared as LIST of '" + ofType + "' but an embedded document of type '" + + ((EmbeddedDocument) item).getType().getName() + "' is used. Value: " + fieldValue); } else if (item instanceof Identifiable) { final RID rid = ((Identifiable) item).getIdentity(); final DocumentType embSchemaType = document.getDatabase().getSchema().getTypeByBucketId(rid.getBucketId()); if (!embSchemaType.instanceOf(ofType)) - throwValidationException(p, - "has been declared as LIST of '" + ofType + "' but a link to type '" + embSchemaType.getName() + "' is used. Value: " + throwValidationException(document.getType(), p, + "has been declared as LIST of '" + ofType + "' but a link to type '" + embSchemaType.getName() + + "' is used. Value: " + fieldValue); } } @@ -129,7 +134,8 @@ public static void validateField(final MutableDocument document, final Property case MAP: { if (!(fieldValue instanceof Map)) - throwValidationException(p, "has been declared as MAP but an incompatible type is used. Value: " + fieldValue); + throwValidationException(document.getType(), p, + "has been declared as MAP but an incompatible type is used. Value: " + fieldValue); final Type embType = ofType != null ? Type.getTypeByName(ofType) : null; @@ -137,19 +143,19 @@ public static void validateField(final MutableDocument document, final Property if (ofType != null) { if (embType != null) { if (Type.getTypeByValue(item) != embType) - throwValidationException(p, + throwValidationException(document.getType(), p, "has been declared as a MAP of but a value of type '" + Type.getTypeByValue(item) + "' is used. Value: " + fieldValue); } else if (item instanceof EmbeddedDocument) { if (!((EmbeddedDocument) item).getType().instanceOf(ofType)) - throwValidationException(p, + throwValidationException(document.getType(), p, "has been declared as a MAP of but an embedded document of type '" + ((EmbeddedDocument) item).getType().getName() + "' is used. Value: " + fieldValue); } else if (item instanceof Identifiable) { final RID rid = ((Identifiable) item).getIdentity(); final DocumentType embSchemaType = document.getDatabase().getSchema().getTypeByBucketId(rid.getBucketId()); if (!embSchemaType.instanceOf(ofType)) - throwValidationException(p, + throwValidationException(document.getType(), p, "has been declared as a MAP of but a link to type '" + embSchemaType.getName() + "' is used. Value: " + fieldValue); } @@ -171,56 +177,56 @@ public static void validateField(final MutableDocument document, final Property case LONG: { final long minAsLong = Long.parseLong(min); if (((Number) fieldValue).longValue() < minAsLong) - throwValidationException(p, "value " + fieldValue + " is less than " + min); + throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min); break; } case INTEGER: { final int minAsInteger = Integer.parseInt(min); if (((Number) fieldValue).intValue() < minAsInteger) - throwValidationException(p, "value " + fieldValue + " is less than " + min); + throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min); break; } case SHORT: { final int minAsInteger = Integer.parseInt(min); if (((Number) fieldValue).shortValue() < minAsInteger) - throwValidationException(p, "value " + fieldValue + " is less than " + min); + throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min); break; } case BYTE: { final int minAsInteger = Integer.parseInt(min); if (((Number) fieldValue).byteValue() < minAsInteger) - throwValidationException(p, "value " + fieldValue + " is less than " + min); + throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min); break; } case FLOAT: { final float minAsFloat = Float.parseFloat(min); if (((Number) fieldValue).floatValue() < minAsFloat) - throwValidationException(p, "value " + fieldValue + " is less than " + min); + throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min); break; } case DOUBLE: { final double minAsDouble = Double.parseDouble(min); if (((Number) fieldValue).floatValue() < minAsDouble) - throwValidationException(p, "value " + fieldValue + " is less than " + min); + throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min); break; } case DECIMAL: { final BigDecimal minAsDecimal = new BigDecimal(min); if (((BigDecimal) fieldValue).compareTo(minAsDecimal) < 0) - throwValidationException(p, "value " + fieldValue + " is less than " + min); + throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min); break; } case STRING: { final int minAsInteger = Integer.parseInt(min); if (fieldValue.toString().length() < minAsInteger) - throwValidationException(p, "contains fewer characters than " + min + " requested"); + throwValidationException(document.getType(), p, "contains fewer characters than " + min + " requested"); break; } @@ -231,7 +237,7 @@ public static void validateField(final MutableDocument document, final Property final Date fieldValueAsDate = (Date) Type.convert(database, fieldValue, Date.class); if (fieldValueAsDate.compareTo(minAsDate) < 0) - throwValidationException(p, + throwValidationException(document.getType(), p, "contains the date " + fieldValue + " which precedes the first acceptable date (" + min + ")"); break; } @@ -240,28 +246,28 @@ public static void validateField(final MutableDocument document, final Property final int minAsInteger = Integer.parseInt(min); if (fieldValue instanceof Binary) { if (((Binary) fieldValue).size() < minAsInteger) - throwValidationException(p, "contains fewer bytes than " + min + " requested"); + throwValidationException(document.getType(), p, "contains fewer bytes than " + min + " requested"); } else if (((byte[]) fieldValue).length < minAsInteger) - throwValidationException(p, "contains fewer bytes than " + min + " requested"); + throwValidationException(document.getType(), p, "contains fewer bytes than " + min + " requested"); break; } case LIST: { final int minAsInteger = Integer.parseInt(min); if (((Collection) fieldValue).size() < minAsInteger) - throwValidationException(p, "contains fewer items than " + min + " requested"); + throwValidationException(document.getType(), p, "contains fewer items than " + min + " requested"); break; } case MAP: { final int minAsInteger = Integer.parseInt(min); if (((Map) fieldValue).size() < minAsInteger) - throwValidationException(p, "contains fewer items than " + min + " requested"); + throwValidationException(document.getType(), p, "contains fewer items than " + min + " requested"); break; } default: - throwValidationException(p, "value " + fieldValue + " is less than " + min); + throwValidationException(document.getType(), p, "value " + fieldValue + " is less than " + min); } } @@ -273,56 +279,56 @@ public static void validateField(final MutableDocument document, final Property case LONG: { final long maxAsLong = Long.parseLong(max); if (((Number) fieldValue).longValue() > maxAsLong) - throwValidationException(p, "value " + fieldValue + " is greater than " + max); + throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max); break; } case INTEGER: { final int maxAsInteger = Integer.parseInt(max); if (((Number) fieldValue).intValue() > maxAsInteger) - throwValidationException(p, "value " + fieldValue + " is greater than " + max); + throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max); break; } case SHORT: { final int maxAsInteger = Integer.parseInt(max); if (((Number) fieldValue).shortValue() > maxAsInteger) - throwValidationException(p, "value " + fieldValue + " is greater than " + max); + throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max); break; } case BYTE: { final int maxAsInteger = Integer.parseInt(max); if (((Number) fieldValue).byteValue() > maxAsInteger) - throwValidationException(p, "value " + fieldValue + " is greater than " + max); + throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max); break; } case FLOAT: { final float maxAsFloat = Float.parseFloat(max); if (((Number) fieldValue).floatValue() > maxAsFloat) - throwValidationException(p, "value " + fieldValue + " is greater than " + max); + throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max); break; } case DOUBLE: { final double maxAsDouble = Double.parseDouble(max); if (((Number) fieldValue).floatValue() > maxAsDouble) - throwValidationException(p, "value " + fieldValue + " is greater than " + max); + throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max); break; } case DECIMAL: { final BigDecimal maxAsDecimal = new BigDecimal(max); if (((BigDecimal) fieldValue).compareTo(maxAsDecimal) > 0) - throwValidationException(p, "value " + fieldValue + " is greater than " + max); + throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max); break; } case STRING: { final int maxAsInteger = Integer.parseInt(max); if (fieldValue.toString().length() > maxAsInteger) - throwValidationException(p, "contains more characters than " + max + " requested"); + throwValidationException(document.getType(), p, "contains more characters than " + max + " requested"); break; } @@ -333,7 +339,7 @@ public static void validateField(final MutableDocument document, final Property final Date fieldValueAsDate = (Date) Type.convert(database, fieldValue, Date.class); if (fieldValueAsDate.compareTo(maxAsDate) > 0) - throwValidationException(p, + throwValidationException(document.getType(), p, "contains the date " + fieldValue + " which is after the last acceptable date (" + max + ")"); break; } @@ -342,28 +348,28 @@ public static void validateField(final MutableDocument document, final Property final int maxAsInteger = Integer.parseInt(max); if (fieldValue instanceof Binary) { if (((Binary) fieldValue).size() > maxAsInteger) - throwValidationException(p, "contains more bytes than " + max + " requested"); + throwValidationException(document.getType(), p, "contains more bytes than " + max + " requested"); } else if (((byte[]) fieldValue).length > maxAsInteger) - throwValidationException(p, "contains more bytes than " + max + " requested"); + throwValidationException(document.getType(), p, "contains more bytes than " + max + " requested"); break; } case LIST: { final int maxAsInteger = Integer.parseInt(max); if (((Collection) fieldValue).size() > maxAsInteger) - throwValidationException(p, "contains more items than " + max + " requested"); + throwValidationException(document.getType(), p, "contains more items than " + max + " requested"); break; } case MAP: { final int maxAsInteger = Integer.parseInt(max); if (((Map) fieldValue).size() > maxAsInteger) - throwValidationException(p, "contains more items than " + max + " requested"); + throwValidationException(document.getType(), p, "contains more items than " + max + " requested"); break; } default: - throwValidationException(p, "value " + fieldValue + " is greater than " + max); + throwValidationException(document.getType(), p, "value " + fieldValue + " is greater than " + max); } } } @@ -373,12 +379,13 @@ public static void validateField(final MutableDocument document, final Property final Document originalDocument = ((LocalDatabase) document.getDatabase()).getOriginalDocument(document); final Object originalFieldValue = originalDocument.get(p.getName()); if (!Objects.equals(fieldValue, originalFieldValue)) - throwValidationException(p, "is immutable and cannot be altered. Field value is: " + fieldValue); + throwValidationException(document.getType(), p, "is immutable and cannot be altered. Field value is: " + fieldValue); } } } - private static void throwValidationException(final Property p, final String message) throws ValidationException { - throw new ValidationException("The property '" + p.getName() + "' " + message); + private static void throwValidationException(final DocumentType type, final Property p, final String message) + throws ValidationException { + throw new ValidationException("The property '" + type.getName() + "." + p.getName() + "' " + message); } } diff --git a/engine/src/main/java/com/arcadedb/schema/LocalDocumentType.java b/engine/src/main/java/com/arcadedb/schema/LocalDocumentType.java index 3aab7d14cd..8454635774 100644 --- a/engine/src/main/java/com/arcadedb/schema/LocalDocumentType.java +++ b/engine/src/main/java/com/arcadedb/schema/LocalDocumentType.java @@ -383,7 +383,7 @@ public Property getOrCreateProperty(final String propertyName, final Type proper public Property getOrCreateProperty(final String propertyName, final Type propertyType, final String ofType) { final Property p = getPolymorphicPropertyIfExists(propertyName); if (p != null) { - if (p.getType().equals(propertyType)) + if (p.getType().equals(propertyType) && Objects.equals(p.getOfType(), ofType)) return p; // DIFFERENT TYPE: DROP THE PROPERTY AND CREATE A NEW ONE