Error generating enumeration type fields with command line entity generator. After updating the generator to the latest versions, I create a new project I have tried to add an entity from the command line 'jhipster entity NameEntity' and adding a field that is of type enumeration does not allow me to create the enumeration and when trying to create the entity throws the following error at me
```
The entity UserExtra is being created.
? Do you want to generate this entity from an existing microservice? No
Generating field jhipster/jhipster-vuejs#1
? Do you want to add a field to your entity? Yes
? What is the name of your field? gender
? What is the type of your field? Enumeration (Java enum type)
? Do you want to add validation rules to your field? No
================= UserExtra =================
Fields
gender (enum)
Generating field jhipster/jhipster-vuejs#2
? Do you want to add a field to your entity? No
================= UserExtra =================
Fields
gender (enum)
Generating relationships to other entities
? Do you want to add a relationship to another entity? No
================= UserExtra =================
Fields
gender (enum)
? Do you want to use separate service class for your business logic? Yes, generate a separate
service class
? Do you want to use a Data Transfer Object (DTO)? Yes, generate a DTO with MapStruct
? Do you want to add filtering? Not needed
? Is this entity read-only? No
? Do you want pagination on your entity? Yes, with infinite scroll
Everything is configured, generating the entity...
WARNING! changelogDate is missing in .jhipster/UserExtra.json, using 20200603133105 as fallback
info Using blueprint generator-jhipster-vuejs for entity-client subgenerator
events.js:288
throw er; // Unhandled 'error' event
^
Error: Copying template /home/acosta/Desktop/serf/node_modules/generator-jhipster/generators/entity-server/templates/src/main/resources/config/liquibase/fake-data/table.csv.ejs failed. [TypeError: /home/acosta/Desktop/serf/node_modules/generator-jhipster/generators/entity-server/templates/src/main/resources/config/liquibase/fake-data/table.csv.ejs:19
17| limitations under the License.
18| -%>
19| <%_
20| let table = [];
21|
22| // Generate CSV header
Cannot read property 'length' of undefined]
at module.exports.error (/home/acosta/Desktop/serf/node_modules/generator-jhipster/generators/generator-base.js:1590:15)
at /home/acosta/Desktop/serf/node_modules/generator-jhipster/generators/utils.js:256:23
at tryHandleCache (/home/acosta/Desktop/serf/node_modules/generator-jhipster/node_modules/ejs/lib/ejs.js:275:14)
at Object.exports.renderFile (/home/acosta/Desktop/serf/node_modules/generator-jhipster/node_modules/ejs/lib/ejs.js:478:10)
at Object.renderContent (/home/acosta/Desktop/serf/node_modules/generator-jhipster/generators/utils.js:252:9)
at module.exports.template (/home/acosta/Desktop/serf/node_modules/generator-jhipster/generators/generator-base-private.js:769:23)
at /home/acosta/Desktop/serf/node_modules/generator-jhipster/generators/generator-base.js:2040:42
at Array.forEach (
at module.exports.writeFilesToDisk (/home/acosta/Desktop/serf/node_modules/generator-jhipster/generators/generator-base.js:2002:45)
at module.exports.writeServerFiles (/home/acosta/Desktop/serf/node_modules/generator-jhipster/generators/entity-server/files.js:345:18)
Emitted 'error' event on instance at:
at Immediate.
##### **Motivation for or Use Case**
<!-- Explain why this is a bug for you -->
##### **Reproduce the error**
Create a new project and try to generate a new entity with the command line `jhipster entity NameEntity` and add a new field Enumeration
##### **JHipster Version(s)**
jhipster-generator v6.9.1
jhipster-generator-vuejs v1.8.2
<!--
Which version of JHipster are you using, is it a regression?
-->
##### **JHipster configuration**
{
"generator-jhipster": {
"promptValues": {
"packageName": "com.test",
"nativeLanguage": "es"
},
"jhipsterVersion": "6.9.1",
"applicationType": "gateway",
"baseName": "test",
"packageName": "com.test",
"packageFolder": "com/test",
"serverPort": "8080",
"authenticationType": "jwt",
"cacheProvider": "hazelcast",
"enableHibernateCache": true,
"websocket": false,
"databaseType": "sql",
"devDatabaseType": "h2Disk",
"prodDatabaseType": "postgresql",
"searchEngine": false,
"messageBroker": false,
"serviceDiscoveryType": "consul",
"buildTool": "gradle",
"enableSwaggerCodegen": false,
"jwtSecretKey": "Your Secrect Key",
"embeddableLaunchScript": false,
"useSass": true,
"clientPackageManager": "yarn",
"clientFramework": "vue",
"clientTheme": "none",
"clientThemeVariant": "",
"creationTimestamp": 1591070127581,
"testFrameworks": [],
"jhiPrefix": "jhi",
"entitySuffix": "",
"dtoSuffix": "DTO",
"otherModules": [
{
"name": "generator-jhipster-vuejs",
"version": "1.8.2"
}
],
"enableTranslation": true,
"nativeLanguage": "es",
"languages": ["es", "en"],
"blueprints": [
{
"name": "generator-jhipster-vuejs",
"version": "1.8.2"
}
]
}
}
```
Chrome Version 80.0.3987.149 (Official Build) (64-bit)
Brave Version 1.9.68 Chromium: 81.0.4044.138 (Official Build) unknown (64-bit)
OS: Manjaro 5.6.15-1-MANJARO
Thanks for reporting this, @yuniel-acosta : I can confirm the issue
I'm transfering this ticket to the main generator-jhipster, as I can reproduce with Angular too.
Probably related to https://github.com/jhipster/generator-jhipster/pull/11588 by @MathieuAA
BTW @pascalgrimaud , wasn't there a question to ask the enum's values? I could swear there was one.
@MathieuAA there are, but they are skipped:
{
when: response => {
if (response.fieldType === 'enum') {
response.fieldIsEnum = true;
return true;
}
response.fieldIsEnum = false;
return false;
},
type: 'input',
name: 'fieldType',
validate: input => {
if (input === '') {
return 'Your class name cannot be empty.';
}
if (jhiCore.isReservedKeyword(input, 'JAVA')) {
return 'Your enum name cannot contain a Java reserved keyword';
}
if (!/^[A-Za-z0-9_]*$/.test(input)) {
return 'Your enum name cannot contain special characters (allowed characters: A-Z, a-z, 0-9 and _)';
}
if (context.enums.includes(input)) {
context.existingEnum = true;
} else {
context.enums.push(input);
}
return true;
},
message: 'What is the class name of your enumeration?',
},
{
when: response => response.fieldIsEnum,
type: 'input',
name: 'fieldValues',
validate: input => {
if (input === '' && context.existingEnum) {
context.existingEnum = false;
return true;
}
if (input === '') {
return 'You must specify values for your enumeration';
}
// Commas allowed so that user can input a list of values split by commas.
if (!/^[A-Za-z0-9_,]+$/.test(input)) {
return 'Enum values cannot contain special characters (allowed characters: A-Z, a-z, 0-9 and _)';
}
const enums = input.replace(/\s/g, '').split(',');
if (_.uniq(enums).length !== enums.length) {
return `Enum values cannot contain duplicates (typed values: ${input})`;
}
for (let i = 0; i < enums.length; i++) {
if (/^[0-9].*/.test(enums[i])) {
return `Enum value "${enums[i]}" cannot start with a number`;
}
if (enums[i] === '') {
return 'Enum value cannot be empty (did you accidentally type "," twice in a row?)';
}
}
return true;
},
message: answers => {
if (!context.existingEnum) {
return 'What are the values of your enumeration (separated by comma, no spaces)?';
}
return 'What are the new values of your enumeration (separated by comma, no spaces)?\nThe new values will replace the old ones.\nNothing will be done if there are no new values.';
},
},
Quick note, this should be related to prompting. An entity with Enum can still be created using JDL.
I don't see where they are skipped...
I don't see where they are skipped...
That's the issue. I have played around with it yesterday, noticed that if the value of name property is changed (as it's the same value used in initial type selection) - questions are shown. Trying to retest this on clean generator-jhipster as I was working with our blueprint while testing that.
Oh yeah, everything's skipped. That's a great issue :)
I just found the problem. I am creating the PR.
@mshima, I see you renamed the field. What is the underlying reason for that? Inquirer does not allow questions with the same name?
I guess so @rimselis. Shadowing isn't allowed I guess
Allowed with askAnswered: true, but not working due to generator changing it. I will fix this.
I will give a more detailed explanation about this, pretty busy now.
A workaround to this bug is jhipster entity Foo --ask-answered.
This behavior allows to simplify generators in general.
Prompts should be as dumb as possible, and almost every time they are stored.
So current generators can implement this:
this.prompt({name: 'myQuestion'}, this.config);
If myQuestion is not undefined at this.config, the prompt is skipped, and every answer is stored at this.config automatically.
At templates part:
this.renderTemplate('myFile.md');
Allows to use <%= myQuestion %> directly.
So every question load/save and template data load/save can be skipped.
Ah! great explanation @mshima, thanks!!
Most helpful comment
Allowed with
askAnswered: true, but not working due to generator changing it. I will fix this.I will give a more detailed explanation about this, pretty busy now.