uranusjr commented on code in PR #77249: URL: https://github.com/apache/airflow/pull/77249#discussion_r4629340049
########## 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.", + ) + is Map<*, *> -> value.values.forEach(::rejectUnsupportedXComValue) + is Collection<*> -> value.forEach(::rejectUnsupportedXComValue) + is Array<*> -> value.forEach(::rejectUnsupportedXComValue) + is IntArray, is LongArray, is ShortArray, is DoubleArray, is FloatArray, is BooleanArray, is CharArray -> + return + else -> { + if (isScalaType(value.javaClass)) { + throw IllegalArgumentException(unsupportedScalaXComMessage(value)) + } Review Comment: I’m not convinced we need this special branch. Scala types can be handled correctly with the check below, just with a slightly generic message. I don’t see a need to special-case Scala types from other unknown types. ########## 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: Duplicates the exact message and check in `packAny`. Extract one shared constant/function, or drop this branch since `packAny` already rejects it. ########## 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. + */ Review Comment: Comment describes history that belongs in the PR/commit, not the KDoc. Reduce to one sentence stating the contract. ########## 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: I think this hints at a larger issue. This entire validation seems to belong on encoding instead e.g. `packAny`, not in a validator called from two client methods. Either encode the user value explicitly with a well-defined policy instead of a denylist that guesses at one JVM language, or make the mapper extensible so Scala users can add jackson-module-scala and get correct encoding rather than an error. Doing this here also misses e.g. nested values, map keys, and else. ########## 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 +} Review Comment: This returns its argument unchanged, but the name suggests a conversion. Make it e.g. `requireStorableValue(value)` returning Unit (), and call it as a statement instead. -- 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]
