Skip to content
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

Incorrect primitive_type in generated code causes "Encountered an unknown type during deserialization str" #5417

Open
jimisola opened this issue Sep 14, 2024 · 2 comments
Labels
help wanted Issue caused by core project dependency modules or library Needs: Attention 👋 Python type:bug A broken experience
Milestone

Comments

@jimisola
Copy link

jimisola commented Sep 14, 2024

What are you generating using Kiota, clients or plugins?

API Client/SDK

In what context or format are you using Kiota?

Linux executable

Client library/SDK language

Python

Describe the bug

When I use the versions below of Kiota the following error occurs:

Exception: Encountered an unknown type during deserialization str

I think that the bug is due to send_collection_of_primitive_async passing argument primitive_type as str instead of <class type[str]> and located here: https://github.com/microsoft/kiota/blob/main/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs#L808

"microsoft-kiota-abstractions==1.3.3",
"microsoft-kiota-http==1.3.3",
"microsoft-kiota-serialization-json==1.3.2",
"microsoft-kiota-serialization-text==1.0.0",
"microsoft-kiota-serialization-form==0.1.1",
"microsoft-kiota-serialization-multipart==0.1.0",

Expected behavior

The get method should return a list of strings (names).

How to reproduce

Have Kiota generate a python code for an endpoint that returns a JSON collection of string (python equivalent: List[str]).

Open API description file

        "/names": {
            "get": {
                "summary": "Get names",
                "operationId": "getNames",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "uniqueItems": true,
                                    "type": "array",
                                    "items": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    },

Kiota Version

1.18.0+5c6b5d0ef23865ba2f9d9f0b9fe4b944cf26b1ec

Latest Kiota version known to work for scenario above?(Not required)

No response

Known Workarounds

Manually edited the generated code and replace "str" with str.

Configuration

  • OS: Ubuntu

Can't see that it is specific to this configuration.

Debug output

Other information

The generated get method for the endpoint seems to be incorrect ("str" instead of str):

return await self.request_adapter.send_collection_of_primitive_async(request_info, "str", None)

Which will cause the if generic_type in primitive_types: in JsonParseNode.get_collection_of_primitive_values to be False

since "str" is NOT in primitive_types due it being an actual string (and not str, i.e. a ).

Example:

>>> types = {bool, str, int, float, bytes}
>>> type(types)
<class 'set'>
>>> str in types
True 
>>> 'str' in types
False
    def get_collection_of_primitive_values(self, primitive_type: Any) -> Optional[List[T]]:
        """Gets the collection of primitive values of the node
        Returns:
            List[T]: The collection of primitive values
        """

        primitive_types = {bool, str, int, float, UUID, datetime, timedelta, date, time, bytes}

        def func(item):
            generic_type = primitive_type if primitive_type else type(item)
            current_parse_node = self._create_new_node(item)
            if generic_type in primitive_types:
                method = getattr(current_parse_node, f'get_{generic_type.__name__.lower()}_value')
                return method()
            raise Exception(f"Encountered an unknown type during deserialization {generic_type}")

        if isinstance(self._json_node, str):
            return list(map(func, json.loads(self._json_node)))
        return list(map(func, list(self._json_node)))

@jimisola jimisola added status:waiting-for-triage An issue that is yet to be reviewed or assigned type:bug A broken experience labels Sep 14, 2024
@msgraph-bot msgraph-bot bot added the Python label Sep 14, 2024
@baywet
Copy link
Member

baywet commented Sep 16, 2024

Hi @jimisola
Thank you for using kiota and for reaching out.
Looking at other examples your analysis seems to be correct.
Is this something you'd like to submit a pull request for provided some guidance?

@baywet baywet added help wanted Issue caused by core project dependency modules or library status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed status:waiting-for-triage An issue that is yet to be reviewed or assigned labels Sep 16, 2024
@baywet baywet added this to the Kiota v1.19 milestone Sep 16, 2024
@jimisola
Copy link
Author

Hi @baywet,

Great that it is confirmed. It surprised my because I thought this code would be used often.

I don't develop in C# and I'm active in other OSS projects, so I'll have to decline this one.

Our workaround now is to change it in the generated code.

I'll be happy to confirm that it works if needed.

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issue caused by core project dependency modules or library Needs: Attention 👋 Python type:bug A broken experience
Projects
Status: Todo 📃
Development

No branches or pull requests

2 participants