In JHipster, the enum field type circulated from the client to the server or the database is expected to be a 'key' (as understood from discussion #12685). However, when dealing with enums that come with values, the generated client model code calls for the 'value' type, not the 'key'.
Motivation for or Use Case
We encounter difficulties in crafting accurate code when making decisions using enum fields or when setting a particular key to be transmitted to the server. For instance, the generated code appears like this:
export enum Language {
FRENCH = 'French-Label',
ENGLISH = 'English-Label',
SPANISH = 'Spanish-Label',
}
export interface IJobHistory {
language?: Language | null; // This interpretation is incorrect as it signifies 'French-Label', 'English-Label', 'Spanish-Label', or null.
}
An illustration of utilizing this code is as follows:
const entity: IJobHistory = ...; // Fetched 'FRENCH' from the server
if (entity.language === Language.FRENCH) { // At first glance, this seems correct, but 'FRENCH' !== 'French-Label'.
}
if (entity.language === 'FRENCH') { // Ideally, it should be written this way, but a type mismatch error occurs.
}
This issue is absent with enums in JDL that designate only the key, as the key and value coincide, such as in FRENCH='FRENCH'. This discrepancy can lead to substantial confusion.
Reproduce the error
When the code entity.language === 'FRENCH' is written in VSCode, an error will be flagged.
Related issues
12685
Suggest a Fix
I propose that the generated code should resemble this:
export interface IJobHistory {
language?: keyof typeof Language | null; // This interpretation is correct as it implies 'FRENCH', 'ENGLISH', 'SPANISH' or null
}
(If my proposal is accepted, I am willing to attempt creating a PR. I would appreciate your guidance throughout the process.)
openjdk version "17.0.7" 2023-04-18
OpenJDK Runtime Environment Temurin-17.0.7+7 (build 17.0.7+7)
OpenJDK 64-Bit Server VM Temurin-17.0.7+7 (build 17.0.7+7, mixed mode, sharing)
git version 2.39.2 (Apple Git-143)
node: v16.19.1
npm: 8.19.3
Docker version 23.0.5, build bc4487a
JDL for the Entity configuration(s) entityName.json files generated in the .jhipster directory
JDL entity definitions
entity JobHistory {
startDate Instant
endDate Instant
language Language
}
enum Language {
FRENCH (French-Label),
ENGLISH (English-Label),
SPANISH (Spanish-Label)
}
search JobHistory with no
Entity configuration(s) entityName.json files generated in the .jhipster directory
Overview of the issue
In JHipster, the enum field type circulated from the client to the server or the database is expected to be a 'key' (as understood from discussion #12685). However, when dealing with enums that come with values, the generated client model code calls for the 'value' type, not the 'key'.
Motivation for or Use Case
We encounter difficulties in crafting accurate code when making decisions using enum fields or when setting a particular key to be transmitted to the server. For instance, the generated code appears like this:
An illustration of utilizing this code is as follows:
This issue is absent with enums in JDL that designate only the key, as the key and value coincide, such as in
FRENCH='FRENCH'
. This discrepancy can lead to substantial confusion.Reproduce the error
When the code
entity.language === 'FRENCH'
is written in VSCode, an error will be flagged.Related issues
12685
Suggest a Fix
I propose that the generated code should resemble this:
(If my proposal is accepted, I am willing to attempt creating a PR. I would appreciate your guidance throughout the process.)
JHipster Version(s)
main(7.10), 7.9, etc...
JHipster configuration
.yo-rc.json file
Environment and Tools
openjdk version "17.0.7" 2023-04-18 OpenJDK Runtime Environment Temurin-17.0.7+7 (build 17.0.7+7) OpenJDK 64-Bit Server VM Temurin-17.0.7+7 (build 17.0.7+7, mixed mode, sharing)
git version 2.39.2 (Apple Git-143)
node: v16.19.1 npm: 8.19.3
Docker version 23.0.5, build bc4487a
JDL for the Entity configuration(s)
entityName.json
files generated in the.jhipster
directoryJDL entity definitions
Entity configuration(s)
entityName.json
files generated in the.jhipster
directoryJobHistory.json
Browsers and Operating System
environment-independent