-
Notifications
You must be signed in to change notification settings - Fork 51
fix(QTDI-2912): fix Possible ClassCastException in SchemaConverter #1231
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
Open
undx
wants to merge
3
commits into
master
Choose a base branch
from
ouf/QTDI-2912-Possible-ClassCastException-in-SchemaConverter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+129
−12
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...st/java/org/talend/sdk/component/runtime/manager/xbean/converter/SchemaConverterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /** | ||
| * Copyright (C) 2006-2026 Talend Inc. - www.talend.com | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.talend.sdk.component.runtime.manager.xbean.converter; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.BigInteger; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| import javax.json.JsonNumber; | ||
| import javax.json.JsonObject; | ||
| import javax.json.JsonValue; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.talend.sdk.component.api.record.Schema; | ||
| import org.talend.sdk.component.api.service.record.RecordBuilderFactory; | ||
| import org.talend.sdk.component.runtime.record.RecordBuilderFactoryImpl; | ||
|
|
||
| class SchemaConverterTest { | ||
|
|
||
| private final RecordBuilderFactory factory = new RecordBuilderFactoryImpl("test"); | ||
|
|
||
| private final SchemaConverter converter = new SchemaConverter(); | ||
|
|
||
| /** | ||
| * Exercises {@link SchemaConverter#toValue(Object)} via the public | ||
| * {@link SchemaConverter#toJson(Schema)} API by building a single-entry record schema whose entry has | ||
| * the requested default value, and returning the {@code defaultValue} JSON node produced by the | ||
| * converter. | ||
| * <p> | ||
| * The {@code entryType} only drives entry construction; the converter routes the default value | ||
| * through {@link SchemaConverter#toValue(Object)} regardless of the declared type. | ||
| */ | ||
| private JsonValue defaultValueOf(final Schema.Type entryType, final Object defaultValue) { | ||
| final Schema.Entry entry = factory | ||
| .newEntryBuilder() | ||
| .withName("field") | ||
| .withType(entryType) | ||
| .withNullable(true) | ||
| .withDefaultValue(defaultValue) | ||
| .build(); | ||
| final Schema schema = factory.newSchemaBuilder(Schema.Type.RECORD).withEntry(entry).build(); | ||
| final JsonObject json = converter.toJson(schema); | ||
| return json.getJsonArray(SchemaConverter.ENTRIES).getJsonObject(0).get("defaultValue"); | ||
| } | ||
|
|
||
| @Test | ||
| void toJsonHandlesFloatAndDoubleDefaultValues() { | ||
| // Regression: previously a Float default value triggered ClassCastException in toValue. | ||
| final JsonNumber fromFloat = (JsonNumber) defaultValueOf(Schema.Type.FLOAT, 1.5f); | ||
| assertEquals(1.5d, fromFloat.doubleValue(), 0.0001d); | ||
|
|
||
| final JsonNumber fromDouble = (JsonNumber) defaultValueOf(Schema.Type.DOUBLE, 2.5d); | ||
| assertEquals(2.5d, fromDouble.doubleValue(), 0.0001d); | ||
| } | ||
|
|
||
| @Test | ||
| void toJsonHandlesIntegralNumberSubtypesWithoutDecimalRepresentation() { | ||
| // Short / Byte must serialize as integers (no .0 suffix), via the Number/longValue() fallback. | ||
| final JsonNumber fromShort = (JsonNumber) defaultValueOf(Schema.Type.INT, (short) 7); | ||
| assertTrue(fromShort.isIntegral(), "Short must serialize as integral"); | ||
| assertEquals(7L, fromShort.longValue()); | ||
|
|
||
| final JsonNumber fromByte = (JsonNumber) defaultValueOf(Schema.Type.INT, (byte) 3); | ||
| assertTrue(fromByte.isIntegral(), "Byte must serialize as integral"); | ||
| assertEquals(3L, fromByte.longValue()); | ||
| } | ||
|
|
||
| @Test | ||
| void toJsonKeepsBigDecimalAndBigIntegerPrecision() { | ||
| // Guard against a regression where the Number fallback would swallow BigDecimal/BigInteger | ||
| // and silently lose precision through doubleValue()/longValue(). | ||
| final BigDecimal bd = new BigDecimal("12345678901234567890.123456789"); | ||
| final JsonNumber fromBigDecimal = (JsonNumber) defaultValueOf(Schema.Type.DECIMAL, bd); | ||
| assertEquals(bd, fromBigDecimal.bigDecimalValue()); | ||
|
|
||
| // No dedicated Schema.Type for BigInteger; the entry type is irrelevant here, only the | ||
| // default-value routing through toValue() matters. | ||
| final BigInteger bi = new BigInteger("123456789012345678901234567890"); | ||
| final JsonNumber fromBigInteger = (JsonNumber) defaultValueOf(Schema.Type.STRING, bi); | ||
| assertEquals(bi, fromBigInteger.bigIntegerValue()); | ||
| } | ||
|
|
||
| @Test | ||
| void toJsonPreservesLargeAtomicLongValues() { | ||
| // AtomicLong falls into the Number fallback. longValue() must be used (not doubleValue()) | ||
| // so that values beyond 2^53 (not exactly representable as double) are preserved. | ||
| final long big = (1L << 53) + 1L; // 9007199254740993, +1 beyond double precision | ||
| final JsonNumber fromAtomicLong = | ||
| (JsonNumber) defaultValueOf(Schema.Type.LONG, new AtomicLong(big)); | ||
| assertTrue(fromAtomicLong.isIntegral(), "AtomicLong must serialize as integral"); | ||
| assertEquals(big, fromAtomicLong.longValue()); | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.