Skip to content

Commit 336521b

Browse files
Slavek Kabrdaspacether
andauthored
[Python-experimental] Don't mandate passing in required value as argument (#7424)
* [Python-experimental] Don't mandate passing in required value as argument * Remove usage of have_value * Address additional review comments * Fix tests according to the code change * Does not set None as value initial value, removes hasRequired tag usage, updates docstring Co-authored-by: Justin Black <justin.a.black@gmail.com>
1 parent b8de51f commit 336521b

21 files changed

Lines changed: 237 additions & 43 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientExperimentalCodegen.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,11 +713,11 @@ public CodegenModel fromModel(String name, Schema sc) {
713713
// 1. no default exists
714714
// schema does not contain default
715715
// cm.defaultValue unset, cm.hasRequired = true
716-
// 2. server has a default
716+
// 2. spec has a default
717717
// schema contains default
718-
// cm.defaultValue set, cm.hasRequired = true
718+
// cm.defaultValue set, cm.hasRequired = false
719719
// different value here to differentiate between use case 3 below
720-
// This defaultValue is used in the client docs only and is not sent to the server
720+
// This defaultValue is used when a consumer (client or server) lacks the input argument, defaultValue will be used
721721
// 3. only one value is allowed in an enum
722722
// schema does not contain default
723723
// cm.defaultValue set, cm.hasRequired = false
@@ -728,7 +728,7 @@ public CodegenModel fromModel(String name, Schema sc) {
728728
cm.hasRequired = true;
729729
} else if (sc.getDefault() != null) {
730730
cm.defaultValue = defaultValue;
731-
cm.hasRequired = true;
731+
cm.hasRequired = false;
732732
} else if (defaultValue != null && cm.defaultValue == null) {
733733
cm.defaultValue = defaultValue;
734734
cm.hasRequired = false;

modules/openapi-generator/src/main/resources/python/python-experimental/model_templates/method_init_simple.mustache

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,37 @@
88
])
99

