aoelvp94 commented on code in PR #77249: URL: https://github.com/apache/airflow/pull/77249#discussion_r4659839113
########## java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/execution/JsonValue.kt: ########## @@ -0,0 +1,97 @@ +/* + * 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.airflow.sdk.execution + +/** + * Checks that [value] can be stored as an XCom or task-state value, and returns + * it unchanged so later encoding matches the previous Jackson behaviour. + * + * [Frame] encodes a request by converting the message object to a map with + * Jackson. A Scala collection, `Option` or case class (none of which are + * `java.util.Map` / `java.util.Collection`) was stored as that object's bean + * properties, or as `{}` when it had none, and the task succeeded. This + * inspects the original value first, and rejects those Scala types and any + * other object Jackson would encode as `{}`. Java types that already encoded + * usefully (POJOs and Kotlin data classes with properties, records, enums, + * `java.time`, `Date`, `Optional`, and so on) still pass through. + * + * Nested values in a `Map`, `Collection` or array are checked the same way. + */ +internal fun jsonValue(value: Any?): Any? { + rejectUnsupportedXComValue(value) + return value +} + +private fun rejectUnsupportedXComValue(value: Any?) { + when (value) { + null, is String, is Boolean, is Number -> return + is ByteArray -> + throw IllegalArgumentException( + "Airflow does not accept byte arrays because they are not JSON values. " + + "Encode the bytes as a string first, for example with java.util.Base64.", + ) Review Comment: Went with the encoder in [7dcdb207e4](https://github.com/apache/airflow/pull/77249/commits/7dcdb207e408f0dde9034dfb92dc073ab2b418eb). `Frame` now encodes the value of `SetXCom` and `SetTaskStateStore` ([`toBody`](https://github.com/aoelvp94/airflow/blob/7dcdb207e408f0dde9034dfb92dc073ab2b418eb/java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/execution/Frame.kt#L176-L189)) with the request mapper plus `findAndRegisterModules()` ([`userValueMapper` / `encodeUserValue`](https://github.com/aoelvp94/airflow/blob/7dcdb207e408f0dde9034dfb92dc073ab2b418eb/java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/execution/Frame.kt#L79-L105)), as `ConfBinding` and `ArgValues` do, so a bundle with `jackson-module-scala`, which every Spark bundle has, gets correct JSON for Scala collections, `Option` and case classes instead of an error. Nested values go through the same path as the top-level value; map keys are written by Jackson's key serializers as JSON strings, unchanged by this PR. The byte-array branch is dropped, so `packAny`'s message is the only one. The encoded tree is read back with the plain request mapper, because `jackson-module-scala` also replaces untyped deserialization with Scala collections, which `packAny` cannot send. [`FrameTest`](https://github.com/aoelvp94/airflow/blob/7dcdb207e408f0dde9034dfb92dc073ab2b418eb/java-sdk/sdk/src/test/kotlin/org/apache/airflow/sdk/execution/FrameTest.kt#L38-L57) covers that. -- 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]
