Repository navigation
Fix parameter type lookup during xCal serialization #2038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f8f8472
094ff51
fec8c46
9666582
16d10b3
6d57605
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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", | ||
|
|
@@ -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 | ||
| "label": "text", | ||
| "linkrel": "text", | ||
| "gap": "duration", | ||
| } | ||
|
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. | ||
|
|
||
|
|
@@ -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": | ||
|
|
@@ -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 | ||
|
|
@@ -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] | ||
|
|
||
|
|
@@ -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")] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
@@ -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. | ||
|
|
@@ -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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
|
|
||
|
|
@@ -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 | ||
|
niccokunzmann marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
|
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") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.""" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That comment is useful.
There was a problem hiding this comment.
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?