This is an automated email from the ASF dual-hosted git repository.
shahar1 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 47d0c64c9cd Fix GcpBodyFieldValidator warning on valid
version-specific fields (#74930)
47d0c64c9cd is described below
commit 47d0c64c9cdc15c13044ad4cbaf3f3160bbcf325
Author: Udaya Vijay Anand <[email protected]>
AuthorDate: Sun Nov 8 08:59:03 2026 -0500
Fix GcpBodyFieldValidator warning on valid version-specific fields (#74930)
* Fix GcpBodyFieldValidator warning on valid version-specific fields
The post-validation check that reports body fields "not specified in the
validation specification" built its set of known field names with an
inverted api_version filter:
if spec.get("type") != "union" and spec.get("api_version") !=
self._api_version
A non-union field whose spec ``api_version`` equals the validator's own
``api_version`` is a valid, applicable field (``_validate_field`` only
skips a field when its ``api_version`` differs). The ``!=`` comparison
excluded exactly those applicable fields from ``all_field_names``, so a
correct, spec-defined, successfully validated field present in the body
produced a spurious "field is in the body, but is not specified in the
validation specification" warning. The same inverted filter was applied
to union-nested field names.
Include a field whenever it has no ``api_version`` or its ``api_version``
matches the validator's, so version-specific fields validated for the
current API version no longer warn, while fields that belong to a
different API version still do (as the warning text intends).
Signed-off-by: Udaya Tejas <[email protected]>
* Parametrize GcpBodyFieldValidator api_version warning tests
Cover both directions of the api_version filter change: a field tagged
with the validator's api_version no longer warns, and a field tagged with
a different api_version now does. Patch the validator's logger instead of
asserting on caplog.text, per review.
Signed-off-by: Udaya Tejas <[email protected]>
Generated-by: Claude Code following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
---------
Signed-off-by: Udaya Tejas <[email protected]>
---
.../google/cloud/utils/field_validator.py | 8 +++--
.../google/cloud/utils/test_field_validator.py | 42 ++++++++++++++++++++++
2 files changed, 48 insertions(+), 2 deletions(-)
diff --git
a/providers/google/src/airflow/providers/google/cloud/utils/field_validator.py
b/providers/google/src/airflow/providers/google/cloud/utils/field_validator.py
index 79df7ee130c..fa1f525e768 100644
---
a/providers/google/src/airflow/providers/google/cloud/utils/field_validator.py
+++
b/providers/google/src/airflow/providers/google/cloud/utils/field_validator.py
@@ -437,7 +437,8 @@ class GcpBodyFieldValidator(LoggingMixin):
all_field_names = {
spec["name"]
for spec in self._validation_specs
- if spec.get("type") != "union" and spec.get("api_version") !=
self._api_version
+ if spec.get("type") != "union"
+ and (not spec.get("api_version") or spec.get("api_version") ==
self._api_version)
}
all_union_fields = [spec for spec in self._validation_specs if
spec.get("type") == "union"]
for union_field in all_union_fields:
@@ -445,7 +446,10 @@ class GcpBodyFieldValidator(LoggingMixin):
nested_union_spec["name"]
for nested_union_spec in union_field["fields"]
if nested_union_spec.get("type") != "union"
- and nested_union_spec.get("api_version") != self._api_version
+ and (
+ not nested_union_spec.get("api_version")
+ or nested_union_spec.get("api_version") ==
self._api_version
+ )
)
for field_name in body_to_validate:
if field_name not in all_field_names:
diff --git
a/providers/google/tests/unit/google/cloud/utils/test_field_validator.py
b/providers/google/tests/unit/google/cloud/utils/test_field_validator.py
index e504de795e1..a4c28c47bdb 100644
--- a/providers/google/tests/unit/google/cloud/utils/test_field_validator.py
+++ b/providers/google/tests/unit/google/cloud/utils/test_field_validator.py
@@ -16,6 +16,8 @@
# under the License.
from __future__ import annotations
+from unittest import mock
+
import pytest
from airflow.providers.google.cloud.utils.field_validator import (
@@ -313,3 +315,43 @@ class TestGcpBodyFieldValidator:
validator = GcpBodyFieldValidator(specification, "v1")
validator.validate(body)
+
+ @pytest.mark.parametrize(
+ ("field_api_version", "should_warn"),
+ [
+ pytest.param("v1", False, id="matching-api-version"),
+ pytest.param("v1beta2", True, id="other-api-version"),
+ ],
+ )
+ def test_validate_warns_only_for_fields_of_other_api_version(self,
field_api_version, should_warn):
+ specification = [
+ dict(name="name", allow_empty=False),
+ dict(name="versioned_field", optional=True,
api_version=field_api_version),
+ ]
+ validator = GcpBodyFieldValidator(specification, "v1")
+ with mock.patch.object(validator.log, "warning") as mock_warning:
+ validator.validate({"name": "value", "versioned_field": "present"})
+ assert mock_warning.called is should_warn
+
+ @pytest.mark.parametrize(
+ ("field_api_version", "should_warn"),
+ [
+ pytest.param("v1", False, id="matching-api-version"),
+ pytest.param("v1beta2", True, id="other-api-version"),
+ ],
+ )
+ def test_validate_warns_only_for_union_fields_of_other_api_version(self,
field_api_version, should_warn):
+ specification = [
+ dict(
+ name="an_union",
+ type="union",
+ optional=True,
+ fields=[
+ dict(name="variant", regexp=r"^.+$",
api_version=field_api_version),
+ ],
+ ),
+ ]
+ validator = GcpBodyFieldValidator(specification, "v1")
+ with mock.patch.object(validator.log, "warning") as mock_warning:
+ validator.validate({"variant": "abc"})
+ assert mock_warning.called is should_warn