fix: resolve duplicate properties error for multiple unidirectional OneToOne relationships to same entity#33267
Open
Gonare-22 wants to merge 1 commit intojhipster:mainfrom
Conversation
…neToOne relationships to same entity Fixes jhipster#33264 When multiple unidirectional OneToOne relationships point from the same source entity to the same target entity, e.g.: relationship OneToOne { Order{recipient} to Member Order{publisher} to Member } Two bugs caused a 'duplicate properties' error: Bug 1 - addOtherRelationship (generators/base-application/support/relationship.ts): When auto-generating the back-reference name for a SQL OneToOne relationship, the function always defaulted to lowerFirst(entity.name) (e.g. 'order') for every relationship. With two relationships both pointing from Order to Member, Member received two injected back- references both named 'order', triggering the duplicate property check. Fix: Before assigning the default back-reference name, check whether that name already exists in the target entity's relationships. If it does, append the source relationship name as a disambiguator (e.g. 'orderAsRecipient', 'orderAsPublisher'). Bug 2 - setRelationshipsToEntity (lib/jdl/converters/jdl-to-json/jdl-to-json-relationship-converter.ts): The line: relationshipToConvert.injectedFieldInTo = relationshipToConvert.injectedFieldInTo ?? lowerFirst(relationshipToConvert.from); mutated the shared JDL relationship object. Since getRelatedRelationships() uses injectedFieldInTo to decide whether to include a relationship in the 'to' list, this mutation caused subsequent entity processing to incorrectly re-include the relationship, resulting in duplicate entries on the source entity (e.g. 'recipient' appearing twice on Order). Fix: Remove the mutation entirely. The relationshipName is already correctly derived from the extractField() call above it; the mutation was a side-effect that served no purpose other than causing this bug.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #33264
Problem
Multiple unidirectional OneToOne relationships targeting the same entity could generate duplicate relationship properties.
Example:
relationship OneToOne {
Order{recipient} to Member
Order{publisher} to Member
}
This resulted in duplicate property validation errors such as:
You have duplicate properties in entity Member: order
and duplicated relationship entries on the source entity.
Root Cause
Two separate issues caused the duplication:
In
addOtherRelationship(generators/base-application/support/relationship.ts), SQL OneToOne relationships generated identical default back-reference names (order) for multiple relationships targeting the same entity.In
setRelationshipsToEntity(lib/jdl/converters/jdl-to-json/jdl-to-json-relationship-converter.ts),injectedFieldInTowas unnecessarily mutated during entity processing, causing relationships to be re-included during subsequent processing passes.Solution
injectedFieldInToto prevent duplicate relationship processing.Tests
Added regression tests covering: