jhipster / jhipster-core

JHipster Domain Language, used by JHipster UML and JDL-Studio to generate entities
Apache License 2.0
345 stars 116 forks source link

JDL import does not set the otherEntityRelationshipName when processing the to part of a OneToMany relationship #299

Closed pvliss closed 5 years ago

pvliss commented 5 years ago
Overview of the issue

It has been reported by @jsm174 at Gitter that JHipster now generates warnings for missing otherEntityRelationshipName while it is actually defined. Quoting here his message from Gitter:

Anyone seeing warnings building apps with JDL in 5.8:

relationship OneToMany {
   Person{personAttributes} to PersonAttribute{person}
}

WARNING! otherEntityRelationshipName is missing in .jhipster/PersonAttribute.json for relationship {
    "relationshipType": "many-to-one",
    "relationshipName": "person",
    "otherEntityName": "person",
    "otherEntityField": "id",
    "otherEntityRelationshipNameUndefined": true,
    "otherEntityRelationshipName": "personAttribute"
}, using personAttribute as fallback

The relationship isn't "many-to-one", and seems to be defined okay.

He is in fact right that JHipster should not complain and this is a byproduct of the changes I made to https://github.com/jhipster/generator-jhipster/issues/8991. The bug is actually in the JSON file generated by JDL import where the 'otherEntityRelationshipName' is missing.

Motivation for or Use Case

The JDL import should generate a valid JSON file with all defined attributes

I have spotted the bug in lib/parser/entity_parser.js and more specifically at method setDestinationAssociationsForClass and will provide a PR

Reproduce the error

For example the test/test_files/big_sample.jdl the Employee{job} to Job{employee} defines the otherEntityRelationshipName as employee but if you see the generated json file for Job the otherEntityRelationshipName is missing.

Related issues

Relates to https://github.com/jhipster/generator-jhipster/issues/8991

Suggest a Fix

Already found a fix and will submit a PR.

JHipster Version(s)

5.8.0

jsm174 commented 5 years ago

Just tried out master generator + master jhipster-core, and I'm seeing this on ManyToOne's as well.

From the docs @ https://www.jhipster.tech/managing-relationships/#2, this is valid syntax:

entity Owner
entity Car

relationship ManyToOne {
  Car{owner} to Owner
}

Take this JDL:

application {
   config {
      baseName gateway,
      applicationType gateway,
      authenticationType oauth2,
      packageName com.example.gateway,
      prodDatabaseType mariadb,
      clientFramework react,
      languages [en],
      buildTool gradle
   }
   entities *
}

application {
   config {
      baseName person,
      applicationType microservice,
      authenticationType oauth2,
      packageName com.example.person,
      prodDatabaseType mariadb,
      buildTool gradle
   }
   entities Person, PersonAttribute, Identifier, IdentifierType
}

entity Person {
   uid String,
   createdAt Instant,
   lastUpdate Instant
}

entity PersonAttribute {
   key String required,
   value String,
   createdAt Instant,
   lastUpdate Instant
}

entity Identifier {
   data String required,
   createdAt Instant,
   lastUpdate Instant
}

entity IdentifierType {
   value String required,
   description String,
   createdAt Instant,
   lastUpdate Instant
}

relationship OneToMany {
   Person{personAttribute} to PersonAttribute{person}
}

relationship ManyToOne {
   Identifier{identifierType} to IdentifierType
}

paginate * with pagination

dto * with mapstruct
service * with serviceClass

microservice Person, PersonAttribute, Identifier, IdentifierType with person

Produces:

WARNING! otherEntityRelationshipName is missing in .jhipster/Identifier.json for relationship {
    "relationshipType": "many-to-one",
    "relationshipName": "identifierType",
    "otherEntityName": "identifierType",
    "otherEntityField": "id",
    "otherEntityRelationshipNameUndefined": true,
    "otherEntityRelationshipName": "identifier"
}, using identifier as fallback
pvliss commented 5 years ago

Thanks @jsm174 for reporting once more. To be honest I only checked the OneToMany case. I will check again later tonight when I get some free time

pvliss commented 5 years ago

@jsm174 Sorry for the delay but was quite busy. I submitted another PR that hopefully fixes all other cases as well, i.e. it always tries to set the otherEntityRelationshipName to either the one provided in the jdl or automatically. This will make the generator to stop issuing warnings