Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2018.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed xCal serialization of parameters whose names overlap with property names by looking up parameter types separately from property types. I used AI to assist with this change. @shekhar4704
2 changes: 1 addition & 1 deletion src/icalendar/parser/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def to_xcal(self, element: Element) -> None:
for key in sorted(self):
if key == "VALUE":
continue
value_factory = factory.for_property(key)
value_factory = factory.for_parameter(key)
param_element = Element(key.lower())
for value in self.get_multiple(key):
# value is expected to be str or a subclass of str
Expand Down
40 changes: 30 additions & 10 deletions src/icalendar/prop/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def instance() -> TypesFactory:
return TypesFactory._instance

def __init__(self, *args: Any, **kwargs: Any) -> None:
"""Set keys to upper for initial dict"""
"""Set keys to upper for initial dict."""
super().__init__(*args, **kwargs)
self.all_types = (
vBinary,
Expand Down Expand Up @@ -202,8 +202,11 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
"xml": "xml-text",
# Miscellaneous Component Properties
"request-status": "request-status-text",
####################################
# parameter types (luckily there is no name overlap)
}
)

parameter_types_map = CaselessDict(
{
"altrep": "uri",
"cn": "text",
"cutype": "text",
Expand All @@ -222,19 +225,19 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
"role": "text",
"rsvp": "boolean",
"sent-by": "cal-address",
"tzid": "text",
"value": "text",
# rfc 9253 parameters

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That comment is useful.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add it back?

"label": "text",
"linkrel": "text",
"gap": "duration",
}
Comment thread
niccokunzmann marked this conversation as resolved.
Resolved
)

def for_property(self, name, value_param: str | None = None) -> type[VPROPERTY]:
"""Returns the type class for a property or parameter.
"""Returns the type class for a property.

Parameters:
name: Property or parameter name
name: Property name.
value_param: Optional ``VALUE`` parameter, for example,
"DATE", "DATE-TIME", or other string.

Expand All @@ -243,6 +246,7 @@ def for_property(self, name, value_param: str | None = None) -> type[VPROPERTY]:
"""
name = name.upper()
value_param = value_param.upper() if value_param else None

# RFC 7265's UNKNOWN type is always represented verbatim, even for
# properties such as RDATE/EXDATE that normally use list parsing.
if value_param and value_param == "UNKNOWN":
Expand All @@ -254,6 +258,7 @@ def for_property(self, name, value_param: str | None = None) -> type[VPROPERTY]:
value_param is None or value_param in vDDDTypes.VALUE_MAP
):
return self["date-time-list"]

# Only use VALUE parameter for known properties that support multiple value
# types (like DTSTART, DTEND, etc. which can be DATE or DATE-TIME)
# For unknown/custom properties, always use the default type from types_map
Expand All @@ -265,6 +270,7 @@ def for_property(self, name, value_param: str | None = None) -> type[VPROPERTY]:
if value_type.default_value == value_param:
return value_type
return self[value_param]

if value_param and (value_param in self) and value_param != "IMAGE":
return self[value_param]

Expand Down Expand Up @@ -294,14 +300,19 @@ def for_property(self, name, value_param: str | None = None) -> type[VPROPERTY]:
}
)

def for_parameter(self, name: str) -> type[VPROPERTY]:
"""Returns the type class for a parameter."""
return self[self.parameter_types_map.get(name.upper(), "unknown")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice! So much smaller and cleaner than passing this through the other one.

Could you also add Parameters: and Returns: here according to our style guide?


def default_value_type(self, name: str) -> str:
"""Return the default jCal value type for a property.

The result is a lowercase :rfc:`7265` value type, for example
``"date-time"`` for ``DTSTART`` or ``"duration"`` for ``TRIGGER``.
This is the value type a property uses when no explicit ``VALUE``
parameter is given, so it tells :meth:`Component.from_jcal <icalendar.cal.component.Component.from_jcal>` whether a
``VALUE`` parameter has to be restored from the jCal type field.
parameter is given, so it tells
:meth:`Component.from_jcal <icalendar.cal.component.Component.from_jcal>`
whether a ``VALUE`` parameter has to be restored from the jCal type field.

Parameters:
name: Property name.
Expand All @@ -312,6 +323,7 @@ def default_value_type(self, name: str) -> str:
internal = self.types_map.get(name, "unknown").lower()
if internal in self._jcal_value_types:
return internal

# Internal keys such as ``categories`` or ``date-time-list`` are not
# jCal value types themselves; ask the value class for the type it
# actually serialises to.
Expand All @@ -321,14 +333,22 @@ def to_ical(self, name, value):
"""Encodes a named value from a primitive python type to an icalendar
encoded string.
"""
type_class = self.for_property(name)
name = name.upper()
if name in self.types_map:
type_class = self.for_property(name)
else:
type_class = self.for_parameter(name)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indicates that what I thought. I does the job but to make it clean, this function would also need splitting up. We can leave it here for now. Is this also used by the parameters?

return type_class(value).to_ical()

def from_ical(self, name, value):
"""Decodes a named property or parameter value from an icalendar
encoded string to a primitive python type.
"""
type_class = self.for_property(name)
name = name.upper()
if name in self.types_map:
type_class = self.for_property(name)
else:
type_class = self.for_parameter(name)
return type_class.from_ical(value)


Expand Down
27 changes: 26 additions & 1 deletion src/icalendar/tests/rfc_6321_xcal/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import pytest

from icalendar import Parameters
from icalendar import Event, Parameters
from icalendar.tests.rfc_6321_xcal.common import list2xml, to_xcal, xml2list


Expand Down Expand Up @@ -260,6 +260,31 @@ def test_parameters_to_xcal(name, value, xcal, message):
assert text[12:-13] == xcal, message


def test_parameter_name_matching_property_uses_parameter_type():
"""A parameter named after a property is not serialized as that property type.

RFC 6321 Section 5 specifies that unknown parameters are represented
using the ``unknown`` value type in xCal.
See https://datatracker.ietf.org/doc/html/rfc6321#section-5
"""
event = Event.from_ical(
"BEGIN:VEVENT\r\nSUMMARY;PRIORITY=hello:x\r\nEND:VEVENT\r\n"
)

xcal = event.to_xcal()

assert b"<priority><unknown>hello</unknown></priority>" in xcal
Comment thread
niccokunzmann marked this conversation as resolved.
Resolved


Comment thread
shekhar4704 marked this conversation as resolved.
Resolved
def test_property_name_matching_parameter_uses_property_type():
"""A property named after a parameter uses the property type."""
event = Event.from_ical("BEGIN:VEVENT\r\nTZID:hello\r\nEND:VEVENT\r\n")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tzid is in both using text.

If you use rsvp, then it should be text whereas before it was boolean.

Basically, this test needs to fail on main but work here and demonstrate that you solved the issue of them getting split up.


xcal = event.to_xcal()

assert b"<tzid><text>hello</text></tzid>" in xcal


@mark_parameters
def test_parameters_from_xcal(name, value, xcal, message):
"""Parse Parameters from xcal."""
Expand Down
Loading