@@ -19,77 +19,84 @@ class DefaultPropertiesTest extends VeryBaseTestCase
19
19
public function getValidTests ()
20
20
{
21
21
return array (
22
+ /*
23
+ // This test case was intended to check whether a default value can be applied for the
24
+ // entire object, however testing this case is impossible, because there is no way to
25
+ // distinguish between a deliberate top-level NULL and a top level that contains nothing.
26
+ // As such, the assumption is that a top-level NULL is deliberate, and should not be
27
+ // altered by replacing it with a default value.
22
28
array(// #0 default value for entire object
23
29
'',
24
30
'{"default":"valueOne"}',
25
31
'"valueOne"'
26
32
),
27
- array (// #1 default value in an empty object
33
+ */
34
+ array (// #0 default value in an empty object
28
35
'{} ' ,
29
36
'{"properties":{"propertyOne":{"default":"valueOne"}}} ' ,
30
37
'{"propertyOne":"valueOne"} '
31
38
),
32
- array (// #2 default value for top-level property
39
+ array (// #1 default value for top-level property
33
40
'{"propertyOne":"valueOne"} ' ,
34
41
'{"properties":{"propertyTwo":{"default":"valueTwo"}}} ' ,
35
42
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"} '
36
43
),
37
- array (// #3 default value for sub-property
44
+ array (// #2 default value for sub-property
38
45
'{"propertyOne":{}} ' ,
39
46
'{"properties":{"propertyOne":{"properties":{"propertyTwo":{"default":"valueTwo"}}}}} ' ,
40
47
'{"propertyOne":{"propertyTwo":"valueTwo"}} '
41
48
),
42
- array (// #4 default value for sub-property with sibling
49
+ array (// #3 default value for sub-property with sibling
43
50
'{"propertyOne":{"propertyTwo":"valueTwo"}} ' ,
44
51
'{"properties":{"propertyOne":{"properties":{"propertyThree":{"default":"valueThree"}}}}} ' ,
45
52
'{"propertyOne":{"propertyTwo":"valueTwo","propertyThree":"valueThree"}} '
46
53
),
47
- array (// #5 default value for top-level property with type check
54
+ array (// #4 default value for top-level property with type check
48
55
'{"propertyOne":"valueOne"} ' ,
49
56
'{"properties":{"propertyTwo":{"default":"valueTwo","type":"string"}}} ' ,
50
57
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"} '
51
58
),
52
- array (// #6 default value for top-level property with v3 required check
59
+ array (// #5 default value for top-level property with v3 required check
53
60
'{"propertyOne":"valueOne"} ' ,
54
61
'{"properties":{"propertyTwo":{"default":"valueTwo","required":"true"}}} ' ,
55
62
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"} '
56
63
),
57
- array (// #7 default value for top-level property with v4 required check
64
+ array (// #6 default value for top-level property with v4 required check
58
65
'{"propertyOne":"valueOne"} ' ,
59
66
'{"properties":{"propertyTwo":{"default":"valueTwo"}},"required":["propertyTwo"]} ' ,
60
67
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"} '
61
68
),
62
- array (// #8 default value for an already set property
69
+ array (// #7 default value for an already set property
63
70
'{"propertyOne":"alreadySetValueOne"} ' ,
64
71
'{"properties":{"propertyOne":{"default":"valueOne"}}} ' ,
65
72
'{"propertyOne":"alreadySetValueOne"} '
66
73
),
67
- array (// #9 default item value for an array
74
+ array (// #8 default item value for an array
68
75
'["valueOne"] ' ,
69
76
'{"type":"array","items":[{},{"type":"string","default":"valueTwo"}]} ' ,
70
77
'["valueOne","valueTwo"] '
71
78
),
72
- array (// #10 default item value for an empty array
79
+ array (// #9 default item value for an empty array
73
80
'[] ' ,
74
81
'{"type":"array","items":[{"type":"string","default":"valueOne"}]} ' ,
75
82
'["valueOne"] '
76
83
),
77
- array (// #11 property without a default available
84
+ array (// #10 property without a default available
78
85
'{"propertyOne":"alreadySetValueOne"} ' ,
79
86
'{"properties":{"propertyOne":{"type":"string"}}} ' ,
80
87
'{"propertyOne":"alreadySetValueOne"} '
81
88
),
82
- array (// #12 default property value is an object
89
+ array (// #11 default property value is an object
83
90
'{"propertyOne":"valueOne"} ' ,
84
91
'{"properties":{"propertyTwo":{"default":{}}}} ' ,
85
92
'{"propertyOne":"valueOne","propertyTwo":{}} '
86
93
),
87
- array (// #13 default item value is an object
94
+ array (// #12 default item value is an object
88
95
'[] ' ,
89
96
'{"type":"array","items":[{"default":{}}]} ' ,
90
97
'[{}] '
91
98
),
92
- array (// #14 only set required values (draft-04)
99
+ array (// #13 only set required values (draft-04)
93
100
'{} ' ,
94
101
'{
95
102
"properties": {
@@ -101,7 +108,7 @@ public function getValidTests()
101
108
'{"propertyTwo":"valueTwo"} ' ,
102
109
Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS
103
110
),
104
- array (// #15 only set required values (draft-03)
111
+ array (// #14 only set required values (draft-03)
105
112
'{} ' ,
106
113
'{
107
114
"properties": {
@@ -112,21 +119,36 @@ public function getValidTests()
112
119
'{"propertyTwo":"valueTwo"} ' ,
113
120
Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS
114
121
),
115
- array (// #16 infinite recursion via $ref (object)
122
+ array (// #15 infinite recursion via $ref (object)
116
123
'{} ' ,
117
124
'{"properties":{"propertyOne": {"$ref": "#","default": {}}}} ' ,
118
125
'{"propertyOne":{}} '
119
126
),
120
- array (// #17 infinite recursion via $ref (array)
127
+ array (// #16 infinite recursion via $ref (array)
121
128
'[] ' ,
122
129
'{"items":[{"$ref":"#","default":[]}]} ' ,
123
130
'[[]] '
124
131
),
125
- array (// #18 default value for null
132
+ array (// #17 default top value does not overwrite defined null
126
133
'null ' ,
127
134
'{"default":"valueOne"} ' ,
128
- '"valueOne" '
129
- )
135
+ 'null '
136
+ ),
137
+ array (// #18 default property value does not overwrite defined null
138
+ '{"propertyOne":null} ' ,
139
+ '{"properties":{"propertyOne":{"default":"valueOne"}}} ' ,
140
+ '{"propertyOne":null} '
141
+ ),
142
+ array (// #19 default value in an object is null
143
+ '{} ' ,
144
+ '{"properties":{"propertyOne":{"default":null}}} ' ,
145
+ '{"propertyOne":null} '
146
+ ),
147
+ array (// #20 default value in an array is null
148
+ '[] ' ,
149
+ '{"items":[{"default":null}]} ' ,
150
+ '[null] '
151
+ ),
130
152
);
131
153
}
132
154
@@ -180,16 +202,16 @@ public function testValidCasesUsingAssocWithoutTypeCast($input, $schema, $expect
180
202
181
203
public function testNoModificationViaReferences ()
182
204
{
183
- $ input = json_decode ('' );
184
- $ schema = json_decode ('{"default ":{"propertyOne":" valueOne"}} ' );
205
+ $ input = json_decode ('{} ' );
206
+ $ schema = json_decode ('{"properties ":{"propertyOne":{"default":" valueOne"} }} ' );
185
207
186
208
$ validator = new Validator ();
187
209
$ validator ->validate ($ input , $ schema , Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_APPLY_DEFAULTS );
188
210
189
211
$ this ->assertEquals ('{"propertyOne":"valueOne"} ' , json_encode ($ input ));
190
212
191
213
$ input ->propertyOne = 'valueTwo ' ;
192
- $ this ->assertEquals ('valueOne ' , $ schema ->default ->propertyOne );
214
+ $ this ->assertEquals ('valueOne ' , $ schema ->properties ->propertyOne -> default );
193
215
}
194
216
195
217
public function testLeaveBasicTypesAlone ()
0 commit comments