Skip to content

Commit 45483db

Browse files
committed
fix(event): remove max length restriction for extension attributes
Remove the 20-character maximum length restriction for extension attribute names in both v03 and v1 event versions. Update validation error messages and add tests to ensure compatibility with longer attribute names. Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>
1 parent 93e47fe commit 45483db

4 files changed

Lines changed: 36 additions & 32 deletions

File tree

‎src/cloudevents/core/v03/event.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ def _validate_extension_attributes(
267267
msg="Extension attribute 'data' is reserved and must not be used",
268268
)
269269
)
270-
if not (1 <= len(extension_attribute) <= 20):
270+
if not (1 <= len(extension_attribute)):
271271
errors[extension_attribute].append(
272272
CustomExtensionAttributeError(
273273
attribute_name=extension_attribute,
274-
msg=f"Extension attribute '{extension_attribute}' should be between 1 and 20 characters long",
274+
msg=f"Extension attribute name must be at least 1 character long but was '{extension_attribute}'",
275275
)
276276
)
277277
if not re.match(r"^[a-z0-9]+$", extension_attribute):

‎src/cloudevents/core/v1/event.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,11 @@ def _validate_extension_attributes(
252252
msg="Extension attribute 'data' is reserved and must not be used",
253253
)
254254
)
255-
if not (1 <= len(extension_attribute) <= 20):
255+
if not (1 <= len(extension_attribute)):
256256
errors[extension_attribute].append(
257257
CustomExtensionAttributeError(
258258
attribute_name=extension_attribute,
259-
msg=f"Extension attribute '{extension_attribute}' should be between 1 and 20 characters long",
259+
msg=f"Extension attribute name must be at least 1 character long but was '{extension_attribute}'",
260260
)
261261
)
262262
if not re.match(r"^[a-z0-9]+$", extension_attribute):

‎tests/test_core/test_v03/test_event.py‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def test_required_attributes_null_or_empty(
410410
str(
411411
CustomExtensionAttributeError(
412412
"",
413-
"Extension attribute '' should be between 1 and 20 characters long",
413+
"Extension attribute name must be at least 1 character long but was ''",
414414
)
415415
),
416416
str(
@@ -422,19 +422,6 @@ def test_required_attributes_null_or_empty(
422422
]
423423
},
424424
),
425-
(
426-
"thisisaverylongextension",
427-
{
428-
"thisisaverylongextension": [
429-
str(
430-
CustomExtensionAttributeError(
431-
"thisisaverylongextension",
432-
"Extension attribute 'thisisaverylongextension' should be between 1 and 20 characters long",
433-
)
434-
)
435-
]
436-
},
437-
),
438425
(
439426
"data",
440427
{
@@ -468,6 +455,21 @@ def test_custom_extension(extension_name: str, expected_error: dict) -> None:
468455
assert actual_errors == expected_error
469456

470457

458+
def test_long_extension_attribute_name() -> None:
459+
# Verify that extension attribute names longer than 20 characters are allowed
460+
long_name = "a" * 21
461+
event = CloudEvent(
462+
{
463+
"id": "1",
464+
"source": "/",
465+
"type": "test",
466+
"specversion": "0.3",
467+
long_name: "value",
468+
}
469+
)
470+
assert event.get_extension(long_name) == "value"
471+
472+
471473
def test_default_specversion() -> None:
472474
event = CloudEvent(
473475
attributes={"source": "/source", "type": "test", "id": "1"},

‎tests/test_core/test_v1/test_event.py‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def test_required_attributes_null_or_empty(
346346
str(
347347
CustomExtensionAttributeError(
348348
"",
349-
"Extension attribute '' should be between 1 and 20 characters long",
349+
"Extension attribute name must be at least 1 character long but was ''",
350350
)
351351
),
352352
str(
@@ -358,19 +358,6 @@ def test_required_attributes_null_or_empty(
358358
]
359359
},
360360
),
361-
(
362-
"thisisaverylongextension",
363-
{
364-
"thisisaverylongextension": [
365-
str(
366-
CustomExtensionAttributeError(
367-
"thisisaverylongextension",
368-
"Extension attribute 'thisisaverylongextension' should be between 1 and 20 characters long",
369-
)
370-
)
371-
]
372-
},
373-
),
374361
(
375362
"data",
376363
{
@@ -404,6 +391,21 @@ def test_custom_extension(extension_name: str, expected_error: dict) -> None:
404391
assert actual_errors == expected_error
405392

406393

394+
def test_long_extension_attribute_name() -> None:
395+
# Verify that extension attribute names longer than 20 characters are allowed
396+
long_name = "a" * 21
397+
event = CloudEvent(
398+
{
399+
"id": "1",
400+
"source": "/",
401+
"type": "test",
402+
"specversion": "1.0",
403+
long_name: "value",
404+
}
405+
)
406+
assert event.get_extension(long_name) == "value"
407+
408+
407409
def test_default_specversion() -> None:
408410
event = CloudEvent(
409411
attributes={"source": "/source", "type": "test", "id": "1"},

0 commit comments

Comments
 (0)