microsoft / kiota-serialization-json-python

JSON serialization implementation for Kiota clients in Python
https://aka.ms/kiota/docs
MIT License
4 stars 12 forks source link

Failing to decode valid response #101

Closed jackdelahunt closed 1 year ago

jackdelahunt commented 1 year ago

Trimmed down response from the API I am using with a generated Python Kiota client:

[
    {
        "id": "...",
        "clientId": "...",
        "name": "service_account_name",
        "description": "",
        "createdBy": "user",
        "createdAt": 166223449
    },

   // and more...
]

When trying to serialize this and error is being thrown in the kiota-serialization-json-python package.

jsa/client/apis/service_accounts/v1/v1_request_builder.py:57: in get
    return await self.request_adapter.send_collection_async(request_info, service_account_data.ServiceAccountData, error_mapping)
../../Library/Caches/pypoetry/virtualenvs/jsa-B2IPpXSd-py3.11/lib/python3.11/site-packages/kiota_http/httpx_request_adapter.py:154: in send_collection_async
    result = root_node.get_collection_of_object_values(model_type)
../../Library/Caches/pypoetry/virtualenvs/jsa-B2IPpXSd-py3.11/lib/python3.11/site-packages/kiota_serialization_json/json_parse_node.py:182: in get_collection_of_object_values
    return list(
../../Library/Caches/pypoetry/virtualenvs/jsa-B2IPpXSd-py3.11/lib/python3.11/site-packages/kiota_serialization_json/json_parse_node.py:184: in <lambda>
    lambda x: JsonParseNode(x).get_object_value(factory),  # type: ignore
../../Library/Caches/pypoetry/virtualenvs/jsa-B2IPpXSd-py3.11/lib/python3.11/site-packages/kiota_serialization_json/json_parse_node.py:227: in get_object_value
    self._assign_field_values(result)
../../Library/Caches/pypoetry/virtualenvs/jsa-B2IPpXSd-py3.11/lib/python3.11/site-packages/kiota_serialization_json/json_parse_node.py:283: in _assign_field_values
    object_dict = json.loads(object_dict)
/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py:346: in loads
    return _default_decoder.decode(s)
/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py:337: in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <json.decoder.JSONDecoder object at 0x1059bb5d0>, s = '[', idx = 0

    def raw_decode(self, s, idx=0):
        """Decode a JSON document from ``s`` (a ``str`` beginning with
        a JSON document) and return a 2-tuple of the Python
        representation and the index in ``s`` where the document ended.

        This can be used to decode a JSON document from a string that may
        have extraneous data at the end.

        """
        try:
            obj, end = self.scan_once(s, idx)
        except StopIteration as err:
>           raise JSONDecodeError("Expecting value", s, err.value) from None
E           json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)
jackdelahunt commented 1 year ago

With some additional testing, I have found that there is no problem when returning a single object (I used a single instance instead of an array). So that issue is when the response is an array at the top level and not an object.

Wrapping the array in an object works fine but of course is not ideal.

samwelkanda commented 1 year ago

Closing as this was fixed in https://github.com/microsoft/kiota-serialization-json-python/pull/99