microsoft / kiota-python

Abstractions library for Kiota generated Python clients
https://aka.ms/kiota/docs
MIT License
14 stars 10 forks source link

Patching of custom security attribute is failing with error TypeError: Encountered an unknown type during serialization <class 'dict'> #357

Open cynepco3hahue opened 7 months ago

cynepco3hahue commented 7 months ago

Environment

Stack trace (if available)

[2024-03-10 14:25:30,457 microsoft/kiota-serialization-json-python#80] INFO: HTTP Request: GET https://graph.microsoft.com/v1.0/servicePrincipals?$top=1&$filter=startswith%28displayName%2C%20%27core-stg6-user-app%27%29&$count=true&$orderby=displayName "HTTP/2 200 OK"
[2024-03-10 14:25:31,855 microsoft/kiota-serialization-json-python#80] INFO: HTTP Request: GET https://graph.microsoft.com/v1.0/servicePrincipals/806a7c55-cbc0-4399-81f5-6df0a73c98a8?$select=customSecurityAttributes "HTTP/2 200 OK"
CustomSecurityAttributeValue(additional_data={'recoverbyok': {'@odata.type': '#microsoft.graph.customSecurityAttributeValue', 'readonly@odata.type': '#Collection(String)', 'readonly': ['stg6-a-9000632-927f4b', 'stg6-a-9000617-3d2e13', 'stg6-a-9000618-9d0710', 'stg6-a-9000620-501d69', 'stg6-a-9000621-3a48cb', 'stg6-a-9000638-ae6e32']}}, odata_type=None)
Patch dict CustomSecurityAttributeValue(additional_data={'recoverbyok': {'@odata.type': '#microsoft.graph.customSecurityAttributeValue', 'readonly@odata.type': '#Collection(String)', 'readonly': ['stg6-a-9000632-927f4b', 'stg6-a-9000617-3d2e13', 'stg6-a-9000618-9d0710', 'stg6-a-9000620-501d69', 'stg6-a-9000621-3a48cb', 'stg6-a-9000638-ae6e32', 'stg6-a-9000641-74553d']}}, odata_type=None)
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/cloud_accessors/common/exception/exception_handler.py", line 59, in azure_exception_handler
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/cloud_accessors/identity_access_manager/azure/identity_access_manager.py", line 111, in add_value_to_service_principal_custom_security_attribute
    self._patch_service_principal_custom_security_attributes(
  File "/usr/local/lib/python3.10/site-packages/cloud_accessors/identity_access_manager/azure/identity_access_manager.py", line 65, in _patch_service_principal_custom_security_attributes
    self._event_loop.run_until_complete(
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.10/site-packages/msgraph/generated/service_principals/item/service_principal_item_request_builder.py", line 108, in patch
    request_info = self.to_patch_request_information(
  File "/usr/local/lib/python3.10/site-packages/msgraph/generated/service_principals/item/service_principal_item_request_builder.py", line 173, in to_patch_request_information
    request_info.set_content_from_parsable(self.request_adapter, "application/json", body)
  File "/usr/local/lib/python3.10/site-packages/kiota_abstractions/request_information.py", line 179, in set_content_from_parsable
    writer.write_object_value(None, values)
  File "/usr/local/lib/python3.10/site-packages/kiota_serialization_json/json_serialization_writer.py", line 285, in write_object_value
    self._serialize_value(temp_writer, value)
  File "/usr/local/lib/python3.10/site-packages/kiota_serialization_json/json_serialization_writer.py", line 462, in _serialize_value
    value.serialize(temp_writer)
  File "/usr/local/lib/python3.10/site-packages/msgraph/generated/models/service_principal.py", line 289, in serialize
    writer.write_object_value("customSecurityAttributes", self.custom_security_attributes)
  File "/usr/local/lib/python3.10/site-packages/kiota_serialization_json/json_serialization_writer.py", line 285, in write_object_value
    self._serialize_value(temp_writer, value)
  File "/usr/local/lib/python3.10/site-packages/kiota_serialization_json/json_serialization_writer.py", line 462, in _serialize_value
    value.serialize(temp_writer)
  File "/usr/local/lib/python3.10/site-packages/msgraph/generated/models/custom_security_attribute_value.py", line 47, in serialize
    writer.write_additional_data_value(self.additional_data)
  File "/usr/local/lib/python3.10/site-packages/kiota_serialization_json/json_serialization_writer.py", line 330, in write_additional_data_value
    self.write_any_value(key, val)
  File "/usr/local/lib/python3.10/site-packages/kiota_serialization_json/json_serialization_writer.py", line 451, in write_any_value
    raise TypeError(
TypeError: Encountered an unknown type during serialization <class 'dict'>                     with key recoverbyok

Describe the bug Patching of custom security attribute is failing with above traceback The input for pathing is

CustomSecurityAttributeValue(additional_data={'recoverbyok': {'@odata.type': '#microsoft.graph.customSecurityAttributeValue', 'readonly@odata.type': '#Collection(String)', 'readonly': ['stg6-a-9000632-927f4b', 'stg6-a-9000617-3d2e13', 'stg6-a-9000618-9d0710', 'stg6-a-9000620-501d69', 'stg6-a-9000621-3a48cb', 'stg6-a-9000638-ae6e32', 'stg6-a-9000641-74553d']}}, odata_type=None)

To Reproduce Steps to reproduce the behavior:

Expected behavior It should work as it worked under version 1.0.1

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

osolmaz commented 6 months ago

Affected by this as well

MaxNad commented 3 months ago

From a rapid investigation, this is related to this kiota issue: https://github.com/microsoft/kiota-abstractions-python/issues/357

The serialize method of PlannerAssignments (and other similar classes like PlannerChecklistItems call write_additional_data_value which is the problematic function.

    def write_additional_data_value(self, value: Dict[str, Any]) -> None:
        """Writes the specified additional data to the stream.
        Args:
            value (Dict[str, Any]): he additional data to be written.
        """
        if isinstance(value, dict):
            for key, val in value.items():
                self.write_any_value(key, val)

That method takes the additional data, loops through the keys and sends them to write_any_value. In this case, the key would be the unique ID of the assigned person and the value would be a dict that contains the @odata.type and associated parameters.

However, the write_any_value doesn't have a isinstance(value, dict) check and therefore fails. Any call to write_additional_data_value would therefore cause this issue.

Even if we fix the parsing issue, we encounter another problem because the @odata_type attribute is never translated to @odata.type.