Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jdl: error passing 0 value in field annotations. #28312

Open
dharmeshmp opened this issue Dec 27, 2024 · 3 comments
Open

jdl: error passing 0 value in field annotations. #28312

dharmeshmp opened this issue Dec 27, 2024 · 3 comments

Comments

@dharmeshmp
Copy link

Issue Description

When adding new fields with @defaultValue in a JHipster entity, the generated Liquibase changelog does not correctly set the default values in the database. Specifically, the changelog generates the following incorrect XML:

<column name="games_played" type="integer" defaultValueNumeric="true">
    <constraints nullable="true" />
</column>

This leads to the following problems:

  1. defaultValueNumeric="true" is invalid and does not set the intended default value (e.g., 0).
  2. The column constraints incorrectly allow NULL values, even though the @defaultValue annotation implies it should be NOT NULL.

Steps to Reproduce

  1. Create a JHipster entity with fields that have @defaultValue annotations:
    entity Player {
        displayName String required
        coins Integer required
        @defaultValue(0) gamesPlayed Integer
        @defaultValue(0) gamesWon Integer
    }
  2. Generate the Liquibase changelog by running:
    jhipster jdl jhipster-jdl.jdl 
  3. Check the generated Liquibase changelog file.

Expected Behavior

The generated changelog should correctly define default values and nullable constraints. For example:

<column name="games_played" type="integer" defaultValueNumeric="0">
    <constraints nullable="false" />
</column>

Observed Behavior

The generated changelog incorrectly defines the columns as follows:

<column name="games_played" type="integer" defaultValueNumeric="true">
    <constraints nullable="true" />
</column>

Environment

  • JHipster Version: 8.8.0
  • JDK Version: 17.0.12
  • Database: MySQL
  • Operating System: macOS

Additional Context

This issue can lead to runtime errors or misaligned database schemas, especially when default values and non-null constraints are crucial for business logic.


Proposed Solution

Update the generator to:

  1. Ensure defaultValueNumeric is set to the numeric value provided in the @defaultValue annotation.
  2. Automatically set nullable="false" if a @defaultValue is defined.
@mshima
Copy link
Member

mshima commented Dec 28, 2024

The bug is in JDL parsing.
@defaultValue(1) works as expected, @defaultValue(0) fails.

You can use @defaultValue("0") as workaround.

@mshima mshima changed the title Liquibase changelog generates incorrect default values for new columns in an entity jdl: error passing 0 value in field annotations. Dec 28, 2024
@mshima
Copy link
Member

mshima commented Dec 28, 2024

I not sure if nullable behavior is expected.
@OmarHawk can you help here?

@OmarHawk
Copy link
Contributor

I not sure if nullable behavior is expected. @OmarHawk can you help here?

No, at the moment it is not expected that you don't pass a value. So, there should always be a parameter here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants