From 4f0351cab9ab186c644861a39e9bcb31c7ea483a Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Mon, 2 Mar 2020 21:44:00 +0100 Subject: [PATCH] Solved recursive JSON problem in RoomController.getAllDevices() --- .../smarthut/config/SpringFoxConfig.java | 12 ++++++++- .../controller/ButtonDimmerController.java | 7 +++-- .../controller/DimmableLightController.java | 7 +++-- .../controller/KnobDimmerController.java | 7 +++-- .../controller/MotionSensorController.java | 7 +++-- .../controller/RegularLightController.java | 7 +++-- .../smarthut/controller/RoomController.java | 22 +++++++++------- .../smarthut/controller/SensorController.java | 7 +++-- .../controller/SmartPlugController.java | 7 +++-- .../smarthut/controller/SwitchController.java | 7 +++-- .../sanmarinoes/smarthut/models/Device.java | 14 ---------- .../sa4/sanmarinoes/smarthut/models/Room.java | 26 +------------------ .../sa4/sanmarinoes/smarthut/models/User.java | 10 ------- .../sa4/sanmarinoes/smarthut/utils/Utils.java | 14 ++++++++++ 14 files changed, 78 insertions(+), 76 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java index 743d119..ae950b2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java @@ -50,6 +50,16 @@ public class SpringFoxConfig { return List.of(new ApiKey("Bearer", "Authorization", "header")); } + /** + * Return a Java functional API predicate for regex matches + * + * @param regex the regex to match on + * @return a Java functional API predicate + */ + private Predicate regexPredicate(final String regex) { + return regex(regex)::apply; + } + /** * Configures the paths the documentation must be generated for. Add a path here only when the * spec has been totally defined. @@ -57,7 +67,7 @@ public class SpringFoxConfig { * @return A predicate that tests whether a path must be included or not */ private Predicate paths() { - return regex("/auth.*")::apply; + return regexPredicate("/auth.*").or(regexPredicate("/room.*")); } /** diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java index 7c2d0b7..c94f5cc 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java @@ -1,7 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; + import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository; +import java.util.List; import java.util.Optional; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; @@ -22,8 +25,8 @@ public class ButtonDimmerController { @Autowired private ButtonDimmerRepository buttonDimmerService; @GetMapping - public Iterable findAll() { - return buttonDimmerService.findAll(); + public List findAll() { + return toList(buttonDimmerService.findAll()); } @GetMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java index 5391e43..2f6900a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java @@ -1,7 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; + import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository; +import java.util.List; import java.util.Optional; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; @@ -23,8 +26,8 @@ public class DimmableLightController { @Autowired private DimmableLightRepository dimmableLightService; @GetMapping - public Iterable findAll() { - return dimmableLightService.findAll(); + public List findAll() { + return toList(dimmableLightService.findAll()); } @GetMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java index 3dd53d7..0a23301 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java @@ -1,7 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; + import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository; +import java.util.List; import java.util.Optional; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; @@ -23,8 +26,8 @@ public class KnobDimmerController { @Autowired private KnobDimmerRepository knobDimmerService; @GetMapping - public Iterable findAll() { - return knobDimmerService.findAll(); + public List findAll() { + return toList(knobDimmerService.findAll()); } @GetMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java index 15c964f..5b6b947 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java @@ -1,7 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; + import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository; +import java.util.List; import java.util.Optional; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; @@ -23,8 +26,8 @@ public class MotionSensorController { @Autowired private MotionSensorRepository motionSensorService; @GetMapping - public Iterable findAll() { - return motionSensorService.findAll(); + public List findAll() { + return toList(motionSensorService.findAll()); } @GetMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java index 2ec1453..6b50312 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java @@ -1,7 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; + import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository; +import java.util.List; import java.util.Optional; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; @@ -23,8 +26,8 @@ public class RegularLightController { @Autowired private RegularLightRepository regularLightService; @GetMapping - public Iterable findAll() { - return regularLightService.findAll(); + public List findAll() { + return toList(regularLightService.findAll()); } @GetMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java index c1a4d87..8c51b23 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java @@ -1,5 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; + import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.security.Principal; @@ -19,15 +21,15 @@ public class RoomController { @Autowired private UserRepository userRepository; - @Autowired private DeviceRepository deviceRepository; + @Autowired private DeviceRepository deviceRepository; @GetMapping - public Iterable findAll() { - return roomRepository.findAll(); + public List findAll() { + return toList(roomRepository.findAll()); } @GetMapping("/{id}") - public Optional findById(@PathVariable("id") long id) { + public @ResponseBody Optional findById(@PathVariable("id") long id) { return roomRepository.findById(id); } @@ -40,7 +42,6 @@ public class RoomController { final String icon = r.getIcon(); newRoom.setUserId(userId); - newRoom.setUser(null); if (img != null) { newRoom.setImage(img.getBytes()); } else if (setWhenNull) { @@ -56,12 +57,14 @@ public class RoomController { } @PostMapping - public Room create(@Valid @RequestBody RoomSaveRequest r, final Principal principal) { + public @ResponseBody Room create( + @Valid @RequestBody RoomSaveRequest r, final Principal principal) { return this.save(r, principal, true); } @PutMapping - public Room update(@Valid @RequestBody RoomSaveRequest r, final Principal principal) { + public @ResponseBody Room update( + @Valid @RequestBody RoomSaveRequest r, final Principal principal) { return this.save(r, principal, false); } @@ -74,9 +77,8 @@ public class RoomController { * Returns a List of all Devices that are present in a given room (identified by its * id). */ - @GetMapping(path = "/room/{roomid}/devices") - @ResponseBody - public List getDevices(@PathVariable("roomid") long roomid) { + @GetMapping(path = "/{roomId}/devices") + public @ResponseBody List getDevices(@PathVariable("roomId") long roomid) { return deviceRepository.findByRoomId(roomid); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java index 957328e..8a9ce92 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java @@ -1,7 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; + import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.util.*; +import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.*; @@ -15,8 +18,8 @@ public class SensorController { @Autowired private SensorRepository sensorRepository; @GetMapping - public Iterable findAll() { - return sensorRepository.findAll(); + public List findAll() { + return toList(sensorRepository.findAll()); } @GetMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java index a0aee71..64dc6b5 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java @@ -1,7 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; + import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.util.*; +import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.*; @@ -15,8 +18,8 @@ public class SmartPlugController { @Autowired private SmartPlugRepository smartPlugRepository; @GetMapping - public Iterable findAll() { - return smartPlugRepository.findAll(); + public List findAll() { + return toList(smartPlugRepository.findAll()); } @GetMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java index 7eb83a0..d13fdc2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java @@ -1,7 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; + import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.util.*; +import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.*; @@ -15,8 +18,8 @@ public class SwitchController { @Autowired private SwitchRepository switchRepository; @GetMapping - public Iterable findAll() { - return switchRepository.findAll(); + public List findAll() { + return toList(switchRepository.findAll()); } @GetMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java index 871b67c..7aa65e8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java @@ -26,12 +26,6 @@ public abstract class Device { @ApiModelProperty(hidden = true) private long id; - /** The room this device belongs in */ - @ManyToOne - @JoinColumn(name = "room_id", nullable = false, updatable = false, insertable = false) - @ApiModelProperty(hidden = true) - private Room room; - /** * The room this device belongs in, as a foreign key id. To use when updating and inserting from * a REST call. @@ -69,14 +63,6 @@ public abstract class Device { this.id = id; } - public Room getRoom() { - return room; - } - - public void setRoom(Room room) { - this.room = room; - } - public String getName() { return name; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java index 4a82ab2..e15805b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java @@ -1,7 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import io.swagger.annotations.ApiModelProperty; -import java.util.Set; import javax.persistence.*; import javax.validation.constraints.NotNull; @@ -42,17 +41,6 @@ public class Room { @Column(nullable = false) private String name; - /** Collection of devices present in this room */ - @ApiModelProperty(hidden = true) - @OneToMany(mappedBy = "room") - private Set devices; - - /** User that owns the house this room is in */ - @ManyToOne - @JoinColumn(name = "user_id", nullable = false, updatable = false, insertable = false) - @ApiModelProperty(hidden = true) - private User user; - public Long getId() { return id; } @@ -69,14 +57,6 @@ public class Room { this.userId = userId; } - public User getUser() { - return user; - } - - public void setUser(User user) { - this.user = user; - } - public String getName() { return name; } @@ -85,10 +65,6 @@ public class Room { this.name = name; } - public Set getDevices() { - return devices; - } - public byte[] getIcon() { return icon; } @@ -107,6 +83,6 @@ public class Room { @Override public String toString() { - return "Room{" + "id=" + id + ", user=" + user + ", name='" + name + "\'}"; + return "Room{" + "id=" + id + ", name='" + name + "\'}"; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java index 4b9cb51..f7feaea 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java @@ -1,7 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import io.swagger.annotations.ApiModelProperty; -import java.util.Set; import javax.persistence.*; import javax.validation.constraints.*; @@ -49,11 +48,6 @@ public class User { @Pattern(regexp = ".+@.+\\..+", message = "Please provide a valid email address") private String email; - /** All rooms in the user's house */ - @OneToMany(mappedBy = "user") - @ApiModelProperty(hidden = true) - private Set rooms; - public Long getId() { return id; } @@ -94,10 +88,6 @@ public class User { this.password = password; } - public Set getRooms() { - return rooms; - } - @Override public String toString() { return "User{" diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java new file mode 100644 index 0000000..d9fcf12 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java @@ -0,0 +1,14 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.utils; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +/** A class with a bunch of useful static methods */ +public class Utils { + private Utils() {} + + public static List toList(Iterable iterable) { + return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList()); + } +}