jhipster / generator-jhipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.
https://www.jhipster.tech
Apache License 2.0
21.54k stars 4.02k forks source link

Inappropriate Type for Enum Fields with Values in Client's Model #22199

Closed hide212131 closed 1 year ago

hide212131 commented 1 year ago
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:

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.)

JHipster Version(s)

main(7.10), 7.9, etc...

JHipster configuration
.yo-rc.json file
{
  "applicationType": "monolith",
  "authenticationType": "jwt",
  "baseName": "myApp",
  "buildTool": "gradle",
  "cacheProvider": "ehcache",
  "clientFramework": "react",
  "clientTheme": "none",
  "creationTimestamp": 1684558849679,
  "databaseType": "sql",
  "devDatabaseType": "h2Disk",
  "devServerPort": 9060,
  "dtoSuffix": "DTO",
  "enableGradleEnterprise": null,
  "enableHibernateCache": true,
  "enableSwaggerCodegen": false,
  "enableTranslation": true,
  "entities": [
    "JobHistory"
  ],
  "entitySuffix": "",
  "gradleEnterpriseHost": null,
  "jhiPrefix": "jhi",
  "jhipsterVersion": "7.10.0",
  "languages": [
    "ja",
    "en",
    "fr"
  ],
  "lastLiquibaseTimestamp": 1684560013000,
  "messageBroker": false,
  "microfrontend": false,
  "microfrontends": [],
  "nativeLanguage": "ja",
  "packageFolder": "com/mycompany/myapp",
  "packageName": "com.mycompany.myapp",
  "pages": [],
  "prodDatabaseType": "postgresql",
  "reactive": false,
  "searchEngine": false,
  "serverPort": null,
  "serverSideOptions": [],
  "serviceDiscoveryType": false,
  "skipCheckLengthOfIdentifier": false,
  "skipClient": false,
  "skipFakeData": false,
  "skipUserManagement": false,
  "testFrameworks": [],
  "websocket": false,
  "withAdminUi": true
}
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 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
JobHistory.json
{
  "applications": "*",
  "changelogDate": "20230520052013",
  "entityTableName": "job_history",
  "fields": [
    {
      "fieldName": "startDate",
      "fieldType": "Instant"
    },
    {
      "fieldName": "endDate",
      "fieldType": "Instant"
    },
    {
      "fieldName": "language",
      "fieldType": "Language",
      "fieldValues": "FRENCH (French-Label),ENGLISH (English-Label),SPANISH (Spanish-Label)"
    }
  ],
  "name": "JobHistory",
  "relationships": [],
  "searchEngine": "no"
}
Browsers and Operating System

environment-independent

mshima commented 1 year ago

Please go ahead.