@@ -1257,3 +1257,36 @@ def test_post_should_accept_empty_string_as_extra(self, test_client, session):
1257
1257
connection = session .query (Connection ).filter_by (conn_id = TEST_CONN_ID ).first ()
1258
1258
assert connection is not None
1259
1259
assert connection .extra == "{}" # Backward compatibility: treat "" as empty JSON object
1260
+
1261
+ @pytest .mark .parametrize (
1262
+ "extra, expected_error_message" ,
1263
+ [
1264
+ ("[1,2,3]" , "Expected JSON object in `extra` field, got non-dict JSON" ),
1265
+ ("some_string" , "Encountered non-JSON in `extra` field" ),
1266
+ ],
1267
+ )
1268
+ def test_post_should_fail_with_non_json_object_as_extra (
1269
+ self , test_client , extra , expected_error_message , session
1270
+ ):
1271
+ """JSON primitives are a valid JSON and should raise 422 validation error."""
1272
+ body = {"connection_id" : TEST_CONN_ID , "conn_type" : TEST_CONN_TYPE , "extra" : extra }
1273
+
1274
+ response = test_client .post ("/connections" , json = body )
1275
+ assert response .status_code == 422
1276
+ assert (
1277
+ "Value error, The `extra` field must be a valid JSON object (e.g., {'key': 'value'})"
1278
+ in response .json ()["detail" ][0 ]["msg" ]
1279
+ )
1280
+
1281
+ _check_last_log (
1282
+ session ,
1283
+ dag_id = None ,
1284
+ event = "post_connection" ,
1285
+ logical_date = None ,
1286
+ expected_extra = {
1287
+ "connection_id" : "test_connection_id" ,
1288
+ "conn_type" : "test_type" ,
1289
+ "extra" : expected_error_message ,
1290
+ "method" : "POST" ,
1291
+ },
1292
+ )
0 commit comments