2222 stringProp: {"type": "string", "default": "a"}
2323 numberProp: {"type": "number", "default": 1.5}
2424 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"}
2628 anyPropWithString: {"default": "b"}
2729 anyPropWithInt: {"default": 3}
2830 booleanWithStringTrue1: {"type": "boolean", "default": "True"}
3234 intWithStringValue: {"type": "integer", "default": "4"}
3335 numberWithIntValue: {"type": "number", "default": 5}
3436 numberWithStringValue: {"type": "number", "default": "5.5"}
35- noneWithStringValue : {"type": "null ", "default": "None" }
37+ stringWithNumberValue : {"type": "string ", "default": 6 }
3638""" )
3739@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 ):
4044 instance = MyModel ()
4145 assert instance == MyModel (
4246 boolean_prop = True ,
4347 string_prop = "a" ,
4448 number_prop = 1.5 ,
4549 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" ),
4653 any_prop_with_string = "b" ,
4754 any_prop_with_int = 3 ,
4855 boolean_with_string_true_1 = True ,
@@ -52,9 +59,31 @@ def test_defaults_in_initializer(self, MyModel, generated_client):
5259 int_with_string_value = 4 ,
5360 number_with_int_value = 5 ,
5461 number_with_string_value = 5.5 ,
62+ string_with_number_value = "6" ,
5563 )
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
5887
5988
6089class TestInvalidDefaultValues :
@@ -79,20 +108,46 @@ def warnings(self):
79108 WithBadFloatAsOther:
80109 properties:
81110 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"}
82132"""
83133 )
84134
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