[PYTHON] GetItem not working for Client generated allOf model and broken since 5.2.0 (#12239)

* I feel the issue is due to the creation of self._var_name_to_model_instances while doing the deserialization of the data.

Earlier the Python SDK code was using get_var_name_to_model_instances function which was adding var name to model instances that contain it. So <class 'openapi_client.model.stream_options_all_of'> will not part of mapping in self._var_name_to_model_instances for variable name stream_options.

Now with the latest Python SDK code following is the way through which var_name_to_model_instances is created:

    for prop_name in model_args:
        if prop_name not in discarded_args:
            var_name_to_model_instances[prop_name] = [self] + composed_instances
Now as we can see that the var_name_to_model_instances is populated with self and composed_instance which will also contain stream_options_all_of as a composed instance and there will be no check that if stream_options is present in composed_instances or not.

As there is no attribute_mapping found for stream_options in stream_options_all_of, the type for stream_options will be treated as dict for mapping stream_options_all_of as mentioned by @Chekov2k.

So what I suggest is the following code:

    for prop_name in model_args:
        if prop_name not in discarded_args:
           var_name_to_model_instances[prop_name] = [self] + list(
                filter(
                    lambda x: prop_name in x.openapi_types, composed_instances))
This way we can check if the property name is present in that composed instance or not. If it's okay for @spacether I can raise a PR for this.

* [get_item_all_of_bug]
Added samples, test cases to validate all_of schema.

* [getiem_all_of_bug]

Updated docs and samples.

* [getiem_all_of_bug]

Updated test cases, docs and samples.
This commit is contained in:
Akhil Nair
2022-05-28 09:39:48 +05:30
committed by GitHub
parent 76eddeb713
commit e823290c8f
29 changed files with 1646 additions and 20 deletions

View File

@@ -747,5 +747,39 @@ class TestFakeApi(unittest.TestCase):
assert isinstance(response, GmFruitNoProperties)
assert model_to_dict(response) == expected_json_body
def test_post_tx_rx_all_of_payload(self):
"""Test case for postInlineAdditionlPropertiesPayload
"""
from petstore_api.model.stream_options import StreamOptions
from petstore_api.model.publish_options_publish import PublishOptionsPublish
endpoint = self.api.tx_rx_all_of_model_endpoint
assert endpoint.openapi_types['stream_options'] == (StreamOptions,)
assert endpoint.settings['response_type'] == (StreamOptions,)
# serialization + deserialization works
from petstore_api.rest import RESTClientObject, RESTResponse
with patch.object(RESTClientObject, 'request') as mock_method:
expected_json_body = {
"egressThresholds": {
"person":0.8
},
"publish": {
"egressUnknownDetections": False
}
}
stream_option_instance = StreamOptions(**expected_json_body)
mock_method.return_value = self.mock_response(expected_json_body)
response = self.api.tx_rx_all_of_model(stream_options=stream_option_instance)
self.assert_request_called_with(
mock_method,
'http://petstore.swagger.io:80/v2/fake/TxRxAllOfModel',
body=expected_json_body
)
assert isinstance(response, StreamOptions)
assert model_to_dict(response) == expected_json_body
assert response.publish == PublishOptionsPublish(**{"egress_unknown_detections": False})
if __name__ == '__main__':
unittest.main()

View File

@@ -147,7 +147,7 @@ class TestFruit(unittest.TestCase):
self.assertEqual(
fruit._var_name_to_model_instances,
{
'color': [fruit, banana_instance],
'color': [fruit],
'length_cm': [fruit, banana_instance],
}
)
@@ -206,7 +206,7 @@ class TestFruit(unittest.TestCase):
self.assertEqual(
fruit._var_name_to_model_instances,
{
'color': [fruit, apple_instance],
'color': [fruit],
'cultivar': [fruit, apple_instance],
}
)

View File

@@ -110,7 +110,7 @@ class TestGmFruit(unittest.TestCase):
self.assertEqual(
fruit._var_name_to_model_instances,
{
'color': [fruit, banana_instance],
'color': [fruit],
'length_cm': [fruit, banana_instance],
}
)
@@ -153,9 +153,9 @@ class TestGmFruit(unittest.TestCase):
self.assertEqual(
fruit._var_name_to_model_instances,
{
'color': [fruit, apple_instance, banana_instance],
'length_cm': [fruit, apple_instance, banana_instance],
'cultivar': [fruit, apple_instance, banana_instance],
'color': [fruit],
'length_cm': [fruit, banana_instance],
'cultivar': [fruit, apple_instance],
}
)
self.assertEqual(
@@ -204,7 +204,7 @@ class TestGmFruit(unittest.TestCase):
self.assertEqual(
fruit._var_name_to_model_instances,
{
'color': [fruit, apple_instance],
'color': [fruit],
'cultivar': [fruit, apple_instance],
'origin': [fruit, apple_instance],
}