snuyanzin commented on code in PR #29026: URL: https://github.com/apache/flink/pull/29026#discussion_r3907894196
########## flink-core/src/test/java/org/apache/flink/types/variant/JsonTokenSourceTest.java: ########## @@ -0,0 +1,209 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.flink.types.variant; + +import org.apache.flink.types.variant.JsonTokenSource.Token; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.IOException; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * Verifies a custom {@link JsonTokenSource} produces byte-for-byte the same encoding as the {@code + * parseJson(String)} path, and reaches error branches a real JSON parser never produces. + */ +class JsonTokenSourceTest { + + @Test + void testCustomSourceMatchesStringPathByteForByte() throws IOException { + // One document that spans every integer width, decimal, double, big integer, the scalars, + // nesting and a non-ASCII key so the metadata dictionary is built from foreign tokens. + final String jsonString = + "{\"b\":1,\"s\":200,\"i\":40000," + + "\"l\":3000000000,\"big\":9223372036854775808," + + "\"dec\":3.14,\"dbl\":1e10," + + "\"str\":\"hi\",\"t\":true,\"f\":false,\"n\":null," + + "\"arr\":[1,[2.5,\"x\"]],\"obj\":{\"k\":\"v\"}," + + "\"schlüssel\":\"Grüße 世界 🚀\"}"; + final Object model = + obj( + "b", + 1, + "s", + 200, + "i", + 40000, + "l", + 3000000000L, + "big", + new BigInteger("9223372036854775808"), + "dec", + new BigDecimal("3.14"), + "dbl", + 1e10, + "str", + "hi", + "t", + true, + "f", + false, + "n", + null, + "arr", + List.of(1, List.of(new BigDecimal("2.5"), "x")), + "obj", + obj("k", "v"), + "schlüssel", + "Grüße 世界 🚀"); + + final BinaryVariant fromString = BinaryVariantInternalBuilder.parseJson(jsonString, false); + final BinaryVariant fromSource = parseSource(model); + + assertThat(fromSource.getValue()).isEqualTo(fromString.getValue()); + assertThat(fromSource.getMetadata()).isEqualTo(fromString.getMetadata()); + } + + @Test + void testTruncatedObjectFromSourceIsRejected() { + // START_OBJECT and a field name, but the stream ends before the field's value. + final List<Tok> tokens = new ArrayList<>(); + tokens.add(new Tok(Token.START_OBJECT, null, null)); + tokens.add(new Tok(Token.FIELD_NAME, "a", null)); + + assertThatThrownBy( + () -> + BinaryVariantInternalBuilder.parseJson( + new ListJsonSource(tokens), false)) + .isInstanceOf(IOException.class); + } + + @ParameterizedTest + @ValueSource(strings = {"1,5", "1.000,5", "1'000"}) Review Comment: how about other non JSON numbers? for instance ``` 0x1p4, 0x1.8p3, 10d, 1.5f, +5, " 5 " (a number between spaces) ``` will they also be rejected? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