1010
@convert_js_args_to_python_args
11-
def __init__(self{{#hasRequired}}, value{{/hasRequired}}, *args, **kwargs):
11+
def __init__(self, *args, **kwargs):
1212
"""{{classname}} - a model defined in OpenAPI
1313

14-
{{#hasRequired}}
14+
Note that value can be passed either in args or in kwargs, but not in both.
15+
1516
Args:
16-
value ({{{dataType}}}):{{#description}} {{description}}.{{/description}}{{#defaultValue}} if omitted the server will use the default value of {{{defaultValue}}}{{/defaultValue}}{{#allowableValues}}, must be one of [{{#enumVars}}{{{value}}}, {{/enumVars}}]{{/allowableValues}} # noqa: E501
17+
args[0] ({{{dataType}}}):{{#description}} {{description}}.{{/description}}{{#defaultValue}} if omitted defaults to {{{defaultValue}}}{{/defaultValue}}{{#allowableValues}}, must be one of [{{#enumVars}}{{{value}}}, {{/enumVars}}]{{/allowableValues}} # noqa: E501
1718

18-
{{/hasRequired}}
1919
Keyword Args:
20-
{{^hasRequired}}
21-
value ({{{dataType}}}):{{#description}} {{description}}.{{/description}}{{#defaultValue}} defaults to {{{defaultValue}}}{{/defaultValue}}{{#allowableValues}}, must be one of [{{#enumVars}}{{{value}}}, {{/enumVars}}]{{/allowableValues}} # noqa: E501
22-
{{/hasRequired}}
20+
value ({{{dataType}}}):{{#description}} {{description}}.{{/description}}{{#defaultValue}} if omitted defaults to {{{defaultValue}}}{{/defaultValue}}{{#allowableValues}}, must be one of [{{#enumVars}}{{{value}}}, {{/enumVars}}]{{/allowableValues}} # noqa: E501
2321
{{> python-experimental/model_templates/docstring_init_required_kwargs }}
2422
"""
2523

26-
{{^hasRequired}}
2724
if 'value' in kwargs:
2825
value = kwargs.pop('value')
2926
elif args:
3027
args = list(args)
3128
value = args.pop(0)
29+
{{#defaultValue}}
3230
else:
3331
value = {{{defaultValue}}}
34-
{{/hasRequired}}
32+
{{/defaultValue}}
33+
{{^defaultValue}}
34+
else:
35+
raise ApiTypeError(
36+
"value is required, but not passed in args or kwargs and doesn't have default",
37+
path_to_item=_path_to_item,
38+
valid_classes=(self.__class__,),
39+
)
40+
{{/defaultValue}}
41+
3542
_check_type = kwargs.pop('_check_type', True)
3643
_spec_property_naming = kwargs.pop('_spec_property_naming', False)
3744
_path_to_item = kwargs.pop('_path_to_item', ())

modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientExperimentalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ public void defaultSettingInPrimitiveModelWithValidations() {
375375

376376
final CodegenModel hasDefaultModel = codegen.fromModel("hasDefaultModel", hasDefault);
377377
Assert.assertEquals(hasDefaultModel.defaultValue, "15.0");
378-
Assert.assertEquals(hasDefaultModel.hasRequired, true);
378+
Assert.assertEquals(hasDefaultModel.hasRequired, false);
379379

380380
final CodegenModel noDefaultEumLengthOneModel = codegen.fromModel("noDefaultEumLengthOneModel", noDefaultEumLengthOne);
381381
Assert.assertEquals(noDefaultEumLengthOneModel.defaultValue, "15.0");

samples/client/petstore/python-experimental/docs/EnumClass.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Properties
44
Name | Type | Description | Notes
55
------------ | ------------- | ------------- | -------------
6-
**value** | **str** | | if omitted the server will use the default value of "-efg", must be one of ["_abc", "-efg", "(xyz)", ]
6+
**value** | **str** | | defaults to "-efg", must be one of ["_abc", "-efg", "(xyz)", ]
77

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

samples/client/petstore/python-experimental/petstore_api/model/animal_farm.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,16 @@ def discriminator():
9999
])
100100

101101
@convert_js_args_to_python_args
102-
def __init__(self, value, *args, **kwargs):
102+
def __init__(self, *args, **kwargs):
103103
"""AnimalFarm - a model defined in OpenAPI
104104
105+
Note that value can be passed either in args or in kwargs, but not in both.
106+
105107
Args:
106-
value ([Animal]): # noqa: E501
108+
args[0] ([Animal]): # noqa: E501
107109
108110
Keyword Args:
111+
value ([Animal]): # noqa: E501
109112
_check_type (bool): if True, values for parameters in openapi_types
110113
will be type checked and a TypeError will be
111114
raised if the wrong type is input.
@@ -138,6 +141,18 @@ def __init__(self, value, *args, **kwargs):
138141
_visited_composed_classes = (Animal,)
139142
"""
140143

144+
if 'value' in kwargs:
145+
value = kwargs.pop('value')
146+
elif args:
147+
args = list(args)
148+
value = args.pop(0)
149+
else:
150+
raise ApiTypeError(
151+
"value is required, but not passed in args or kwargs and doesn't have default",
152+
path_to_item=_path_to_item,
153+
valid_classes=(self.__class__,),
154+
)
155+
141156
_check_type = kwargs.pop('_check_type', True)
142157
_spec_property_naming = kwargs.pop('_spec_property_naming', False)
143158
_path_to_item = kwargs.pop('_path_to_item', ())

samples/client/petstore/python-experimental/petstore_api/model/enum_class.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,16 @@ def discriminator():
9999
])
100100

101101
@convert_js_args_to_python_args
102-
def __init__(self, value, *args, **kwargs):
102+
def __init__(self, *args, **kwargs):
103103
"""EnumClass - a model defined in OpenAPI
104104
105+
Note that value can be passed either in args or in kwargs, but not in both.
106+
105107
Args:
106-
value (str): if omitted the server will use the default value of "-efg", must be one of ["_abc", "-efg", "(xyz)", ] # noqa: E501
108+
args[0] (str): if omitted defaults to "-efg", must be one of ["_abc", "-efg", "(xyz)", ] # noqa: E501
107109
108110
Keyword Args:
111+
value (str): if omitted defaults to "-efg", must be one of ["_abc", "-efg", "(xyz)", ] # noqa: E501
109112
_check_type (bool): if True, values for parameters in openapi_types
110113
will be type checked and a TypeError will be
111114
raised if the wrong type is input.
@@ -138,6 +141,14 @@ def __init__(self, value, *args, **kwargs):
138141
_visited_composed_classes = (Animal,)
139142
"""
140143

144+
if 'value' in kwargs:
145+
value = kwargs.pop('value')
146+
elif args:
147+
args = list(args)
148+
value = args.pop(0)
149+
else:
150+
value = "-efg"
151+
141152
_check_type = kwargs.pop('_check_type', True)
142153
_spec_property_naming = kwargs.pop('_spec_property_naming', False)
143154
_path_to_item = kwargs.pop('_path_to_item', ())

samples/client/petstore/python-experimental/petstore_api/model/number_with_validations.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,16 @@ def discriminator():
9898
])
9999

