Skip to content

Commit 5aa4380

Browse files
committed
correct some logic
1 parent efb8514 commit 5aa4380

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

sdks/python/apache_beam/yaml/json_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,10 @@ def _validate_compatible(weak_schema, strong_schema):
318318
if weak_schema['type'] == 'array':
319319
_validate_compatible(weak_schema['items'], strong_schema['items'])
320320
elif weak_schema['type'] == 'object':
321+
# If the weak schema allows for arbitrary keys (is a map),
322+
# the strong schema must also allow for arbitrary keys.
321323
if weak_schema.get('additionalProperties'):
322-
if not strong_schema.get('additionalProperties'):
324+
if not strong_schema.get('additionalProperties', True):
323325
raise ValueError('Incompatible types: map vs object')
324326
_validate_compatible(
325327
weak_schema['additionalProperties'],
@@ -328,12 +330,15 @@ def _validate_compatible(weak_schema, strong_schema):
328330
if required not in weak_schema['properties']:
329331
raise ValueError(f"Missing or unknown property '{required}'")
330332
for name, spec in weak_schema.get('properties', {}).items():
333+
331334
if name in strong_schema['properties']:
332335
try:
333336
_validate_compatible(spec, strong_schema['properties'][name])
334337
except Exception as exn:
335338
raise ValueError(f"Incompatible schema for '{name}'") from exn
336-
elif not strong_schema.get('additionalProperties'):
339+
elif not strong_schema.get('additionalProperties', True):
340+
# The property is not explicitly in the strong schema, and the strong
341+
# schema does not allow for extra properties.
337342
raise ValueError(
338343
f"Prohibited property: '{name}'; "
339344
"perhaps additionalProperties: False is missing?")

sdks/python/apache_beam/yaml/json_utils_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,34 @@ def test_validate_compatible_map(self):
248248
'type': 'object', 'properties': {}, 'additionalProperties': False
249249
})
250250

251+
def test_validate_compatible_extra_properties(self):
252+
from apache_beam.yaml.json_utils import _validate_compatible
253+
254+
# Extra properties in weak_schema should be allowed if strong_schema
255+
# doesn't explicitly forbid them (default additionalProperties=True).
256+
_validate_compatible({
257+
'type': 'object', 'properties': {
258+
'extra': {
259+
'type': 'string'
260+
}
261+
}
262+
}, {
263+
'type': 'object', 'properties': {}
264+
})
265+
266+
# But if strong_schema says additionalProperties: False, it should raise.
267+
with self.assertRaisesRegex(ValueError, 'Prohibited property'):
268+
_validate_compatible(
269+
{
270+
'type': 'object', 'properties': {
271+
'extra': {
272+
'type': 'string'
273+
}
274+
}
275+
}, {
276+
'type': 'object', 'properties': {}, 'additionalProperties': False
277+
})
278+
251279

252280
if __name__ == '__main__':
253281
unittest.main()

0 commit comments

Comments
 (0)