Skip to content

Add a modification reference as a new modification type#807

Open
SlimaneAmar wants to merge 6 commits into
mainfrom
shared-modification
Open

Add a modification reference as a new modification type#807
SlimaneAmar wants to merge 6 commits into
mainfrom
shared-modification

Conversation

@SlimaneAmar
Copy link
Copy Markdown
Contributor

PR Summary

@coderabbitai

This comment was marked as low quality.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@src/main/java/org/gridsuite/modification/server/entities/ModificationReferenceEntity.java`:
- Around line 59-64: Validate and guard against nulls for referenceType,
referenceId and referenceInfos before dereferencing in the
conversion/constructor that sets this.referenceType and this.referenceId and
calls modificationReferenceInfos.getReferenceInfos(); specifically check
modificationReferenceInfos, modificationReferenceInfos.getReferenceType(),
modificationReferenceInfos.getReferenceId(), and
modificationReferenceInfos.getReferenceInfos() (and its
getMessageType()/getMapMessageValues()) and either throw a clear
IllegalArgumentException or handle missing values gracefully; only call
setMessageType(...) and new ObjectMapper().writeValueAsString(...) when
referenceInfos is non-null, and include a descriptive error message mentioning
ModificationReferenceEntity/ modificationReferenceInfos to aid debugging.

In
`@src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java`:
- Around line 489-499: The loadModificationReference method calls
modificationRepository.getReferenceById(...) and passes its result directly to
toModificationsInfosOptimized, which can NPE if the repository returns null;
update loadModificationReference to guard the reference result (from
modificationRepository.getReferenceById) before conversion: retrieve the
reference into a local variable, check for null (or empty) and either pass an
empty/nullable-safe value to toModificationsInfosOptimized or set referenceInfos
to an appropriate default, keeping the existing fields (uuid, activated,
description, date, stashed, referenceId, referenceType) intact; references to
modify: loadModificationReference, modificationRepository.getReferenceById, and
toModificationsInfosOptimized.

In `@src/main/resources/db/changelog/changesets/changelog_20260424T113143Z.xml`:
- Around line 4-13: Make the schema enforce that references are present by
adding NOT NULL constraints on modification_reference.reference_id and
modification_reference.reference_type and a check constraint that both are set
together; update the Liquibase changeset that defines table
modification_reference (and/or add a new changeSet) to run addNotNullConstraint
for columns reference_id and reference_type and add a logical checkConstraint
(e.g., "reference_id IS NOT NULL AND reference_type IS NOT NULL") on
modification_reference; optionally, if there is a single concrete target type,
add the corresponding addForeignKeyConstraint from
modification_reference.reference_id to that target table, otherwise leave
polymorphic FK logic to application code.

In
`@src/test/java/org/gridsuite/modification/server/modifications/ModificationReferenceTest.java`:
- Around line 49-51: buildModificationUpdate() currently just returns
buildModification(), so the update tests reuse the creation payload; change
buildModificationUpdate() to produce a distinct ModificationInfos instance
(e.g., clone the object returned by buildModification() then mutate one or more
fields such as name, version, description, or attributes) so the update path
exercises a changed payload, and update any assertions in the test to verify the
modified fields differ (use the ModificationInfos constructor/setters or a
builder to make the change).
- Around line 64-70: The tests testCreationModificationMessage and
testUpdateModificationMessage currently misuse assertNotNull by passing the
constant ModificationType.COMPOSITE_MODIFICATION.name() as the first parameter
and modificationInfos.getMessageType() as the message, so they always pass;
replace those assertNotNull calls with
assertEquals(ModificationType.COMPOSITE_MODIFICATION.name(),
modificationInfos.getMessageType()) in both methods to assert the actual message
type equals the expected value.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 459859bb-1f68-43e6-8758-552f6600df57

📥 Commits

Reviewing files that changed from the base of the PR and between 6af09c1 and f73bc65.

📒 Files selected for processing (8)
  • src/main/java/org/gridsuite/modification/server/entities/EntityRegistry.java
  • src/main/java/org/gridsuite/modification/server/entities/ModificationReferenceEntity.java
  • src/main/java/org/gridsuite/modification/server/modifications/ModificationTypeWithPreloadingStrategy.java
  • src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java
  • src/main/resources/db/changelog/changesets/changelog_20260424T113143Z.xml
  • src/main/resources/db/changelog/db.changelog-master.yaml
  • src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java
  • src/test/java/org/gridsuite/modification/server/modifications/ModificationReferenceTest.java

@Mathieu-Deharbe Mathieu-Deharbe self-assigned this Apr 29, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@src/main/java/org/gridsuite/modification/server/entities/ModificationReferenceEntity.java`:
- Around line 60-63: The update method in ModificationReferenceEntity currently
casts modificationInfos to ModificationReferenceInfos without checking its
runtime type, which can throw ClassCastException; modify
ModificationReferenceEntity.update to first verify modificationInfos is an
instance of ModificationReferenceInfos (using instanceof or equivalent), then
call assignAttributes((ModificationReferenceInfos) modificationInfos) only when
the check passes, and if not, throw a controlled domain error (e.g.,
IllegalArgumentException or your project's DomainException) with a clear message
indicating the expected DTO type; keep the call to
super.update(modificationInfos) as appropriate and reference the update and
assignAttributes methods and the ModificationInfos/ModificationReferenceInfos
types when making the change.

In
`@src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java`:
- Around line 490-506: loadModificationReference(...) calls
toModificationsInfosOptimized(referencedEntity) with no cycle detection which
can cause stack overflow on self/cyclic references; modify
toModificationsInfosOptimized to accept a recursion guard (e.g., a Set of
visited modification IDs or a max-depth param) and check/mark
referencedEntity.getId() before recursing, returning a safe placeholder or
skipping recursive expansion when a cycle is detected; update
loadModificationReference (and the other call sites at the other reference
handling locations) to create/pass the visited set (add the current modification
id before the call) so cycles are detected and prevented.
- Line 30: Remove the unused import of DirectoryService from the top of
NetworkModificationRepository: delete the line importing
org.gridsuite.modification.server.service.DirectoryService (or alternatively
reference DirectoryService where needed); ensure no other code in class
NetworkModificationRepository references DirectoryService so Checkstyle
UnusedImports no longer fails.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: dd6ef7cf-1a81-42c7-bb29-11488c9bb807

📥 Commits

Reviewing files that changed from the base of the PR and between f73bc65 and 7676ad7.

📒 Files selected for processing (4)
  • src/main/java/org/gridsuite/modification/server/entities/ModificationReferenceEntity.java
  • src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java
  • src/main/resources/db/changelog/changesets/changelog_20260424T113143Z.xml
  • src/test/java/org/gridsuite/modification/server/modifications/ModificationReferenceTest.java
✅ Files skipped from review due to trivial changes (1)
  • src/main/resources/db/changelog/changesets/changelog_20260424T113143Z.xml

Copy link
Copy Markdown
Contributor

@Mathieu-Deharbe Mathieu-Deharbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read but not executed yet. I don't want to mess with my database until this is completely finished.

Comment on lines +27 to +31
@Column(name = "referenceId")
UUID referenceId;

@Column(name = "referenceType")
String referenceType;
Copy link
Copy Markdown
Contributor

@Mathieu-Deharbe Mathieu-Deharbe Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those will be turned into reference_id and reference_type in the database won't they ? If yes, why not directly :

Suggested change
@Column(name = "referenceId")
UUID referenceId;
@Column(name = "referenceType")
String referenceType;
@Column(name = "reference_id")
UUID referenceId;
@Column(name = "reference_type")
String referenceType;

The reference would be more precise, easier to track.

Comment on lines +496 to +497
.messageType(modificationEntity.getMessageType())
.messageValues(modificationEntity.getMessageValues())
Copy link
Copy Markdown
Contributor

@Mathieu-Deharbe Mathieu-Deharbe Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure of how the update will happen when the referenced modification is changed so I may be wrong. But just in case shouldn't we use the message type and message values of the referencedEntity here ?

Isn't it how we want this modification reference to be displayed in the front ?

.referenceType(ModificationReferenceInfos.Type.SAMPLE)
.referenceId(loadModificationInfo.getUuid())
.referenceInfos(loadModificationInfo)
.stashed(false)
Copy link
Copy Markdown
Contributor

@Mathieu-Deharbe Mathieu-Deharbe Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? :

Suggested change
.stashed(false)

(false by default)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants