Skip to content

Commit a632f9f

Browse files
authored
fix(specs): allow null records in getObjects response (#6582)
1 parent 8149b88 commit a632f9f

12 files changed

Lines changed: 152 additions & 7 deletions

File tree

โ€Žgenerators/src/main/java/com/algolia/codegen/AlgoliaDartGenerator.javaโ€Ž

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,35 @@ public String toApiName(String name) {
129129
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
130130
Map<String, ModelsMap> modelsMap = super.postProcessAllModels(objs);
131131
FieldUtils.normalizeVarNames(modelsMap);
132+
markNullableArrayItems(modelsMap);
132133
return support.clearOneOfFromModels(libName, modelsMap);
133134
}
134135

136+
/**
137+
* Renders nullable array items as `List<X?>` so responses with `null` entries (e.g. `getObjects`
138+
* results) can be deserialized.
139+
*/
140+
private static void markNullableArrayItems(Map<String, ModelsMap> modelsMap) {
141+
for (ModelsMap modelContainer : modelsMap.values()) {
142+
List<ModelMap> models = modelContainer.getModels();
143+
if (models == null || models.isEmpty()) {
144+
continue;
145+
}
146+
CodegenModel model = models.get(0).getModel();
147+
if (model.vars == null) {
148+
continue;
149+
}
150+
for (CodegenProperty prop : model.vars) {
151+
if (prop.isArray && prop.items != null && prop.items.isNullable && prop.datatypeWithEnum != null) {
152+
int idx = prop.datatypeWithEnum.lastIndexOf('>');
153+
if (idx >= 0) {
154+
prop.datatypeWithEnum = prop.datatypeWithEnum.substring(0, idx) + "?" + prop.datatypeWithEnum.substring(idx);
155+
}
156+
}
157+
}
158+
}
159+
}
160+
135161
@Override
136162
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
137163
CodegenOperation op = super.fromOperation(path, httpMethod, operation, servers);

โ€Žgenerators/src/main/java/com/algolia/codegen/AlgoliaGoGenerator.javaโ€Ž

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,22 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
130130
// consider it the same as model for our purpose
131131
prop.isModel = true;
132132
}
133+
}
133134

134-
// simplify some dataTypes
135+
// simplify some dataTypes. The field, constructor params, getters and setters come from
136+
// distinct CodegenProperty instances, so we need to update every var list.
137+
for (CodegenProperty prop : Iterables.concat(model.vars, model.allVars, model.requiredVars, model.optionalVars)) {
135138
if (prop.dataType.contains("[]*[]")) {
136139
prop.dataType = prop.dataType.replace("[]*[]", "[][]");
137140
prop.vendorExtensions.put("x-go-base-type", prop.dataType);
138141
}
142+
143+
// a map is already a nilable reference type, so a pointer to a map in a slice is
144+
// redundant: a `null` JSON element unmarshals to a nil map either way.
145+
if (prop.dataType.contains("[]*map[")) {
146+
prop.dataType = prop.dataType.replace("[]*map[", "[]map[");
147+
prop.vendorExtensions.put("x-go-base-type", prop.dataType);
148+
}
139149
}
140150
}
141151
return models;

โ€Žgenerators/src/main/java/com/algolia/codegen/cts/AlgoliaCTSGenerator.javaโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ protected Builder<String, Lambda> addMustacheLambdas() {
102102
lambdas.put("escapeSlash", new EscapeSlashLambda());
103103
lambdas.put("escapeJSON", new EscapeJSONLambda());
104104
lambdas.put("replaceBacktick", new ReplaceBacktickLambda());
105+
lambdas.put("rubyNil", new RubyNilLambda());
105106

106107
return lambdas;
107108
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.algolia.codegen.cts.lambda;
2+
3+
import com.samskivert.mustache.Mustache;
4+
import com.samskivert.mustache.Template;
5+
import java.io.IOException;
6+
import java.io.Writer;
7+
8+
/**
9+
* Ruby inlines the expected response payload (a JSON string) as a Ruby literal, which relies on
10+
* JSON being valid Ruby. The only mismatch is the JSON `null` token, which must become Ruby `nil`.
11+
* This lambda rewrites bare `null` tokens (outside of string values) to `nil`.
12+
*/
13+
public class RubyNilLambda implements Mustache.Lambda {
14+
15+
@Override
16+
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
17+
String text = fragment.execute();
18+
StringBuilder out = new StringBuilder(text.length());
19+
boolean inString = false;
20+
21+
for (int i = 0; i < text.length(); i++) {
22+
char c = text.charAt(i);
23+
24+
if (c == '"' && (i == 0 || text.charAt(i - 1) != '\\')) {
25+
inString = !inString;
26+
}
27+
28+
if (!inString && text.startsWith("null", i) && isTokenBoundary(text, i - 1) && isTokenBoundary(text, i + 4)) {
29+
out.append("nil");
30+
i += 3;
31+
continue;
32+
}
33+
34+
out.append(c);
35+
}
36+
37+
writer.write(out.toString());
38+
}
39+
40+
// A JSON `null` token is surrounded by structural characters (or the string edges), never by
41+
// identifier characters.
42+
private static boolean isTokenBoundary(String text, int index) {
43+
if (index < 0 || index >= text.length()) {
44+
return true;
45+
}
46+
char c = text.charAt(index);
47+
return !Character.isLetterOrDigit(c) && c != '_';
48+
}
49+
}

