Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5bc9540
Adds additionalproperties feature in python
spacether Feb 4, 2019
64d5444
Adds ensure-up-to-date updates
spacether Feb 4, 2019
ec73f56
Adds docstring description of the model argument inputs
spacether Feb 4, 2019
5cdfe51
Adds ensure up to date oepnapi3 python updates
spacether Feb 4, 2019
0a52027
Adds test fix
spacether Feb 4, 2019
8be21b0
Adds fix for Shippable, gives the additionalProperties CodegenParamet…
spacether Feb 4, 2019
ba7ea49
Adds function to set freeform types to string of all types
spacether Feb 7, 2019
88f8197
Adds postProcessAllModels function which processes and fixes dataType…
spacether Feb 7, 2019
0590d05
Adds models with additionalproperties of each type and model tests
spacether Feb 9, 2019
372ae60
Adds _check_type parameter to model, adds additionalproperties tests
spacether Feb 11, 2019
5b4f108
Creates utils module, adds additionalproperties map for serialization
spacether Feb 12, 2019
2e33cf1
Removes additional_properties_map, creates model_to_dict function to …
spacether Feb 13, 2019
983f570
Improves docstring for model_to_dict
spacether Feb 14, 2019
f218ed5
Adds class type definition in models
spacether Feb 17, 2019
7656756
Adds datetime and date type classes, adds general OpenApiException, r…
spacether Feb 18, 2019
7ac9285
Adds class imports to models, python generator uses dataType for docs…
spacether Feb 20, 2019
67dbb9c
Model discriminator now stores classes, adds __deserialize_model in a…
spacether Feb 22, 2019
9680dfd
Creates exceptions module, all deserialization tests in py2 except 1 …
spacether Feb 23, 2019
05fe1aa
Adds validate_and_convert_types, python 2.0 tests pass except for tes…
spacether Feb 25, 2019
e8f97fa
Adds anytype deserialization tests in python swagger client
spacether Feb 28, 2019
59b2d4c
Fixes typos
spacether Feb 28, 2019
e158601
Adds file deserialization test
spacether Mar 2, 2019
7d84ef2
Updates all v2 and v3 python samples
spacether Mar 3, 2019
b2446cf
Removes dubug flag, reverts readme, in python generator tweaks suppor…
spacether Mar 3, 2019
9e9b94e
Adds dict instantiationType back in
spacether Mar 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds models with additionalproperties of each type and model tests
  • Loading branch information
