Skip to content

Commit

Permalink
Fixed Recursive error on Initialization
Browse files Browse the repository at this point in the history
Some fixes with lazy load on enemies and actions

Reformat code and optimize imports (sorry Barčo)
  • Loading branch information
rcMarty committed Feb 11, 2024
1 parent 9f716b4 commit b5645dd
Show file tree
Hide file tree
Showing 26 changed files with 91 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ public class Sanitized {
/**
* Formats a message using the given format string and arguments.
* <p>
* This method is a wrapper around {@link ParameterizedMessage#getFormattedMessage()}.
* It is used to format messages in a safe way, preventing log injection attacks.
* This method is a wrapper around {@link ParameterizedMessage#getFormattedMessage()}.
* It is used to format messages in a safe way, preventing log injection attacks.
* </p>
*
* @param format the format string, using '{}' as a placeholder
* @param args the arguments to be substituted into the format string
*
* @return the formatted message
*/
public static String format(String format, Object... args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,30 @@
@ConfigurationProperties(prefix = "validation")
public class ValidationConfig {

private final HexGrid hexGrid;
private final Description description;
private final Title title;
private final Tag tag;

@Data
public static class HexGrid {
private int minHexes;
private int maxHexes;
private int maxWidth;
}
private final HexGrid hexGrid;

@Data
public static class Description {
private int maxLen;
private String allowedChars;
}
private final Description description;

@Data
public static class Title {
private int minLen;
private int maxLen;
private String allowedChars;
}
private final Title title;

@Data
public static class Tag {
Expand All @@ -37,8 +39,6 @@ public static class Tag {
private String allowedChars;
private String prefix;
}
private final Tag tag;



}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cz.trailsthroughshadows.api.rest.jsonfilter;
package cz.trailsthroughshadows.api.rest.json;

import jakarta.persistence.Persistence;
import org.hibernate.proxy.HibernateProxy;

import java.util.Collection;

/**
* Filter non fetched lazy fields from serialization.
*/
public class LazyFieldsFilter {
@Override
public boolean equals(Object obj) {
Expand All @@ -18,9 +20,6 @@ public boolean equals(Object obj) {
// if (obj instanceof HibernateProxy)
// return false;

if (Persistence.getPersistenceUtil().isLoaded(obj))
return false;

return true;
return !Persistence.getPersistenceUtil().isLoaded(obj);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cz.trailsthroughshadows.api.rest.jsonfilter;
package cz.trailsthroughshadows.api.rest.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
Expand All @@ -8,7 +8,10 @@
import java.io.IOException;
import java.util.Collection;

public class IncludeNullOrEmptySerializer extends com.fasterxml.jackson.databind.JsonSerializer<Object> {
/**
* Serializer which serialize non fetched lazy fields as ID or array of IDs.
*/
public class LazyFieldsSerializer extends com.fasterxml.jackson.databind.JsonSerializer<Object> {
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cz.trailsthroughshadows.api.table.action;

import cz.trailsthroughshadows.api.rest.exception.RestException;
import cz.trailsthroughshadows.api.table.action.model.ActionDTO;
import cz.trailsthroughshadows.api.table.action.features.movement.MovementRepo;
import cz.trailsthroughshadows.api.table.action.model.ActionDTO;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.rest.jsonfilter.LazyFieldsFilter;
import cz.trailsthroughshadows.api.rest.json.LazyFieldsFilter;
import cz.trailsthroughshadows.api.table.effect.model.EffectDTO;
import cz.trailsthroughshadows.api.table.effect.relation.foraction.AttackEffect;
import jakarta.persistence.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cz.trailsthroughshadows.algorithm.validation.ValidationConfig;
import cz.trailsthroughshadows.algorithm.validation.text.Tag;
import cz.trailsthroughshadows.algorithm.validation.text.Title;
import cz.trailsthroughshadows.api.rest.jsonfilter.LazyFieldsFilter;
import cz.trailsthroughshadows.api.rest.json.LazyFieldsFilter;
import cz.trailsthroughshadows.api.rest.model.error.type.ValidationError;
import cz.trailsthroughshadows.api.table.action.model.ActionDTO;
import cz.trailsthroughshadows.api.table.effect.model.EffectDTO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.rest.jsonfilter.LazyFieldsFilter;
import cz.trailsthroughshadows.api.rest.json.LazyFieldsFilter;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import cz.trailsthroughshadows.algorithm.validation.ValidationConfig;
import cz.trailsthroughshadows.algorithm.validation.text.Description;
import cz.trailsthroughshadows.algorithm.validation.text.Title;
import cz.trailsthroughshadows.api.rest.jsonfilter.IncludeNullOrEmptySerializer;
import cz.trailsthroughshadows.api.rest.json.LazyFieldsSerializer;
import cz.trailsthroughshadows.api.rest.model.error.type.ValidationError;
import cz.trailsthroughshadows.api.table.action.features.attack.Attack;
import cz.trailsthroughshadows.api.table.action.features.movement.Movement;
Expand All @@ -27,7 +27,6 @@
@Entity
@NoArgsConstructor
@Table(name = "Action")
//@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class ActionDTO extends Validable {

Expand All @@ -51,24 +50,27 @@ public class ActionDTO extends Validable {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "movement")
@JsonSerialize(using = IncludeNullOrEmptySerializer.class)
@JsonSerialize(using = LazyFieldsSerializer.class)
private Movement movement;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "skill")
@JsonSerialize(using = LazyFieldsSerializer.class)
private Skill skill;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "attack")
@JsonSerialize(using = LazyFieldsSerializer.class)
private Attack attack;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "restoreCards")
@JsonSerialize(using = LazyFieldsSerializer.class)
private RestoreCards restoreCards;

@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "idAction")
@JsonSerialize(using = IncludeNullOrEmptySerializer.class)
@JsonSerialize(using = LazyFieldsSerializer.class)
private List<SummonAction> summonActions;

//region Validation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cz.trailsthroughshadows.api.table.background.race;

import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.rest.jsonfilter.LazyFieldsFilter;
import cz.trailsthroughshadows.api.rest.json.LazyFieldsFilter;
import cz.trailsthroughshadows.api.table.action.model.ActionDTO;
import cz.trailsthroughshadows.api.table.effect.relation.forcharacter.RaceEffect;
import jakarta.persistence.*;
Expand All @@ -24,22 +24,19 @@ public class Race {

@Column(nullable = false, length = 128)
public String title;
@Column(length = 32)
private String tag;
@Column(nullable = true)
private String description;

@Column(nullable = false)
public int baseInitiative;

@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "idRace")
public Collection<RaceEffect> effects;

@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "idRace")
@ToString.Exclude
public Collection<RaceAction> actions;
@Column(length = 32)
private String tag;
@Column
private String description;

@ToString.Include(name = "actions")
public Collection<ActionDTO> getActions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class CampaignAchievements {
private Achievement achievement;

@Embeddable
@Data
public static class CampaignAchievementsId implements Serializable {
@Column(nullable = false)
private Integer idCampaign;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.trailsthroughshadows.api.table.effect.relation.forothers;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.rest.json.LazyFieldsFilter;
import cz.trailsthroughshadows.api.table.effect.model.EffectDTO;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand All @@ -12,15 +13,14 @@
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "EnemyEffect")
@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
public class EnemyEffect {
@Id
@Column(nullable = false)
@JsonIgnore
private int idEnemy;

@Id
@Column(nullable = false)
@JsonIgnore
private int idEffect;

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.trailsthroughshadows.api.table.enemy.model.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.rest.json.LazyFieldsFilter;
import cz.trailsthroughshadows.api.table.action.model.ActionDTO;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand All @@ -12,15 +13,14 @@
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "EnemyAction")
@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
public class EnemyActionDTO {
@Id
@Column(nullable = false)
@JsonIgnore
private int idEnemy;

@Id
@Column(nullable = false)
@JsonIgnore
private int idAction;

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package cz.trailsthroughshadows.api.table.enemy.model.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import cz.trailsthroughshadows.algorithm.validation.Validable;
import cz.trailsthroughshadows.algorithm.validation.ValidationConfig;
import cz.trailsthroughshadows.algorithm.validation.text.Tag;
import cz.trailsthroughshadows.algorithm.validation.text.Title;
import cz.trailsthroughshadows.algorithm.validation.Validable;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import cz.trailsthroughshadows.api.rest.jsonfilter.LazyFieldsFilter;
import cz.trailsthroughshadows.api.rest.json.LazyFieldsSerializer;
import cz.trailsthroughshadows.api.rest.model.error.type.ValidationError;
import cz.trailsthroughshadows.api.table.action.model.ActionDTO;
import cz.trailsthroughshadows.api.table.effect.model.EffectDTO;
Expand All @@ -24,45 +25,50 @@
@Entity
@NoArgsConstructor
@Table(name = "Enemy")
@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = LazyFieldsFilter.class)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class EnemyDTO extends Validable implements Cloneable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private Integer id;

@Column(nullable = false, length = 128)
private String title;

@Column(length = 32)
private String tag;

@Column(nullable = true)
private String description;

@Column(nullable = false)
private int baseHealth;
private Integer baseHealth;

@Column(nullable = false)
private int baseDefence;
private Integer baseDefence;

@Column(nullable = false)
private int baseInitiative;
private Integer baseInitiative;

@Column
private Integer usages;

@OneToMany(mappedBy = "idEnemy", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonSerialize(using = LazyFieldsSerializer.class)
private List<EnemyEffect> effects;

@OneToMany(mappedBy = "idEnemy", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonSerialize(using = LazyFieldsSerializer.class)
private List<EnemyActionDTO> actions;

public List<EffectDTO> getEffects() {
@JsonIgnore
public List<EffectDTO> getMappedEffects() {
if (effects == null) return new ArrayList<>();
return effects.stream().map(EnemyEffect::getEffect).toList();
}

public List<ActionDTO> getActions() {
@JsonIgnore
public List<ActionDTO> getMappedActions() {
if (actions == null) return new ArrayList<>();
return actions.stream().map(EnemyActionDTO::getAction).toList();
}
Expand Down Expand Up @@ -101,8 +107,8 @@ protected void validateInner(@Nullable ValidationConfig validationConfig) {
}

// All actions and effects must be validated.
List<ActionDTO> actions = getActions();
List<EffectDTO> effects = getEffects();
List<ActionDTO> actions = getMappedActions();
List<EffectDTO> effects = getMappedEffects();

for (ActionDTO action : actions) {
validateChild(action, validationConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Market {
// TODO getter for items in this market

@Embeddable
@Data
public static class MarketId implements Serializable {
@Column
private Integer idItem;
Expand Down
Loading

0 comments on commit b5645dd

Please sign in to comment.