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

Error with response_type on Python client #10129

Closed
juanpaps03 opened this issue Mar 24, 2020 · 5 comments
Closed

Error with response_type on Python client #10129

juanpaps03 opened this issue Mar 24, 2020 · 5 comments

Comments

@juanpaps03
Copy link

juanpaps03 commented Mar 24, 2020

Generating Python client with codegen, the code has an error in the default_api.py file.

In runtime I'm sending data with a client function, the error that I get is:

AttributeError: module 'inbound_manager_client.models' has no attribute 'Object'

manually checking the code the error is in api_client.py/__deserialize function:

            # convert str to class
            if klass in self.NATIVE_TYPES_MAPPING:
                klass = self.NATIVE_TYPES_MAPPING[klass]
            else:
                klass = getattr(inbound_manager_client.models, klass)

The error raise in the last line of the code shared above, the 'klass' variable value is "Object" (with uppercase O). I think that the root of the problem is on:

        return self.api_client.call_api(
            "/patient",
            "POST",
            path_params,
            query_params,
            header_params,
            body=body_params,
            post_params=form_params,
            files=local_var_files,
            response_type="Object",  # noqa: E501
            auth_settings=auth_settings,
            async_req=params.get("async_req"),
            _return_http_data_only=params.get("_return_http_data_only"),
            _preload_content=params.get("_preload_content", True),
            _request_timeout=params.get("_request_timeout"),
            collection_formats=collection_formats,
        )

response_type is where the klass variable is being settled.
This is part of a function generated based on an API endpoint, the function name is:

    def add_patient_patient_post_with_http_info(self, body, **kwargs):  # noqa: E501

the endpoint function in the original python client is add_patient.

I change the reponse_type manually to test if the error is actually there, and if I change it for "object" or for a valid model class name, it works like a charm.

I was looking how this response_type is being generated in order to make any necessary change in the openapi.json used to generate this, but I didn't found the proper value to set on openapi.

From my point of view, this could be a bug in codegen, but maybe is something wrong with my openapi.json, if someone can guide me to make the changes on the openapi, it would be nice, but if this is a bug on codegen, I can't do more than wait

swagger-cogen version: 3.0.18

Command line used for generation

On linux:
java -jar swagger-codegen-cli.jar generate -i http://localhost:8000/openapi.json -l python -c swagger-config-inbound-manager-client.json
On mac:
swagger-codegen generate -i http://127.0.0.1:8000/openapi.json -l python -c swagger-config-inbound-manager-client.json
(same problem with both versions)

Openapi.json file link

https://gist.github.com/juanpaps03/21fac4119749f6668dd5f7797cbb4c08

@cole-floodbase
Copy link

cole-floodbase commented Feb 11, 2021

Happens for me too.

This is the proximate cause of the bug:

# convert str to class
if klass in self.NATIVE_TYPES_MAPPING:
klass = self.NATIVE_TYPES_MAPPING[klass]
else:
klass = getattr({{modelPackage}}, klass)

I was able to fix it the same way Juan mentioned, treating "Object" as "object".

            # convert str to class
            if klass in self.NATIVE_TYPES_MAPPING:
                klass = self.NATIVE_TYPES_MAPPING[klass]
            elif hasattr(arturo_client.models, klass):
                klass = getattr(arturo_client.models, klass)
            elif klass == "Object":
                klass = self.NATIVE_TYPES_MAPPING["object"]

I'm not sure if this special case is the right fix or if it's part of a bigger bug, though.

@flozi00
Copy link

flozi00 commented Mar 30, 2021

working with 3.0.25 and still getting this error
fixed by making it lowercase

def __deserialize(self, data, klass):
        """Deserializes dict, list, str into an object.

        :param data: dict, list or str.
        :param klass: class literal, or string of class name.

        :return: object.
        """
        if data is None:
            return None

        if type(klass) == str:
            if klass.startswith('list['):
                sub_kls = re.match(r'list\[(.*)\]', klass).group(1)
                return [self.__deserialize(sub_data, sub_kls)
                        for sub_data in data]

            if klass.startswith('dict('):
                sub_kls = re.match(r'dict\(([^,]*), (.*)\)', klass).group(2)
                return {k: self.__deserialize(v, sub_kls)
                        for k, v in six.iteritems(data)}

            # convert str to class
            klass = klass.lower()
            if klass in self.NATIVE_TYPES_MAPPING:
                klass = self.NATIVE_TYPES_MAPPING[klass]
            else:
                klass = getattr(paiskills.models, klass)

        if klass in self.PRIMITIVE_TYPES:
            return self.__deserialize_primitive(data, klass)
        elif klass == object:
            return self.__deserialize_object(data)
        elif klass == datetime.date:
            return self.__deserialize_date(data)
        elif klass == datetime.datetime:
            return self.__deserialize_datatime(data)
        else:
            return self.__deserialize_model(data, klass)

did this in generated code, not sure where it needs to be in this repo

@bkelley17
Copy link

bkelley17 commented Jul 6, 2021

edit: PythonClientCodegen gets a null in toModelName and returns "Object" instead of "object". I'm making a PR for this fix.

@bkelley17
Copy link

The above commit should resolve this.

@HugoMario
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants