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}
38
+ stringConst: {"type": "string", "const": "always", "default": "always"}
36
39
""" )
37
40
@with_generated_code_imports (".models.MyModel" )
38
- class TestDefaultValues :
39
- def test_defaults_in_initializer (self , MyModel , generated_client ):
41
+ class TestSimpleDefaults :
42
+ # Note, the null/None type is not covered here due to a known bug:
43
+ # https://github.com/openapi-generators/openapi-python-client/issues/1162
44
+ def test_defaults_in_initializer (self , MyModel ):
40
45
instance = MyModel ()
41
46
assert instance == MyModel (
42
47
boolean_prop = True ,
43
48
string_prop = "a" ,
44
49
number_prop = 1.5 ,
45
50
int_prop = 2 ,
51
+ date_prop = datetime .date (2024 , 1 , 2 ),
52
+ date_time_prop = datetime .datetime (2024 , 1 , 2 , 3 , 4 , 5 , tzinfo = datetime .timezone .utc ),
53
+ uuid_prop = uuid .UUID ("07EF8B4D-AA09-4FFA-898D-C710796AFF41" ),
46
54
any_prop_with_string = "b" ,
47
55
any_prop_with_int = 3 ,
48
56
boolean_with_string_true_1 = True ,
@@ -52,9 +60,32 @@ def test_defaults_in_initializer(self, MyModel, generated_client):
52
60
int_with_string_value = 4 ,
53
61
number_with_int_value = 5 ,
54
62
number_with_string_value = 5.5 ,
63
+ string_with_number_value = "6" ,
64
+ string_const = "always" ,
55
65
)
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.
66
+
67
+
68
+
69
+ @with_generated_client_fixture (
70
+ """
71
+ components:
72
+ schemas:
73
+ MyEnum:
74
+ type: string
75
+ enum: ["a", "b"]
76
+ MyModel:
77
+ type: object
78
+ properties:
79
+ enumProp:
80
+ allOf:
81
+ - $ref: "#/components/schemas/MyEnum"
82
+ default: "a"
83
+
84
+ """ )
85
+ @with_generated_code_imports (".models.MyEnum" , ".models.MyModel" )
86
+ class TestEnumDefaults :
87
+ def test_enum_default (self , MyEnum , MyModel ):
88
+ assert MyModel ().enum_prop == MyEnum .A
58
89
59
90
60
91
class TestInvalidDefaultValues :
@@ -79,20 +110,48 @@ def warnings(self):
79
110
WithBadFloatAsOther:
80
111
properties:
81
112
badInt: {"type": "number", "default": true}
113
+ WithBadDateAsString:
114
+ properties:
115
+ badDate: {"type": "string", "format": "date", "default": "xxx"}
116
+ WithBadDateAsOther:
117
+ properties:
118
+ badDate: {"type": "string", "format": "date", "default": 3}
119
+ WithBadDateTimeAsString:
120
+ properties:
121
+ badDate: {"type": "string", "format": "date-time", "default": "xxx"}
122
+ WithBadDateTimeAsOther:
123
+ properties:
124
+ badDate: {"type": "string", "format": "date-time", "default": 3}
125
+ WithBadUuidAsString:
126
+ properties:
127
+ badUuid: {"type": "string", "format": "uuid", "default": "xxx"}
128
+ WithBadUuidAsOther:
129
+ properties:
130
+ badUuid: {"type": "string", "format": "uuid", "default": 3}
131
+ WithBadEnum:
132
+ properties:
133
+ badEnum: {"type": "string", "enum": ["a", "b"], "default": "x"}
82
134
"""
83
135
)
136
+ # Note, the null/None type, and binary strings (files), are not covered here due to a known bug:
137
+ # https://github.com/openapi-generators/openapi-python-client/issues/1162
84
138
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" )
139
+ @pytest .mark .parametrize (
140
+ ("model_name" , "message" ),
141
+ [
142
+ ("WithBadBoolean" , "Invalid boolean value" ),
143
+ ("WithBadIntAsString" , "Invalid int value" ),
144
+ ("WithBadIntAsOther" , "Invalid int value" ),
145
+ ("WithBadFloatAsString" , "Invalid float value" ),
146
+ ("WithBadFloatAsOther" , "Cannot convert True to a float" ),
147
+ ("WithBadDateAsString" , "Invalid date" ),
148
+ ("WithBadDateAsOther" , "Cannot convert 3 to a date" ),
149
+ ("WithBadDateTimeAsString" , "Invalid datetime" ),
150
+ ("WithBadDateTimeAsOther" , "Cannot convert 3 to a datetime" ),
151
+ ("WithBadUuidAsString" , "Invalid UUID value" ),
152
+ ("WithBadUuidAsOther" , "Invalid UUID value" ),
153
+ ("WithBadEnum" , "Value x is not valid for enum" ),
154
+ ]
155
+ )
156
+ def test_bad_default_warning (self , model_name , message , warnings ):
157
+ assert_bad_schema_warning (warnings , model_name , message )
0 commit comments