100100
@convert_js_args_to_python_args
101-
def __init__(self, value, *args, **kwargs):
101+
def __init__(self, *args, **kwargs):
102102
"""NumberWithValidations - a model defined in OpenAPI
103103
104+
Note that value can be passed either in args or in kwargs, but not in both.
105+
104106
Args:
105-
value (float): # noqa: E501
107+
args[0] (float): # noqa: E501
106108
107109
Keyword Args:
110+
value (float): # noqa: E501
108111
_check_type (bool): if True, values for parameters in openapi_types
109112
will be type checked and a TypeError will be
110113
raised if the wrong type is input.
@@ -137,6 +140,18 @@ def __init__(self, value, *args, **kwargs):
137140
_visited_composed_classes = (Animal,)
138141
"""
139142

143+
if 'value' in kwargs:
144+
value = kwargs.pop('value')
145+
elif args:
146+
args = list(args)
147+
value = args.pop(0)
148+
else:
149+
raise ApiTypeError(
150+
"value is required, but not passed in args or kwargs and doesn't have default",
151+
path_to_item=_path_to_item,
152+
valid_classes=(self.__class__,),
153+
)
154+
140155
_check_type = kwargs.pop('_check_type', True)
141156
_spec_property_naming = kwargs.pop('_spec_property_naming', False)
142157
_path_to_item = kwargs.pop('_path_to_item', ())

samples/client/petstore/python-experimental/petstore_api/model/string_enum.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,16 @@ def discriminator():
9999
])
100100

101101
@convert_js_args_to_python_args
102-
def __init__(self, value, *args, **kwargs):
102+
def __init__(self, *args, **kwargs):
103103
"""StringEnum - a model defined in OpenAPI
104104
105+
Note that value can be passed either in args or in kwargs, but not in both.
106+
105107
Args:
106-
value (str):, must be one of ["placed", "approved", "delivered", ] # noqa: E501
108+
args[0] (str):, must be one of ["placed", "approved", "delivered", ] # noqa: E501
107109
108110
Keyword Args:
111+
value (str):, must be one of ["placed", "approved", "delivered", ] # noqa: E501
109112
_check_type (bool): if True, values for parameters in openapi_types
110113
will be type checked and a TypeError will be
111114
raised if the wrong type is input.
@@ -138,6 +141,18 @@ def __init__(self, value, *args, **kwargs):
138141
_visited_composed_classes = (Animal,)
139142
"""
140143

144+
if 'value' in kwargs:
145+
value = kwargs.pop('value')
146+
elif args:
147+
args = list(args)
148+
value = args.pop(0)
149+
else:
150+
raise ApiTypeError(
151+
"value is required, but not passed in args or kwargs and doesn't have default",
152+
path_to_item=_path_to_item,
153+
valid_classes=(self.__class__,),
154+
)
155+
141156
_check_type = kwargs.pop('_check_type', True)
142157
_spec_property_naming = kwargs.pop('_spec_property_naming', False)
143158
_path_to_item = kwargs.pop('_path_to_item', ())

samples/openapi3/client/petstore/python-experimental/docs/EnumClass.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Properties
44
Name | Type | Description | Notes
55
------------ | ------------- | ------------- | -------------
6-
**value** | **str** | | if omitted the server will use the default value of "-efg", must be one of ["_abc", "-efg", "(xyz)", ]
6+
**value** | **str** | | defaults to "-efg", must be one of ["_abc", "-efg", "(xyz)", ]
77

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

samples/openapi3/client/petstore/python-experimental/docs/IntegerEnumWithDefaultValue.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Properties
44
Name | Type | Description | Notes
55
------------ | ------------- | ------------- | -------------
6-
**value** | **int** | | if omitted the server will use the default value of 0, must be one of [0, 1, 2, ]
6+
**value** | **int** | | defaults to 0, must be one of [0, 1, 2, ]
77

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

0 commit comments

Comments
 (0)