Skip to content

Commit 8a435c3

Browse files
SK-2646: fix tokenize reponse handling
1 parent 67860eb commit 8a435c3

4 files changed

Lines changed: 20 additions & 45 deletions

File tree

v3/src/main/java/com/skyflow/utils/Utils.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,6 @@ public static TokenizeResponse formatTokenizeResponse(
445445
com.skyflow.generated.rest.resources.flowservice.requests.V1FlowTokenizeRequest batchRequest,
446446
int batchNumber, int batchSize) {
447447
if (response != null && response.getResponse().isPresent()) {
448-
// The API returns a flat list — one entry per (value x tokenGroupName).
449-
// We group them by input record position using the request's tokenGroupNames count
450-
// so that records with identical values are still treated as separate entries.
451448
List<com.skyflow.generated.rest.types.V1FlowTokenizeResponseObject> flatList =
452449
response.getResponse().get();
453450
List<com.skyflow.generated.rest.types.V1FlowTokenizeRequestObject> requestData =
@@ -456,20 +453,19 @@ public static TokenizeResponse formatTokenizeResponse(
456453
List<TokenizeSuccess> successRecords = new ArrayList<>();
457454
List<ErrorRecord> errorRecords = new ArrayList<>();
458455

456+
459457
int flatIndex = 0;
460458
for (int i = 0; i < requestData.size(); i++) {
461459
int inputRecordIndex = batchNumber * batchSize + i;
462460
com.skyflow.generated.rest.types.V1FlowTokenizeRequestObject reqObj = requestData.get(i);
463-
List<String> groupNames = reqObj.getTokenGroupNames().isPresent()
464-
? reqObj.getTokenGroupNames().get() : new ArrayList<>();
465-
int groupCount = groupNames.size();
461+
int groupCount = reqObj.getTokenGroupNames().isPresent()
462+
? reqObj.getTokenGroupNames().get().size() : 0;
463+
int entriesToConsume = groupCount > 0 ? groupCount : 1;
466464

467-
// Consume exactly groupCount entries from the flat response for this record
468465
TokenizeSuccess successEntry = null;
469-
for (int g = 0; g < groupCount && flatIndex < flatList.size(); g++, flatIndex++) {
470-
com.skyflow.generated.rest.types.V1FlowTokenizeResponseObject obj = flatList.get(flatIndex);
466+
for (int j = 0; j < entriesToConsume && flatIndex < flatList.size(); j++) {
467+
com.skyflow.generated.rest.types.V1FlowTokenizeResponseObject obj = flatList.get(flatIndex++);
471468
Map<String, Object> props = obj.getAdditionalProperties();
472-
473469
Object value = obj.getValue().isPresent() ? obj.getValue().get()
474470
: (props != null ? props.get("value") : null);
475471
String tokenGroupName = props != null ? (String) props.get("tokenGroupName") : null;

v3/src/main/java/com/skyflow/utils/validations/Validations.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,21 +248,17 @@ public static void validateTokenizeRequest(TokenizeRequest request) throws Skyfl
248248
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyValueInTokenizeRecord.getMessage());
249249
}
250250
List<String> tokenGroupNames = record.getTokenGroupNames();
251-
if (tokenGroupNames == null || tokenGroupNames.isEmpty()) {
252-
LogUtil.printErrorLog(Utils.parameterizedString(
253-
ErrorLogs.EMPTY_TOKEN_GROUP_NAMES_IN_TOKENIZE_RECORD.getLog(), InterfaceName.TOKENIZE.getName()
254-
));
255-
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyTokenGroupNamesInTokenizeRecord.getMessage());
256-
}
257-
for (int j = 0; j < tokenGroupNames.size(); j++) {
258-
String groupName = tokenGroupNames.get(j);
259-
if (groupName == null || groupName.trim().isEmpty()) {
260-
LogUtil.printErrorLog(Utils.parameterizedString(
261-
ErrorLogs.EMPTY_TOKEN_GROUP_NAME_IN_TOKENIZE_RECORD.getLog(),
262-
InterfaceName.TOKENIZE.getName(),
263-
String.valueOf(j)
264-
));
265-
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyTokenGroupNameInTokenizeRecord.getMessage());
251+
if (tokenGroupNames != null) {
252+
for (int j = 0; j < tokenGroupNames.size(); j++) {
253+
String groupName = tokenGroupNames.get(j);
254+
if (groupName == null || groupName.trim().isEmpty()) {
255+
LogUtil.printErrorLog(Utils.parameterizedString(
256+
ErrorLogs.EMPTY_TOKEN_GROUP_NAME_IN_TOKENIZE_RECORD.getLog(),
257+
InterfaceName.TOKENIZE.getName(),
258+
String.valueOf(j)
259+
));
260+
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyTokenGroupNameInTokenizeRecord.getMessage());
261+
}
266262
}
267263
}
268264
}

v3/src/test/java/com/skyflow/vault/controller/VaultControllerTokenizeTests.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,6 @@ public void testValidation_emptyValue() {
164164
}
165165
}
166166

167-
@Test
168-
public void testValidation_emptyTokenGroupNames() {
169-
ArrayList<TokenizeRecord> records = new ArrayList<>();
170-
records.add(TokenizeRecord.builder().value("val").tokenGroupNames(new ArrayList<>()).build());
171-
TokenizeRequest req = TokenizeRequest.builder().data(records).build();
172-
try {
173-
Validations.validateTokenizeRequest(req);
174-
fail("Expected SkyflowException for empty token group names");
175-
} catch (SkyflowException e) {
176-
assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
177-
assertEquals(ErrorMessage.EmptyTokenGroupNamesInTokenizeRecord.getMessage(), e.getMessage());
178-
}
179-
}
180-
181167
@Test
182168
public void testValidation_emptyTokenGroupNameInList() {
183169
ArrayList<TokenizeRecord> records = new ArrayList<>();

v3/src/test/java/com/skyflow/vault/data/TokenizeRequestTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,24 +163,21 @@ public void testValidateTokenizeRequest_nullTokenGroupNames() {
163163
TokenizeRequest req = TokenizeRequest.builder().data(records).build();
164164
try {
165165
Validations.validateTokenizeRequest(req);
166-
Assert.fail("Expected SkyflowException for null tokenGroupNames");
167166
} catch (SkyflowException e) {
168-
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
169-
Assert.assertEquals(ErrorMessage.EmptyTokenGroupNamesInTokenizeRecord.getMessage(), e.getMessage());
167+
Assert.fail("Should not throw for null tokenGroupNames: " + e.getMessage());
170168
}
171169
}
172170

173171
@Test
174172
public void testValidateTokenizeRequest_emptyTokenGroupNames() {
173+
// empty tokenGroupNames list is valid — the API tokenizes against all token groups
175174
ArrayList<TokenizeRecord> records = new ArrayList<>();
176175
records.add(TokenizeRecord.builder().value("val").tokenGroupNames(new ArrayList<>()).build());
177176
TokenizeRequest req = TokenizeRequest.builder().data(records).build();
178177
try {
179178
Validations.validateTokenizeRequest(req);
180-
Assert.fail("Expected SkyflowException for empty tokenGroupNames");
181179
} catch (SkyflowException e) {
182-
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
183-
Assert.assertEquals(ErrorMessage.EmptyTokenGroupNamesInTokenizeRecord.getMessage(), e.getMessage());
180+
Assert.fail("Should not throw for empty tokenGroupNames: " + e.getMessage());
184181
}
185182
}
186183

0 commit comments

Comments
 (0)