โ€Žscripts/cts/testServer/algoliaMock.tsโ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ function addRoutes(app: Express): void {
3838
});
3939
});
4040

41+
// `getObjects` returns `null` entries for requested objectIDs that don't exist in the index, so
42+
// the response deserialization must accept a mix of records and `null`.
43+
app.post('/1/indexes/:indexName/objects', (req, res) => {
44+
res.json({
45+
results: [{ objectID: 'foo' }, null],
46+
});
47+
});
48+
4149
// languages that just put the response in a map, there is no strict parsing or types to match.
4250
const isLaxLanguage = (lang: string) => {
4351
return lang === 'dart' || lang === 'javascript' || lang === 'python' || lang === 'php';

โ€Žspecs/search/paths/objects/getObjects.ymlโ€Ž

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ post:
7070
description: Retrieved records.
7171
items:
7272
type: object
73-
description: Retrieved record.
73+
description: |
74+
Retrieved record.
75+
76+
`null` if the requested `objectID` doesn't exist in the index.
77+
nullable: true
7478
x-is-generic: true
7579
required:
7680
- results
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#vendorExtensions}}{{#isContainer}}{{#x-has-child-generic}}List<{{{complexType}}}<T>>{{/x-has-child-generic}}{{^x-has-child-generic}}{{#x-propagated-generic}}List<T>{{/x-propagated-generic}}{{^x-propagated-generic}}{{{datatypeWithEnum}}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}}{{/x-propagated-generic}}{{/x-has-child-generic}}{{/isContainer}}{{^isContainer}}{{#x-is-generic}}T{{/x-is-generic}}{{^x-is-generic}}{{{datatypeWithEnum}}}{{#isEnum}}?{{/isEnum}}{{^required}}{{^isNullable}}{{^x-is-reference-type}}{{^isEnum}}?{{/isEnum}}{{/x-is-reference-type}}{{/isNullable}}{{/required}}{{/x-is-generic}}{{/isContainer}}{{/vendorExtensions}}
1+
{{#vendorExtensions}}{{#isContainer}}{{#x-has-child-generic}}List<{{{complexType}}}<T>>{{/x-has-child-generic}}{{^x-has-child-generic}}{{#x-propagated-generic}}List<T{{#items.isNullable}}?{{/items.isNullable}}>{{/x-propagated-generic}}{{^x-propagated-generic}}{{{datatypeWithEnum}}}{{^isContainer}}{{^required}}?{{/required}}{{/isContainer}}{{/x-propagated-generic}}{{/x-has-child-generic}}{{/isContainer}}{{^isContainer}}{{#x-is-generic}}T{{/x-is-generic}}{{^x-is-generic}}{{{datatypeWithEnum}}}{{#isEnum}}?{{/isEnum}}{{^required}}{{^isNullable}}{{^x-is-reference-type}}{{^isEnum}}?{{/isEnum}}{{/x-is-reference-type}}{{/isNullable}}{{/required}}{{/x-is-generic}}{{/isContainer}}{{/vendorExtensions}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#complexType}}{{{.}}}<T>{{/complexType}}{{^complexType}}T{{/complexType}}{{#isArray}}[]{{/isArray}}
1+
{{#isArray}}{{#items.isNullable}}({{/items.isNullable}}{{#complexType}}{{{.}}}<T>{{/complexType}}{{^complexType}}T{{/complexType}}{{#items.isNullable}} | null){{/items.isNullable}}[]{{/isArray}}{{^isArray}}{{#complexType}}{{{.}}}<T>{{/complexType}}{{^complexType}}T{{/complexType}}{{/isArray}}

โ€Žtemplates/ruby/base_object.mustacheโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
# @return [Hash] Returns the value in the form of hash
118118
def _to_hash(value)
119119
if value.is_a?(Array)
120-
value.compact.map { |v| _to_hash(v) }
120+
value.map { |v| _to_hash(v) }
121121
elsif value.is_a?(Hash)
122122
{}.tap do |hash|
123123
value.each { |k, v| hash[k] = _to_hash(v) }

โ€Žtemplates/ruby/tests/client/tests.mustacheโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
{{/testHost}}
3636
{{#testResponse}}
3737
{{^match.isPrimitive}}
38-
assert_equal({{{match.value}}}, req.is_a?(Array) ? req.map(&:to_hash) : req.to_hash)
38+
assert_equal({{#lambda.rubyNil}}{{{match.value}}}{{/lambda.rubyNil}}, req.is_a?(Array) ? req.map(&:to_hash) : req.to_hash)
3939
{{/match.isPrimitive}}
4040
{{#match.isPrimitive}}
4141
assert_equal({{#match}}{{> tests/generateInnerParams}}{{/match}}, req)

0 commit comments

Comments
ย (0)