spacether committed Mar 2, 2019
commit 0590d057f8bf64ad2a08c0b68a641fbd1e6a35c7
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
List<Map<String, Object>> models = (List<Map<String, Object>>) inner.get("models");
for (Map<String, Object> mo : models) {
CodegenModel cm = (CodegenModel) mo.get("model");
cm.additionalProperties.dataType = getCodegenPropertyDataType(cm.additionalProperties);
if (cm.additionalProperties != null) {
cm.additionalProperties.dataType = getCodegenPropertyDataType(cm.additionalProperties);
}
for (CodegenProperty cp : cm.allVars) {
cp.dataType = getCodegenPropertyDataType(cp);
}
Expand All @@ -325,7 +327,7 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {

public String getCodegenPropertyDataType(CodegenProperty cp) {
if (cp.jsonSchema.equals("{\n \"type\" : \"object\",\n \"properties\" : { }\n}") ||
cp.jsonSchema.equals("{\n \"type\" : \"object\"\n}")) {
cp.jsonSchema.equals("{\n \"type\" : \"object\"\n}")) {
return "str|float|int|bool|list|dict";
} else if (cp.isMapContainer && cp.items != null) {
return "dict(str, " + getCodegenPropertyDataType(cp.items) + ")";
Expand Down
57 changes: 42 additions & 15 deletions modules/openapi-generator/src/main/resources/python/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ class {{classname}}(object):
{{name}} ({{dataType}}):{{#description}} {{description}}{{/description}}{{/defaultValue}}{{/requiredVars}}{{#requiredVars}}{{#defaultValue}}
{{name}} ({{dataType}}):{{#description}} {{description}}.{{/description}} defaults to {{{defaultValue}}}, must be one of [{{{defaultValue}}}]{{/defaultValue}}{{/requiredVars}}

{{#optionalVars}}{{^hasMore}} Keyword Args:{{/hasMore}}{{/optionalVars}}{{#optionalVars}}
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 wront type is input.
Defaults to False{{#optionalVars}}
{{name}} ({{dataType}}):{{#description}} {{description}}.{{/description}} [optional]{{#defaultValue}} if omitted the server will use the default value of {{{defaultValue}}}{{/defaultValue}}{{/optionalVars}}

""" # noqa: E501
"""

self._data_store = {}

self.discriminator = {{#discriminator}}'{{{discriminatorName}}}'{{/discriminator}}{{^discriminator}}None{{/discriminator}}
self._check_type = kwargs.get('_check_type') or False
{{#requiredVars}}
self.__setitem__('{{name}}', {{name}})
{{/requiredVars}}
Expand All @@ -83,8 +86,9 @@ class {{classname}}(object):
for child_key, child_value in six.iteritems(item):
child_key_types.add(self.recursive_type(child_key))
child_value_types.add(self.recursive_type(child_value))
if child_key_types != set(['str']):
raise ValueError('Invalid dict key type. All Openapi dict keys must be strings')
# only allow empty dicts or dicts with str keys
if child_key_types not in [set(['str']), set()]:
raise TypeError('Invalid dict key type. All Openapi dict keys must be strings')
child_value_types = '|'.join(sorted(list(child_value_types)))
return "dict(str, {0})".format(child_value_types)
elif item_type == list:
Expand All @@ -96,9 +100,35 @@ class {{classname}}(object):
else:
return type(item).__name__

def valid_type(self, passed_type_str, required_type_str):
"""Returns a boolean, True if passed_type is required_type"""
if passed_type_str == required_type_str:
return True
req_types, req_remainder = self.get_types_remainder(required_type_str)
passed_types, passed_remainder = self.get_types_remainder(passed_type_str)
if not passed_types.issubset(req_types):
return False
# passed_types is in req_types
if req_remainder == '':
return True
if (passed_types == set(['list']) and passed_remainder == '' and
all(char not in req_remainder for char in '([')):
# we have an empty list, and the inner required types are
# primitives like str, int etc, allow it
return True
return self.valid_type(passed_remainder, req_remainder)

def get_types_remainder(self, type_string):
container_types = [('dict(str, ', ')'), ('list[', ']')]
for type_prefix, type_suffix in container_types:
if type_string.startswith(type_prefix) and type_string.endswith(type_suffix):
return set([type_prefix[:4]]), type_string[len(type_prefix):-1]
type_set = set(type_string.split('|'))
return type_set, ''

def __setitem__(self, name, value):
check_type = False
if name in self.openapi_types:
check_type = self._check_type
required_type = self.openapi_types[name]
else:
{{#additionalProperties}}
Expand All @@ -113,14 +143,11 @@ class {{classname}}(object):
passed_type = self.recursive_type(value)
if type(name) != str:
raise TypeError('Variable name must be type string and %s was not' % name)
elif passed_type != required_type and check_type:
raise ValueError('Variable value must be type %s but you passed in %s' %
(required_type, passed_type))
elif check_type and not self.valid_type(passed_type, required_type):
raise TypeError('Variable value must be type %s but you passed in %s' %
(required_type, passed_type))

if name in self.openapi_types:
setattr(self, name, value)
else:
self._data_store[name] = value
self._data_store[name] = value

def __getitem__(self, name):
if name in self.openapi_types:
Expand Down Expand Up @@ -223,7 +250,7 @@ class {{classname}}(object):
{{/hasValidation}}
{{/isEnum}}

self._data_store['{{name}}'] = {{name}}
self.__setitem__('{{name}}', {{name}})

{{/allVars}}
{{#discriminator}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1437,14 +1437,50 @@ definitions:
property_anytype_3:
type: object
properties: {}
additionalProperties:
type: object
AdditionalPropertiesString:
type: object
properties:
name:
type: string
additionalProperties:
type: string
AdditionalPropertiesInteger:
type: object
properties:
name:
type: string
additionalProperties:
type: integer
AdditionalPropertiesNumber:
type: object
properties:
name:
type: string
additionalProperties:
type: number
AdditionalPropertiesBoolean:
type: object
properties:
name:
type: string
additionalProperties:
type: boolean
AdditionalPropertiesArray:
type: object
properties:
name:
type: string
additionalProperties:
type: array
items:
type: object
AdditionalPropertiesObject:
type: object
properties:
name:
type: string
additionalProperties:
type: object
MixedPropertiesAndAdditionalPropertiesClass:
type: object
properties:
Expand Down
6 changes: 6 additions & 0 deletions samples/client/petstore/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ Class | Method | HTTP request | Description

## Documentation For Models

- [AdditionalPropertiesArray](docs/AdditionalPropertiesArray.md)
- [AdditionalPropertiesBoolean](docs/AdditionalPropertiesBoolean.md)
- [AdditionalPropertiesClass](docs/AdditionalPropertiesClass.md)
- [AdditionalPropertiesInteger](docs/AdditionalPropertiesInteger.md)
- [AdditionalPropertiesNumber](docs/AdditionalPropertiesNumber.md)
- [AdditionalPropertiesObject](docs/AdditionalPropertiesObject.md)
- [AdditionalPropertiesString](docs/AdditionalPropertiesString.md)
- [Animal](docs/Animal.md)
- [ApiResponse](docs/ApiResponse.md)
- [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md)
Expand Down
10 changes: 10 additions & 0 deletions samples/client/petstore/python/docs/AdditionalPropertiesArray.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# AdditionalPropertiesArray

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **str** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


10 changes: 10 additions & 0 deletions samples/client/petstore/python/docs/AdditionalPropertiesBoolean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# AdditionalPropertiesBoolean

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **str** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


17 changes: 11 additions & 6 deletions samples/client/petstore/python/docs/AdditionalPropertiesClass.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**map_property** | **dict(str, str)** | | [optional]
**map_of_map_property** | **dict(str, dict(str, str))** | | [optional]
**any_values_1** | [**object**](.md) | | [optional]
**any_values_2** | [**object**](.md) | | [optional]
**any_values_3** | [**object**](.md) | | [optional]
**map_with_any_values** | [**object**](.md) | | [optional]
**map_string** | **dict(str, str)** | | [optional]
**map_number** | **dict(str, float)** | | [optional]
**map_integer** | **dict(str, int)** | | [optional]
**map_boolean** | **dict(str, bool)** | | [optional]
**map_array_integer** | **dict(str, list[int])** | | [optional]
**map_array_anytype** | **dict(str, list[object])** | | [optional]
**map_map_string** | **dict(str, dict(str, str))** | | [optional]
**map_anytype** | **dict(str, dict(str, object))** | | [optional]
**property_anytype_1** | [**object**](.md) | | [optional]
**property_anytype_2** | [**object**](.md) | | [optional]
**property_anytype_3** | [**object**](.md) | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
10 changes: 10 additions & 0 deletions samples/client/petstore/python/docs/AdditionalPropertiesInteger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# AdditionalPropertiesInteger

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **str** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


10 changes: 10 additions & 0 deletions samples/client/petstore/python/docs/AdditionalPropertiesNumber.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# AdditionalPropertiesNumber

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **str** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


10 changes: 10 additions & 0 deletions samples/client/petstore/python/docs/AdditionalPropertiesObject.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# AdditionalPropertiesObject

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **str** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


10 changes: 10 additions & 0 deletions samples/client/petstore/python/docs/AdditionalPropertiesString.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# AdditionalPropertiesString

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **str** | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


6 changes: 6 additions & 0 deletions samples/client/petstore/python/petstore_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
from petstore_api.api_client import ApiClient
from petstore_api.configuration import Configuration
# import models into sdk package
from petstore_api.models.additional_properties_array import AdditionalPropertiesArray
from petstore_api.models.additional_properties_boolean import AdditionalPropertiesBoolean
from petstore_api.models.additional_properties_class import AdditionalPropertiesClass
from petstore_api.models.additional_properties_integer import AdditionalPropertiesInteger
from petstore_api.models.additional_properties_number import AdditionalPropertiesNumber
from petstore_api.models.additional_properties_object import AdditionalPropertiesObject
from petstore_api.models.additional_properties_string import AdditionalPropertiesString
from petstore_api.models.animal import Animal
from petstore_api.models.api_response import ApiResponse
from petstore_api.models.array_of_array_of_number_only import ArrayOfArrayOfNumberOnly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
from __future__ import absolute_import

# import models into model package
from petstore_api.models.additional_properties_array import AdditionalPropertiesArray
from petstore_api.models.additional_properties_boolean import AdditionalPropertiesBoolean
from petstore_api.models.additional_properties_class import AdditionalPropertiesClass
from petstore_api.models.additional_properties_integer import AdditionalPropertiesInteger
from petstore_api.models.additional_properties_number import AdditionalPropertiesNumber
from petstore_api.models.additional_properties_object import AdditionalPropertiesObject
from petstore_api.models.additional_properties_string import AdditionalPropertiesString
from petstore_api.models.animal import Animal
from petstore_api.models.api_response import ApiResponse
from petstore_api.models.array_of_array_of_number_only import ArrayOfArrayOfNumberOnly
Expand Down
Loading