jhipster / generator-jhipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.
https://www.jhipster.tech
Apache License 2.0
21.53k stars 4.02k forks source link

JsonIgnoreProperties: try to prevent infinite recursion while there is no potential bidirectionnal relationship #18948

Closed Rapster closed 10 months ago

Rapster commented 2 years ago
Overview of the issue

@JsonIgnoreProperties is generated to prevent circular references which is good, but gets generated while there is no need to it

Motivation for or Use Case

Since there is no risk of circular references, no need to have this annotation on certain entities

Reproduce the error

Considering the jdl file:

JDL definitions
entity A {

}

entity B {

}

entity C {

}

entity D {

}

relationship ManyToOne {
    A{b} to B
    B{c} to C
}
  

A entity looks like this:

@Entity
@Table(name = "a")
public class A implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    @Column(name = "id")
    private Long id;

    @ManyToOne
    @JsonIgnoreProperties(value = { "c" }, allowSetters = true)
    private B b;

   // ...
}

C does not reference A, neither C to B and yet it's being ignored for no reason.

Suggest a Fix

Problem comes from here:

/**
       * Process json ignore references to prevent cyclic relationships.
       */
      processJsonIgnoreReferences() {
        this.relationships
          .filter(relationship => relationship.ignoreOtherSideProperty === undefined)
          .forEach(relationship => {
            relationship.ignoreOtherSideProperty =
              !relationship.embedded && !!relationship.otherEntity && relationship.otherEntity.relationships.length > 0;
          });
        this.relationshipsContainOtherSideIgnore = this.relationships.some(relationship => relationship.ignoreOtherSideProperty);
      },

The condition is not quite right, and can't say it's easy to fix... It gets really tricky if I had a D references A: because we get a circular reference a this point... Any way to ignore subproperties, so whenever we decide to serialize A it won't try to serialize D, something like:

    @ManyToOne
    @JsonIgnoreProperties(value = { "c.d.a" }, allowSetters = true)
    private B b;

I'm confused with all the ways to solve circular references in jackson: https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion

JHipster Version(s)

7.8.1

Browsers and Operating System
github-actions[bot] commented 10 months ago

This issue is stale because it has been open for too long without any activity. Due to the moving nature of jhipster generated application, bugs can become invalid. If this issue still applies please comment otherwise it will be closed in 7 days