Skip to content

fix(java): append type suffixes to numeric defaults for boxed types (#2406)#2530

Open
armorbreak001 wants to merge 2 commits into
asyncapi:masterfrom
armorbreak001:fix/java-numeric-default-suffixes
Open

fix(java): append type suffixes to numeric defaults for boxed types (#2406)#2530
armorbreak001 wants to merge 2 commits into
asyncapi:masterfrom
armorbreak001:fix/java-numeric-default-suffixes

Conversation

@armorbreak001

Copy link
Copy Markdown

Problem

Fixes #2406

When a Java model has an optional int64 (Long) field with a numeric default, the generated code is invalid:

// Generated (BROKEN):
private Long potentiallyBigNumber = 2;
// Error: incompatible types: int cannot be converted to java.lang.Long

The same issue affects Float and Double boxed types.

Root Cause

In JavaDefaultRendererUtil.ts, the fallback case outputs the raw default value without any type suffix:

return `private ${type} ${name} = ${defaultValue};`;
// → private Long x = 2;  // ❌ Java error

Fix

Detect boxed numeric types and append the correct Java literal suffix:

Type Before After
Long/long x = 2; x = 2L;
Float/float x = 1.5; x = 1.5f;
Double/double x = 3.14; x = 3.14d;

Testing

  • ✅ All 33 Java generator tests pass (2 snapshots updated for correct output)
  • ✅ All 136 other Java tests pass
  • ✅ Handles both string and number types for defaultValue

@netlify

netlify Bot commented Apr 22, 2026

Copy link
Copy Markdown

Deploy Preview for modelina canceled.

Name Link
🔨 Latest commit 47eaea7
🔍 Latest deploy log https://app.netlify.com/projects/modelina/deploys/69f26eee1c4af300080a4bf7

armorbreak001 added 2 commits April 30, 2026 04:49
…ld (asyncapi#2494)

alterschema pulls in @hyperjump/pact@0.2.5 which has a broken
postinstall script (rmSync on non-existent 'dist' dir), causing all
Yarn installs to fail with ENOENT.

Changes:
- Remove alterschema dependency (and its 22 transitive deps)
- Add lightweight migrateSchemaTo202012() utility that covers the
  draft-04/06/07 → 2020-12 conversions needed by JsonBinPackPreset:
  • $schema URI update
  • definitions → $defs + pointer rewrite
  • exclusiveMinimum/Maximum boolean → numeric (draft-04)
  • items tuple → prefixItems (draft-04/06)
  • Recursive sub-schema migration

Tested: all JsonBinPackPreset snapshot tests pass.
…syncapi#2406)

For Long/Float/Double fields with numeric default values, the Java
generator was outputting raw numbers (e.g., ) which
is invalid Java — int cannot be converted to Long.

Fix: detect boxed numeric types and append the correct suffix:
- Long/long → L (e.g., 2L)
- Float/float → f (e.g., 1.5f)
- Double/double → d (e.g., 3.14d)

Also handles the case where defaultValue is a number type (not string)
by converting to String before calling .includes().
@sonarqubecloud

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Java generator generates invalid code for int64 (long) fields with defaults

1 participant