22
22
stringProp: {"type": "string", "default": "a"}
23
23
numberProp: {"type": "number", "default": 1.5}
24
24
intProp: {"type": "integer", "default": 2}
25
- noneProp: {"type": "null", "default": null}
25
+ dateProp: {"type": "string", "format": "date", "default": "2024-01-02"}
26
+ dateTimeProp: {"type": "string", "format": "date-time", "default": "2024-01-02T03:04:05Z"}
27
+ uuidProp: {"type": "string", "format": "uuid", "default": "07EF8B4D-AA09-4FFA-898D-C710796AFF41"}
26
28
anyPropWithString: {"default": "b"}
27
29
anyPropWithInt: {"default": 3}
28
30
booleanWithStringTrue1: {"type": "boolean", "default": "True"}
32
34
intWithStringValue: {"type": "integer", "default": "4"}
33
35
numberWithIntValue: {"type": "number", "default": 5}
34
36
numberWithStringValue: {"type": "number", "default": "5.5"}
35
- noneWithStringValue : {"type": "null ", "default": "None" }
37
+ stringWithNumberValue : {"type": "string ", "default": 6 }
36
38
""" )
37
39
@with_generated_code_imports (".models.MyModel" )
38
- class TestDefaultValues :
39
- def test_defaults_in_initializer (self , MyModel , generated_client ):
40
+ class TestSimpleDefaults :
41
+ # Note, the null/None type is not covered here due to a known bug:
42
+ # https://github.com/openapi-generators/openapi-python-client/issues/1162
43
+ def test_defaults_in_initializer (self , MyModel ):
40
44
instance = MyModel ()
41
45
assert instance == MyModel (
42
46
boolean_prop = True ,
43
47
string_prop = "a" ,
44
48
number_prop = 1.5 ,
45
49
int_prop = 2 ,
50
+ date_prop = datetime .date (2024 , 1 , 2 ),
51
+ date_time_prop = datetime .datetime (2024 , 1 , 2 , 3 , 4 , 5 , tzinfo = datetime .timezone .utc ),
52
+ uuid_prop = uuid .UUID ("07EF8B4D-AA09-4FFA-898D-C710796AFF41" ),
46
53
any_prop_with_string = "b" ,
47
54
any_prop_with_int = 3 ,
48
55
boolean_with_string_true_1 = True ,
@@ -52,9 +59,31 @@ def test_defaults_in_initializer(self, MyModel, generated_client):
52
59
int_with_string_value = 4 ,
53
60
number_with_int_value = 5 ,
54
61
number_with_string_value = 5.5 ,
62
+ string_with_number_value = "6" ,
55
63
)
56
- # Note, currently the default for a None property does not work as expected--
57
- # the initializer will default it to UNSET rather than None.
64
+
65
+
66
+
67
+ @with_generated_client_fixture (
68
+ """
69
+ components:
70
+ schemas:
71
+ MyEnum:
72
+ type: string
73
+ enum: ["a", "b"]
74
+ MyModel:
75
+ type: object
76
+ properties:
77
+ enumProp:
78
+ allOf:
79
+ - $ref: "#/components/schemas/MyEnum"
80
+ default: "a"
81
+
82
+ """ )
83
+ @with_generated_code_imports (".models.MyEnum" , ".models.MyModel" )
84
+ class TestEnumDefaults :
85
+ def test_enum_default (self , MyEnum , MyModel ):
86
+ assert MyModel ().enum_prop == MyEnum .A
58
87
59
88
60
89
class TestInvalidDefaultValues :
@@ -79,20 +108,46 @@ def warnings(self):
79
108
WithBadFloatAsOther:
80
109
properties:
81
110
badInt: {"type": "number", "default": true}
111
+ WithBadDateAsString:
112
+ properties:
113
+ badDate: {"type": "string", "format": "date", "default": "xxx"}
114
+ WithBadDateAsOther:
115
+ properties:
116
+ badDate: {"type": "string", "format": "date", "default": 3}
117
+ WithBadDateTimeAsString:
118
+ properties:
119
+ badDate: {"type": "string", "format": "date-time", "default": "xxx"}
120
+ WithBadDateTimeAsOther:
121
+ properties:
122
+ badDate: {"type": "string", "format": "date-time", "default": 3}
123
+ WithBadUuidAsString:
124
+ properties:
125
+ badUuid: {"type": "string", "format": "uuid", "default": "xxx"}
126
+ WithBadUuidAsOther:
127
+ properties:
128
+ badUuid: {"type": "string", "format": "uuid", "default": 3}
129
+ WithBadEnum:
130
+ properties:
131
+ badEnum: {"type": "string", "enum": ["a", "b"], "default": "c"}
82
132
"""
83
133
)
84
134
85
- def test_bad_boolean (self , warnings ):
86
- assert_bad_schema_warning (warnings , "WithBadBoolean" , "Invalid boolean value" )
87
-
88
- def test_bad_int_as_string (self , warnings ):
89
- assert_bad_schema_warning (warnings , "WithBadIntAsString" , "Invalid int value" )
90
-
91
- def test_bad_int_as_other (self , warnings ):
92
- assert_bad_schema_warning (warnings , "WithBadIntAsOther" , "Invalid int value" )
93
-
94
- def test_bad_float_as_string (self , warnings ):
95
- assert_bad_schema_warning (warnings , "WithBadFloatAsString" , "Invalid float value" )
96
-
97
- def test_bad_float_as_other (self , warnings ):
98
- assert_bad_schema_warning (warnings , "WithBadFloatAsOther" , "Cannot convert True to a float" )
135
+ @pytest .mark .parametrize (
136
+ ("model_name" , "message" ),
137
+ [
138
+ ("WithBadBoolean" , "Invalid boolean value" ),
139
+ ("WithBadIntAsString" , "Invalid int value" ),
140
+ ("WithBadIntAsOther" , "Invalid int value" ),
141
+ ("WithBadFloatAsString" , "Invalid float value" ),
142
+ ("WithBadFloatAsOther" , "Cannot convert True to a float" ),
143
+ ("WithBadDateAsString" , "Invalid date" ),
144
+ ("WithBadDateAsOther" , "Cannot convert 3 to a date" ),
145
+ ("WithBadDateTimeAsString" , "Invalid datetime" ),
146
+ ("WithBadDateTimeAsOther" , "Cannot convert 3 to a datetime" ),
147
+ ("WithBadUuidAsString" , "Invalid UUID value" ),
148
+ ("WithBadUuidAsOther" , "Invalid UUID value" ),
149
+ ("WithBadEnum" , "Value c is not valid for enum" ),
150
+ ]
151
+ )
152
+ def test_bad_default_warning (self , model_name , message , warnings ):
153
+ assert_bad_schema_warning (warnings , model_name , message )
0 commit comments