Skip to content

Commit fa058a3

Browse files
committed
required pain
1 parent b4320e2 commit fa058a3

File tree

1 file changed

+31
-41
lines changed

1 file changed

+31
-41
lines changed

generators/src/main/java/com/algolia/codegen/cts/tests/ParametersWithDataType.java

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -91,32 +91,28 @@ private Map<String, Object> traverseParams(
9191
Object param,
9292
IJsonSchemaValidationProperties spec,
9393
String parent,
94-
int suffix,
94+
int depth,
9595
boolean isParentFreeFormObject
9696
) throws CTSException {
9797
if (spec == null) {
98-
return traverseParamsWithoutSpec(paramName, param, parent, suffix);
98+
return traverseParamsWithoutSpec(paramName, param, parent, depth);
9999
}
100100
String baseType = getTypeName(spec);
101101
if (baseType == null) {
102102
throw new CTSException("Cannot determine type of " + paramName + " (value: " + param + ")");
103103
}
104104

105105
boolean isCodegenModel = spec instanceof CodegenModel;
106-
107-
Map<String, Object> testOutput = createDefaultOutput();
108-
boolean isRequired = false;
106+
Boolean isRequired = null;
109107

110108
if (spec instanceof CodegenParameter parameter) {
111109
isRequired = parameter.required;
112110
} else if (spec instanceof CodegenProperty property) {
113111
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() + ")");
118112
}
119113

114+
Map<String, Object> testOutput = createDefaultOutput();
115+
120116
if (!isCodegenModel) {
121117
// don't overwrite it if it's already a model sometimes it's in lowercase for some reason
122118
String lowerBaseType = baseType.substring(0, 1).toLowerCase() + baseType.substring(1);
@@ -133,31 +129,32 @@ private Map<String, Object> traverseParams(
133129

134130
testOutput.put("key", finalParamName);
135131
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);
141133
testOutput.put("parent", parent);
142134
testOutput.put("isRoot", "".equals(parent));
143135
testOutput.put("objectName", getObjectNameForLanguage(baseType));
144136
testOutput.put("isParentFreeFormObject", isParentFreeFormObject);
145137

138+
if (isRequired != null) {
139+
testOutput.put("required", isRequired);
140+
testOutput.put("goFunctionalParam", !isRequired && depth == 0);
141+
}
142+
146143
if (param == null) {
147144
handleNull(spec, testOutput);
148145
} else if (spec.getIsArray()) {
149-
handleArray(paramName, param, testOutput, spec, suffix);
146+
handleArray(paramName, param, testOutput, spec, depth);
150147
} else if (spec.getIsEnum()) {
151148
handleEnum(param, testOutput);
152149
} else if (spec.getIsModel() || isCodegenModel) {
153150
// recursive object
154-
handleModel(paramName, param, testOutput, spec, baseType, parent, suffix, isParentFreeFormObject);
151+
handleModel(paramName, param, testOutput, spec, baseType, parent, depth, isParentFreeFormObject);
155152
} else if (baseType.equals("Object")) {
156153
// not var, no item, pure free form
157-
handleObject(paramName, param, testOutput, true, suffix);
154+
handleObject(paramName, param, testOutput, true, depth);
158155
} else if (spec.getIsMap()) {
159156
// free key but only one type
160-
handleMap(paramName, param, testOutput, spec, suffix);
157+
handleMap(paramName, param, testOutput, spec, depth);
161158
} else {
162159
handlePrimitive(param, testOutput, spec);
163160
}
@@ -166,14 +163,12 @@ private Map<String, Object> traverseParams(
166163
}
167164

168165
/** 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 {
170167
String finalParamName = getFinalParamName(paramName);
171168

172169
Map<String, Object> testOutput = createDefaultOutput();
173170
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);
177172
testOutput.put("parent", parent);
178173
testOutput.put("isRoot", "".equals(parent));
179174
// try to infer the type
@@ -187,9 +182,9 @@ private Map<String, Object> traverseParamsWithoutSpec(String paramName, Object p
187182
if (param == null) {
188183
handleNull(null, testOutput);
189184
} else if (param instanceof List) {
190-
handleArray(paramName, param, testOutput, null, suffix);
185+
handleArray(paramName, param, testOutput, null, depth);
191186
} else if (param instanceof Map) {
192-
handleObject(paramName, param, testOutput, false, suffix);
187+
handleObject(paramName, param, testOutput, false, depth);
193188
} else {
194189
handlePrimitive(param, testOutput, null);
195190
}
@@ -240,18 +235,13 @@ private void handleNull(IJsonSchemaValidationProperties spec, Map<String, Object
240235
}
241236
}
242237

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 {
250240
List<Object> items = (List<Object>) param;
251241

252242
List<Object> values = new ArrayList<>();
253243
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));
255245
}
256246

257247
testOutput.put("isArray", true);
@@ -270,7 +260,7 @@ private void handleModel(
270260
IJsonSchemaValidationProperties spec,
271261
String baseType,
272262
String parent,
273-
int suffix,
263+
int depth,
274264
boolean isParentFreeFormObject
275265
) throws CTSException {
276266
if (!spec.getHasVars()) {
@@ -281,7 +271,7 @@ private void handleModel(
281271
List<CodegenProperty> allOf = composedSchemas.getAllOf();
282272

283273
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);
285275

286276
return;
287277
}
@@ -301,7 +291,7 @@ private void handleModel(
301291
CodegenModel model = (CodegenModel) spec;
302292
IJsonSchemaValidationProperties match = findMatchingOneOf(param, model);
303293

304-
testOutput.putAll(traverseParams(paramName, param, match, parent, suffix, isParentFreeFormObject));
294+
testOutput.putAll(traverseParams(paramName, param, match, parent, depth, isParentFreeFormObject));
305295

306296
Map<String, Object> oneOfModel = new HashMap<>();
307297
IJsonSchemaValidationProperties current = match;
@@ -362,7 +352,7 @@ private void handleModel(
362352
entry.getValue(),
363353
additionalPropertiesSpec,
364354
paramName,
365-
suffix + 1,
355+
depth + 1,
366356
false
367357
);
368358
value.put("isAdditionalProperty", true);
@@ -382,7 +372,7 @@ private void handleModel(
382372
);
383373
}
384374
} 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);
386376
values.add(transformedParam);
387377
}
388378
}
@@ -416,23 +406,23 @@ private String getTransformedParamName(String paramName) {
416406
return paramName;
417407
}
418408

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)
420410
throws CTSException {
421411
Map<String, Object> vars = (Map<String, Object>) param;
422412

423413
List<Object> values = new ArrayList<>();
424414
for (Entry<String, Object> entry : vars.entrySet()) {
425415
CodegenParameter objSpec = new CodegenParameter();
426416
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));
428418
}
429419

430420
testOutput.put("isSimpleObject", isSimpleObject);
431421
testOutput.put("isFreeFormObject", true);
432422
testOutput.put("value", values);
433423
}
434424

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)
436426
throws CTSException {
437427
if (spec.getHasVars()) {
438428
throw new CTSException("Spec has vars.");
@@ -459,7 +449,7 @@ private void handleMap(String paramName, Object param, Map<String, Object> testO
459449
itemType = maybeMatch;
460450
}
461451

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));
463453
}
464454

465455
testOutput.put("isFreeFormObject", true);

0 commit comments

Comments
 (0)