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}
38+ stringConst: {"type": "string", "const": "always", "default": "always"}
3639""" )
3740@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 ):
4045 instance = MyModel ()
4146 assert instance == MyModel (
4247 boolean_prop = True ,
4348 string_prop = "a" ,
4449 number_prop = 1.5 ,
4550 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" ),
4654 any_prop_with_string = "b" ,
4755 any_prop_with_int = 3 ,
4856 boolean_with_string_true_1 = True ,
@@ -52,9 +60,32 @@ def test_defaults_in_initializer(self, MyModel, generated_client):
5260 int_with_string_value = 4 ,
5361 number_with_int_value = 5 ,
5462 number_with_string_value = 5.5 ,
63+ string_with_number_value = "6" ,
64+ string_const = "always" ,
5565 )
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
5889
5990
6091class TestInvalidDefaultValues :
@@ -79,20 +110,48 @@ def warnings(self):
79110 WithBadFloatAsOther:
80111 properties:
81112 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"}
82134"""
83135 )
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
84138
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