jhipster / jhipster-core

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

import-jdl ignores the config in .yo-rc.json #404

Closed martin-g closed 4 years ago

martin-g commented 4 years ago
Overview of the issue

I have generated an application with the help of jhipster command line tool. At application generation time I've answered all questions and my application was successfully generated. All answers of the questions are properly saved in .yo-rc.json file!

Later I've decided to add some new entities to my existing application, so I've used jhipster import-jdl entities.jh command. entities.json looks like:

application {
//  config {}
    entities *
}

entity TestEntity {
    code String required
}

dto * with mapstruct
service all with serviceImpl

The problem that I face, and I believe is a bug, is that the importer overrides the values in .yo-rc.json. For example at application generation time I've selected MongoDB as a database but now JHipster import-jdl command overrides the value to sql, because this is the default, I guess.

In my opinion jhipster import-jdl should use the values from .yo-rc.json if there are no custom config settings in entities.jh file, not to override them with the defaults.

Or I need to copy the answers from .yo-rc.json to entities.json > application > config ?

Motivation for or Use Case

N/A

Reproduce the error
  1. create an app with jhipster and select some non-default options, e.g. MongoDB as a database
  2. try to add new entity with jhipster import-jdl entities.json
  3. git diff .yo-rc.json and see that the original answers are changed
Related issues

I was not able to find a ticket for this issue.

Suggest a Fix

import-jdl command should use the values from .yo-rc.json and override them only if there is an option in the .jdl file

JHipster Version(s)

6.4.1

JHipster configuration
MyService@0.0.1-SNAPSHOT /home/martin/git/my-service
└── generator-jhipster@6.4.1 
JHipster configuration, a .yo-rc.json file generated in the root folder
.yo-rc.json file
{
  "generator-jhipster": {
    "databaseType": "mongodb",
    "devDatabaseType": "mongodb",
    "enableHibernateCache": false,
    "enableSwaggerCodegen": true,
    "enableTranslation": true,
    "jhiPrefix": "jhi",
    "languages": ["en", "de"],
    "messageBroker": false,
    "nativeLanguage": "en",
    "packageName": "com.tsystems.ucarsp",
    "packageFolder": "com/tsystems/ucarsp",
    "prodDatabaseType": "mongodb",
    "searchEngine": false,
    "serviceDiscoveryType": false,
    "skipClient": false,
    "skipServer": false,
    "testFrameworks": ["protractor", "cucumber", "gatling"],
    "websocket": false,
    "baseName": "MyService",
    "applicationType": "monolith",
    "buildTool": "gradle",
    "clientPackageManager": "npm",
    "authenticationType": "jwt",
    "clientFramework": "angularX",
    "jhipsterVersion": "6.4.1",
    "skipUserManagement": false,
    "cacheProvider": "ehcache",
    "clientTheme": "none",
    "clientThemeVariant": "",
    "serverPort": "8080",
    "useSass": true,
    "jwtSecretKey": "123",
    "embeddableLaunchScript": false,
    "entitySuffix": "",
    "dtoSuffix": "DTO",
    "otherModules": [],
    "blueprints": []
  },
  "entities": []
}

JDL for the Entity configuration(s) entityName.json files generated in the .jhipster directory
JDL entity definitions
entity TestEntity {
  code String required
}
dto ComplexType, TestEntity with mapstruct
service ComplexType, TestEntity with serviceImpl

Environment and Tools

openjdk version "1.8.0_232-ea" OpenJDK Runtime Environment (build 1.8.0_232-ea-8u232-b09-0ubuntu1-b09) OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)

git version 2.20.1

node: v11.12.0

npm: 6.12.0

yeoman: 3.1.0

Docker version 17.12.0-ce, build c97c6d6

docker-compose version 1.22.0, build f46880fe

Entity configuration(s) entityName.json files generated in the .jhipster directory

See above

Browsers and Operating System

N/A

MathieuAA commented 4 years ago

Thanks for reporting this issue! I or someone else will look into it

MathieuAA commented 4 years ago

The step to reproduce are incorrect, and that was how you described your issue that made me understand the issue... There is no bug here: if you first generate your app with the CLI then create a JDL file with an app definition there, it will obviously use default values because they weren't specified in the JDL.

We could take into account an existing .yo-rc.json when generating an app. That implies modifying the JDL file where the bare JDL app definition is to complete it, and that's something I don't wanna do because overriding what an user writes is just wrong: the user should have complete control over what's generated.

martin-g commented 4 years ago

Thanks for the answer, @MathieuAA ! But then how am I supposed to add entities to my application ? https://www.jhipster.tech/jdl/getting-started does not explain this use case.

If I remove

application {
//  config {}
    entities *
}

from entities.jh then the import does nothing.

The only way I was able to add entities is by duplicating the config settings in entities.jh > application > config. But this looks wrong to me. 1) why do I need to add them again if they are already in .yo-rc.json 2) the syntax in .yo-rc.json and in .jh files is different and it is not just a copy/paste operation.

MathieuAA commented 4 years ago

The issue is that having differences between a Yeoman config file and a JDL isn't supported. I'm open to suggestions because it the first time (if my memory doesn't fail me) that this use case is reported.

I'll think of something, and reopening this...

martin-g commented 4 years ago

I guess I miss something obvious. Could you please explain what is the normal workflow of using JDL after application creation ?

I'd imagine something like: 1) use jhipster to generate an application. Here I answer all the questions and those are saved in .yo-rc.json. 2) use jhipster import-jdl someFile.jh to add entities to my newly created application

Here I miss what I should put in the application > config part of someFile.jh. The only way I was able to use import-jdl is by manually map all settings from .yo-rc.json to someFile.jh. If this is the right way then I suggest this ticket to be converted to a feature request. jhipster import-jdl could use the values from .yo-rc.json or at application creation time jhipster could save one more config file if .yo-rc.json is purely Yeoman thing and should not be consumed by JHipster.

To avoid the manual mapping I even tried jhipster export-jdl exported.jh hoping that this will create a JDL file with the application config to which I will just add my entities and relationships and then import it. But it does not create exported.jh at all. Maybe because I have no existing entities so far. Is this a separate issue ?

MathieuAA commented 4 years ago

Instead of using the CLI to generate your app, you can use the JDL. If you define your app in the JDL but don't have the same options in your .yo-rc & JDL file, then it's normal you have the issues you mentioned.

martin-g commented 4 years ago

I see. Thanks for clarifying this! I'll let to you (the JHipster maintainers) decide what to do with this issue. Maybe you can just improve the documentation explaining the way JDL importing is supposed to be used.

MathieuAA commented 4 years ago

Maybe this needs better covering

MathieuAA commented 4 years ago

@martin-g Have you tried exporting to a JDL file and then using this base JDL file to further customize your application? (with entities for instance)

I'm doing some tests, that's why I ask you this.

MathieuAA commented 4 years ago

Okay this is tricky. From now on, JCore supports existing apps and reads info in them. This is done by passing the application attribute in the importer configuration. The other properties, except forceNoFiltering, are deprecated and will be removed in v8. Right now, passing an app configuration takes more precedence than the other attributes.

As a workaround on this issue: exporting to JDL and modifying the exported JDL file works. This is not needed anymore though.