Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@

import java.util.List;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.ReadOnlyProperty;

@Data
@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Vehicle {
@EqualsAndHashCode.Include
Long vehicleid;
String make;
String model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.johnlpage.memex.util.DeleteFlag;
import com.johnlpage.memex.util.ObjectConverter;
import jakarta.validation.constraints.Min;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.annotation.Version;
Expand All @@ -18,7 +20,7 @@
import java.util.HashMap;
import java.util.Map;

/* Replace @Data with this to make an Immutable model
/* Replace @Getter/@Setter with this to make an Immutable model
* which is a little more efficient but no setters just a builder
* This also impact the controller and fuzzer and JsonLoaderService -
* changes there are commented
Expand All @@ -28,12 +30,15 @@
* @Value
*/

@Data
@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Document(collection = "vehicleinspection")
public class VehicleInspection {
@Id
@EqualsAndHashCode.Include
Long testid;

@Field("lock_version")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import java.util.Date;
import java.util.Map;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/* Replace @Data with this to make an Immutable model
/* Replace @Getter/@Setter with this to make an Immutable model
* which is a little more efficient but no setters just a builder
* This also impacts the controller and PreWriteTrigger and
* JsonLoaderService changes there are commented
Expand All @@ -20,12 +22,15 @@
* @Value
*/

@Data
@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Document()
public class DocumentHistory {
@Id
@EqualsAndHashCode.Include
ObjectId historyId;
Object recordId;
Date timestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import com.johnlpage.memex.util.UpdateStrategy;
import com.mongodb.bulk.BulkWriteResult;
import jakarta.annotation.Nullable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -131,13 +129,11 @@ public JsonStreamingLoadResponse loadFromJsonStream(
}
}

@Data
@AllArgsConstructor
public static class JsonStreamingLoadResponse {
long updates;
long deletes;
long inserts;
boolean success;
String message;
}
public record JsonStreamingLoadResponse(
long updates,
long deletes,
long inserts,
boolean success,
String message
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.johnlpage.memex.VehicleInspection.model;

import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

class VehicleInspectionTest {

@Test
void toString_noStackOverflow_whenVehicleHasInspections() {
VehicleInspection inspection = new VehicleInspection();
inspection.setTestid(1L);

Vehicle vehicle = new Vehicle();
vehicle.setVehicleid(100L);
vehicle.setInspections(List.of(inspection));

inspection.setVehicle(vehicle);

assertDoesNotThrow(() -> inspection.toString());
}

@Test
void equals_noStackOverflow_whenVehicleHasInspections() {
VehicleInspection a = new VehicleInspection();
a.setTestid(1L);
VehicleInspection b = new VehicleInspection();
b.setTestid(1L);

// Separate Vehicle instances so equals() can't short-circuit on reference equality
Vehicle vehicleA = new Vehicle();
vehicleA.setVehicleid(100L);
vehicleA.setInspections(List.of(a));

Vehicle vehicleB = new Vehicle();
vehicleB.setVehicleid(100L);
vehicleB.setInspections(List.of(b));

a.setVehicle(vehicleA);
b.setVehicle(vehicleB);

assertDoesNotThrow(() -> a.equals(b));
}

@Test
void hashCode_noStackOverflow_whenVehicleHasInspections() {
VehicleInspection inspection = new VehicleInspection();
inspection.setTestid(1L);

Vehicle vehicle = new Vehicle();
vehicle.setVehicleid(100L);
vehicle.setInspections(List.of(inspection));

inspection.setVehicle(vehicle);

assertDoesNotThrow(() -> inspection.hashCode());
}
}
9 changes: 7 additions & 2 deletions memex/templates/model/Model.java.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.johnlpage.memex.util.DeleteFlag;
import com.johnlpage.memex.util.ObjectConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.springframework.data.annotation.Id;
Expand All @@ -19,13 +21,16 @@ import java.util.Map;

__idImport__

@Data
@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "__collectionName__")
public class __className__ {

@Id
@EqualsAndHashCode.Include
private __idType__ __idFieldName__;

@Field("lock_version")
Expand Down
9 changes: 7 additions & 2 deletions memex/templates/scripts/generate-models-from-json.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ processClass = { Map classes, String className, Map fields, boolean isRoot, Stri
isRoot : isRoot,
fields : [],
imports : new HashSet([
'lombok.Data',
'lombok.Getter',
'lombok.Setter',
'lombok.EqualsAndHashCode',
'org.springframework.data.mongodb.core.mapping.Field',
'com.fasterxml.jackson.annotation.JsonAnySetter',
'com.fasterxml.jackson.annotation.JsonAnyGetter',
Expand Down Expand Up @@ -556,7 +558,9 @@ def generateClassContent = { Map classInfo, String pkgName, String collectionNam
sb.append(" */\n")

// Annotations
sb.append("@Data\n")
sb.append("@Getter\n");
sb.append("@Setter\n");
sb.append("@EqualsAndHashCode(onlyExplicitlyIncluded = true)\n");
if (classInfo.isRoot) {
sb.append("@Document(collection = \"${collectionName}\")\n")
}
Expand All @@ -567,6 +571,7 @@ def generateClassContent = { Map classInfo, String pkgName, String collectionNam
if (classInfo.isRoot) {
// Add ID field first
sb.append(" @Id\n")
sb.append(" @EqualsAndHashCode.Include\n")
sb.append(" private String ${classInfo.idFieldName};\n\n")

// Add version field
Expand Down
Loading