-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
[BUG][python] Support named arrays #6493
Changes from all commits
c43c170
7b109de
b8a1a37
31407d4
45e8dd6
ed9f53b
9b393c6
c1ee2a5
0b00e42
4a8ad25
a589599
e29055e
49eb08f
fcbd31a
ebc7996
1f890db
7a2366a
a5d3700
65a1c6d
a7dbe80
b2ca2f7
3132aca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,7 +270,7 @@ public void arrayModelTest() { | |
Assert.assertEquals(cm.classname, "sample.Sample"); | ||
Assert.assertEquals(cm.classVarName, "sample"); | ||
Assert.assertEquals(cm.description, "an array model"); | ||
Assert.assertEquals(cm.vars.size(), 0); | ||
Assert.assertEquals(cm.vars.size(), 1); // there is one value for Childer definition | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the dataType in this CodegenModel.dataType?
We can debug the CodegenModels and see the information in them by running the generate command with the How about in the classvars template here
We would also need to do a similar change in
Our model doc mustache template would need a similar change to grab the data from dataType rather than vars. @jirikuncar There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to keep the current code. It looks like too many changes and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, understood. That's fine. |
||
Assert.assertEquals(cm.parent, "list"); | ||
Assert.assertEquals(cm.imports.size(), 1); | ||
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("children.Children")).size(), 1); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# animal_farm.AnimalFarm | ||
|
||
## Properties | ||
Name | Type | Description | Notes | ||
------------ | ------------- | ------------- | ------------- | ||
**value** | [**[animal.Animal]**](Animal.md) | | | ||
|
||
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
# coding: utf-8 | ||
|
||
""" | ||
OpenAPI Petstore | ||
|
||
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501 | ||
|
||
The version of the OpenAPI document: 1.0.0 | ||
Generated by: https://openapi-generator.tech | ||
""" | ||
|
||
|
||
from __future__ import absolute_import | ||
import re # noqa: F401 | ||
import sys # noqa: F401 | ||
|
||
import six # noqa: F401 | ||
import nulltype # noqa: F401 | ||
|
||
from petstore_api.model_utils import ( # noqa: F401 | ||
ApiTypeError, | ||
ModelComposed, | ||
ModelNormal, | ||
ModelSimple, | ||
cached_property, | ||
change_keys_js_to_python, | ||
convert_js_args_to_python_args, | ||
date, | ||
datetime, | ||
file_type, | ||
int, | ||
none_type, | ||
str, | ||
validate_get_composed_info, | ||
) | ||
try: | ||
from petstore_api.model import animal | ||
except ImportError: | ||
animal = sys.modules[ | ||
'petstore_api.model.animal'] | ||
|
||
|
||
class AnimalFarm(ModelSimple): | ||
"""NOTE: This class is auto generated by OpenAPI Generator. | ||
Ref: https://openapi-generator.tech | ||
|
||
Do not edit the class manually. | ||
|
||
Attributes: | ||
allowed_values (dict): The key is the tuple path to the attribute | ||
and the for var_name this is (var_name,). The value is a dict | ||
with a capitalized key describing the allowed value and an allowed | ||
value. These dicts store the allowed enum values. | ||
validations (dict): The key is the tuple path to the attribute | ||
and the for var_name this is (var_name,). The value is a dict | ||
that stores validations for max_length, min_length, max_items, | ||
min_items, exclusive_maximum, inclusive_maximum, exclusive_minimum, | ||
inclusive_minimum, and regex. | ||
additional_properties_type (tuple): A tuple of classes accepted | ||
as additional properties values. | ||
""" | ||
|
||
allowed_values = { | ||
} | ||
|
||
validations = { | ||
} | ||
|
||
additional_properties_type = None | ||
|
||
_nullable = False | ||
|
||
@cached_property | ||
def openapi_types(): | ||
""" | ||
This must be a class method so a model may have properties that are | ||
of type self, this ensures that we don't create a cyclic import | ||
|
||
Returns | ||
openapi_types (dict): The key is attribute name | ||
and the value is attribute type. | ||
""" | ||
return { | ||
'value': ([animal.Animal],), # noqa: E501 | ||
} | ||
|
||
@cached_property | ||
def discriminator(): | ||
return None | ||
|
||
_composed_schemas = None | ||
|
||
required_properties = set([ | ||
'_data_store', | ||
'_check_type', | ||
'_spec_property_naming', | ||
'_path_to_item', | ||
'_configuration', | ||
'_visited_composed_classes', | ||
]) | ||
|
||
@convert_js_args_to_python_args | ||
def __init__(self, value, *args, **kwargs): # noqa: E501 | ||
"""animal_farm.AnimalFarm - a model defined in OpenAPI | ||
|
||
Args: | ||
value ([animal.Animal]): | ||
|
||
Keyword Args: | ||
_check_type (bool): if True, values for parameters in openapi_types | ||
will be type checked and a TypeError will be | ||
raised if the wrong type is input. | ||
Defaults to True | ||
_path_to_item (tuple/list): This is a list of keys or values to | ||
drill down to the model in received_data | ||
when deserializing a response | ||
_spec_property_naming (bool): True if the variable names in the input data | ||
are serialized names, as specified in the OpenAPI document. | ||
False if the variable names in the input data | ||
are pythonic names, e.g. snake case (default) | ||
_configuration (Configuration): the instance to use when | ||
deserializing a file_type parameter. | ||
If passed, type conversion is attempted | ||
If omitted no type conversion is done. | ||
_visited_composed_classes (tuple): This stores a tuple of | ||
classes that we have traveled through so that | ||
if we see that class again we will not use its | ||
discriminator again. | ||
When traveling through a discriminator, the | ||
composed schema that is | ||
is traveled through is added to this set. | ||
For example if Animal has a discriminator | ||
petType and we pass in "Dog", and the class Dog | ||
allOf includes Animal, we move through Animal | ||
once using the discriminator, and pick Dog. | ||
Then in Dog, we will make an instance of the | ||
Animal class but this time we won't travel | ||
through its discriminator because we passed in | ||
_visited_composed_classes = (Animal,) | ||
""" | ||
|
||
_check_type = kwargs.pop('_check_type', True) | ||
_spec_property_naming = kwargs.pop('_spec_property_naming', False) | ||
_path_to_item = kwargs.pop('_path_to_item', ()) | ||
_configuration = kwargs.pop('_configuration', None) | ||
_visited_composed_classes = kwargs.pop('_visited_composed_classes', ()) | ||
|
||
if args: | ||
raise ApiTypeError( | ||
"Invalid positional arguments=%s passed to %s. Remove those invalid positional arguments." % ( | ||
args, | ||
self.__class__.__name__, | ||
), | ||
path_to_item=_path_to_item, | ||
valid_classes=(self.__class__,), | ||
) | ||
|
||
self._data_store = {} | ||
self._check_type = _check_type | ||
self._spec_property_naming = _spec_property_naming | ||
self._path_to_item = _path_to_item | ||
self._configuration = _configuration | ||
self._visited_composed_classes = _visited_composed_classes + (self.__class__,) | ||
|
||
self.value = value | ||
for var_name, var_value in six.iteritems(kwargs): | ||
if var_name not in self.attribute_map and \ | ||
self._configuration is not None and \ | ||
self._configuration.discard_unknown_keys and \ | ||
self.additional_properties_type is None: | ||
# discard variable. | ||
continue | ||
setattr(self, var_name, var_value) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# coding: utf-8 | ||
|
||
""" | ||
OpenAPI Petstore | ||
|
||
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501 | ||
|
||
The version of the OpenAPI document: 1.0.0 | ||
Generated by: https://openapi-generator.tech | ||
""" | ||
|
||
|
||
from __future__ import absolute_import | ||
import sys | ||
import unittest | ||
|
||
import petstore_api | ||
try: | ||
from petstore_api.model import animal | ||
except ImportError: | ||
animal = sys.modules[ | ||
'petstore_api.model.animal'] | ||
from petstore_api.model.animal_farm import AnimalFarm | ||
|
||
|
||
class TestAnimalFarm(unittest.TestCase): | ||
"""AnimalFarm unit test stubs""" | ||
|
||
def setUp(self): | ||
pass | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
def testAnimalFarm(self): | ||
"""Test AnimalFarm""" | ||
# FIXME: construct object with mandatory attributes with example values | ||
# model = AnimalFarm() # noqa: E501 | ||
pass | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array models are making a model that aliases the array type, so I think that this should be true.
Long term my goal is to have the ModelSimple classes inherrit from their base classes like str int etc.
Why do we need to set this to False for ArrayModels? Historically generateAliasAsModel applied only to Map and Array models in most generators. In python-experimental we apply it to those AND all primitives that they have validations and enums. Standard processing in the generators was to describe all these primitive models as their simple types like str, list. But if those models had validations and that data was lost in that process. So I added code to preserve that information and make object type models which preserve the validation information which would have been lost. That code hoists the model information into a required
value
variable.After I did that the CodegenModel class was updated to include validation information which previously had been missing. So in summary this code here is an older hack to preserve information. If we can use the existing ArrayModels as is that is preferable. It should define its datatype somewhere inside the CodegenModel and that should be used to set the ModelSimple data type in the python class for our made up value parameter.
We can do this by doing a check of the model type when we assign the openapi_types in ModelSimple.
We can check the data that is available by running the generator with a debugmodels flag.
In my comment here I explain how to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not aware of all the corner cases in the code. I was trying to make it work for my use-case and not break existing tests. Feel free to propose code changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope I have found a sufficient fix in b2ca2f7.