@@ -91,32 +91,28 @@ private Map<String, Object> traverseParams(
91
91
Object param ,
92
92
IJsonSchemaValidationProperties spec ,
93
93
String parent ,
94
- int suffix ,
94
+ int depth ,
95
95
boolean isParentFreeFormObject
96
96
) throws CTSException {
97
97
if (spec == null ) {
98
- return traverseParamsWithoutSpec (paramName , param , parent , suffix );
98
+ return traverseParamsWithoutSpec (paramName , param , parent , depth );
99
99
}
100
100
String baseType = getTypeName (spec );
101
101
if (baseType == null ) {
102
102
throw new CTSException ("Cannot determine type of " + paramName + " (value: " + param + ")" );
103
103
}
104
104
105
105
boolean isCodegenModel = spec instanceof CodegenModel ;
106
-
107
- Map <String , Object > testOutput = createDefaultOutput ();
108
- boolean isRequired = false ;
106
+ Boolean isRequired = null ;
109
107
110
108
if (spec instanceof CodegenParameter parameter ) {
111
109
isRequired = parameter .required ;
112
110
} else if (spec instanceof CodegenProperty property ) {
113
111
isRequired = property .required ;
114
- } else if (spec instanceof CodegenModel model ) {
115
- isRequired = !model .isOptional ;
116
- } else {
117
- throw new CTSException ("unknown type for " + paramName + " (type: " + spec .getClass ().getSimpleName () + ")" );
118
112
}
119
113
114
+ Map <String , Object > testOutput = createDefaultOutput ();
115
+
120
116
if (!isCodegenModel ) {
121
117
// don't overwrite it if it's already a model sometimes it's in lowercase for some reason
122
118
String lowerBaseType = baseType .substring (0 , 1 ).toLowerCase () + baseType .substring (1 );
@@ -133,31 +129,32 @@ private Map<String, Object> traverseParams(
133
129
134
130
testOutput .put ("key" , finalParamName );
135
131
testOutput .put ("isKeyAllUpperCase" , StringUtils .isAllUpperCase (finalParamName ));
136
- testOutput .put ("parentSuffix" , suffix - 1 );
137
- testOutput .put ("useAnonymousKey" , !finalParamName .matches ("(.*)_[0-9]$" ) && suffix != 0 );
138
- testOutput .put ("required" , isRequired );
139
- testOutput .put ("goFunctionalParam" , !isRequired && suffix == 0 );
140
- testOutput .put ("suffix" , suffix );
132
+ testOutput .put ("useAnonymousKey" , !finalParamName .matches ("(.*)_[0-9]$" ) && depth != 0 );
141
133
testOutput .put ("parent" , parent );
142
134
testOutput .put ("isRoot" , "" .equals (parent ));
143
135
testOutput .put ("objectName" , getObjectNameForLanguage (baseType ));
144
136
testOutput .put ("isParentFreeFormObject" , isParentFreeFormObject );
145
137
138
+ if (isRequired != null ) {
139
+ testOutput .put ("required" , isRequired );
140
+ testOutput .put ("goFunctionalParam" , !isRequired && depth == 0 );
141
+ }
142
+
146
143
if (param == null ) {
147
144
handleNull (spec , testOutput );
148
145
} else if (spec .getIsArray ()) {
149
- handleArray (paramName , param , testOutput , spec , suffix );
146
+ handleArray (paramName , param , testOutput , spec , depth );
150
147
} else if (spec .getIsEnum ()) {
151
148
handleEnum (param , testOutput );
152
149
} else if (spec .getIsModel () || isCodegenModel ) {
153
150
// recursive object
154
- handleModel (paramName , param , testOutput , spec , baseType , parent , suffix , isParentFreeFormObject );
151
+ handleModel (paramName , param , testOutput , spec , baseType , parent , depth , isParentFreeFormObject );
155
152
} else if (baseType .equals ("Object" )) {
156
153
// not var, no item, pure free form
157
- handleObject (paramName , param , testOutput , true , suffix );
154
+ handleObject (paramName , param , testOutput , true , depth );
158
155
} else if (spec .getIsMap ()) {
159
156
// free key but only one type
160
- handleMap (paramName , param , testOutput , spec , suffix );
157
+ handleMap (paramName , param , testOutput , spec , depth );
161
158
} else {
162
159
handlePrimitive (param , testOutput , spec );
163
160
}
@@ -166,14 +163,12 @@ private Map<String, Object> traverseParams(
166
163
}
167
164
168
165
/** Same method but with inference only */
169
- private Map <String , Object > traverseParamsWithoutSpec (String paramName , Object param , String parent , int suffix ) throws CTSException {
166
+ private Map <String , Object > traverseParamsWithoutSpec (String paramName , Object param , String parent , int depth ) throws CTSException {
170
167
String finalParamName = getFinalParamName (paramName );
171
168
172
169
Map <String , Object > testOutput = createDefaultOutput ();
173
170
testOutput .put ("key" , finalParamName );
174
- testOutput .put ("parentSuffix" , suffix - 1 );
175
- testOutput .put ("useAnonymousKey" , !finalParamName .matches ("(.*)_[0-9]$" ) && suffix != 0 );
176
- testOutput .put ("suffix" , suffix );
171
+ testOutput .put ("useAnonymousKey" , !finalParamName .matches ("(.*)_[0-9]$" ) && depth != 0 );
177
172
testOutput .put ("parent" , parent );
178
173
testOutput .put ("isRoot" , "" .equals (parent ));
179
174
// try to infer the type
@@ -187,9 +182,9 @@ private Map<String, Object> traverseParamsWithoutSpec(String paramName, Object p
187
182
if (param == null ) {
188
183
handleNull (null , testOutput );
189
184
} else if (param instanceof List ) {
190
- handleArray (paramName , param , testOutput , null , suffix );
185
+ handleArray (paramName , param , testOutput , null , depth );
191
186
} else if (param instanceof Map ) {
192
- handleObject (paramName , param , testOutput , false , suffix );
187
+ handleObject (paramName , param , testOutput , false , depth );
193
188
} else {
194
189
handlePrimitive (param , testOutput , null );
195
190
}
@@ -240,18 +235,13 @@ private void handleNull(IJsonSchemaValidationProperties spec, Map<String, Object
240
235
}
241
236
}
242
237
243
- private void handleArray (
244
- String paramName ,
245
- Object param ,
246
- Map <String , Object > testOutput ,
247
- IJsonSchemaValidationProperties spec ,
248
- int suffix
249
- ) throws CTSException {
238
+ private void handleArray (String paramName , Object param , Map <String , Object > testOutput , IJsonSchemaValidationProperties spec , int depth )
239
+ throws CTSException {
250
240
List <Object > items = (List <Object >) param ;
251
241
252
242
List <Object > values = new ArrayList <>();
253
243
for (int i = 0 ; i < items .size (); i ++) {
254
- values .add (traverseParams (paramName + "_" + i , items .get (i ), spec == null ? null : spec .getItems (), paramName , suffix + 1 , false ));
244
+ values .add (traverseParams (paramName + "_" + i , items .get (i ), spec == null ? null : spec .getItems (), paramName , depth + 1 , false ));
255
245
}
256
246
257
247
testOutput .put ("isArray" , true );
@@ -270,7 +260,7 @@ private void handleModel(
270
260
IJsonSchemaValidationProperties spec ,
271
261
String baseType ,
272
262
String parent ,
273
- int suffix ,
263
+ int depth ,
274
264
boolean isParentFreeFormObject
275
265
) throws CTSException {
276
266
if (!spec .getHasVars ()) {
@@ -281,7 +271,7 @@ private void handleModel(
281
271
List <CodegenProperty > allOf = composedSchemas .getAllOf ();
282
272
283
273
if (allOf != null && !allOf .isEmpty ()) {
284
- traverseParams (paramName , param , allOf .get (0 ), parent , suffix , false );
274
+ traverseParams (paramName , param , allOf .get (0 ), parent , depth , false );
285
275
286
276
return ;
287
277
}
@@ -301,7 +291,7 @@ private void handleModel(
301
291
CodegenModel model = (CodegenModel ) spec ;
302
292
IJsonSchemaValidationProperties match = findMatchingOneOf (param , model );
303
293
304
- testOutput .putAll (traverseParams (paramName , param , match , parent , suffix , isParentFreeFormObject ));
294
+ testOutput .putAll (traverseParams (paramName , param , match , parent , depth , isParentFreeFormObject ));
305
295
306
296
Map <String , Object > oneOfModel = new HashMap <>();
307
297
IJsonSchemaValidationProperties current = match ;
@@ -362,7 +352,7 @@ private void handleModel(
362
352
entry .getValue (),
363
353
additionalPropertiesSpec ,
364
354
paramName ,
365
- suffix + 1 ,
355
+ depth + 1 ,
366
356
false
367
357
);
368
358
value .put ("isAdditionalProperty" , true );
@@ -382,7 +372,7 @@ private void handleModel(
382
372
);
383
373
}
384
374
} else {
385
- Map <String , Object > transformedParam = traverseParams (entry .getKey (), entry .getValue (), varSpec , paramName , suffix + 1 , false );
375
+ Map <String , Object > transformedParam = traverseParams (entry .getKey (), entry .getValue (), varSpec , paramName , depth + 1 , false );
386
376
values .add (transformedParam );
387
377
}
388
378
}
@@ -416,23 +406,23 @@ private String getTransformedParamName(String paramName) {
416
406
return paramName ;
417
407
}
418
408
419
- private void handleObject (String paramName , Object param , Map <String , Object > testOutput , boolean isSimpleObject , int suffix )
409
+ private void handleObject (String paramName , Object param , Map <String , Object > testOutput , boolean isSimpleObject , int depth )
420
410
throws CTSException {
421
411
Map <String , Object > vars = (Map <String , Object >) param ;
422
412
423
413
List <Object > values = new ArrayList <>();
424
414
for (Entry <String , Object > entry : vars .entrySet ()) {
425
415
CodegenParameter objSpec = new CodegenParameter ();
426
416
objSpec .dataType = inferDataType (entry .getValue (), objSpec , null );
427
- values .add (traverseParams (entry .getKey (), entry .getValue (), objSpec , paramName , suffix + 1 , true ));
417
+ values .add (traverseParams (entry .getKey (), entry .getValue (), objSpec , paramName , depth + 1 , true ));
428
418
}
429
419
430
420
testOutput .put ("isSimpleObject" , isSimpleObject );
431
421
testOutput .put ("isFreeFormObject" , true );
432
422
testOutput .put ("value" , values );
433
423
}
434
424
435
- private void handleMap (String paramName , Object param , Map <String , Object > testOutput , IJsonSchemaValidationProperties spec , int suffix )
425
+ private void handleMap (String paramName , Object param , Map <String , Object > testOutput , IJsonSchemaValidationProperties spec , int depth )
436
426
throws CTSException {
437
427
if (spec .getHasVars ()) {
438
428
throw new CTSException ("Spec has vars." );
@@ -459,7 +449,7 @@ private void handleMap(String paramName, Object param, Map<String, Object> testO
459
449
itemType = maybeMatch ;
460
450
}
461
451
462
- values .add (traverseParams (entry .getKey (), entry .getValue (), itemType , paramName , suffix + 1 , true ));
452
+ values .add (traverseParams (entry .getKey (), entry .getValue (), itemType , paramName , depth + 1 , true ));
463
453
}
464
454
465
455
testOutput .put ("isFreeFormObject" , true );
0 commit comments