Conversation
sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/GroundingController.java
Show resolved
Hide resolved
sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/GroundingTest.java
Outdated
Show resolved
Hide resolved
sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/GroundingTest.java
Outdated
Show resolved
Hide resolved
newtork
left a comment
There was a problem hiding this comment.
Minor code inconsistency
CharlesDuboisSAP
left a comment
There was a problem hiding this comment.
Add the new endpoint in the HTML homepage, otherwise LGTM
I left it out intentionally because most of the other endpoints are also not part of the HTML page. I did not see a reason to add this when the others like collection/create are not there. |
| final var collections = this.getAllCollections("json"); | ||
| final var collectionsList = ((CollectionsListResponse) collections).getResources(); | ||
| var statusCode = 0; | ||
| final var deletions = new java.util.ArrayList<>(List.of()); |
There was a problem hiding this comment.
(Minor)
| final var deletions = new java.util.ArrayList<>(List.of()); | |
| final var deletions = new java.util.ArrayList<>(); |
You could also use an import statement for that.
There was a problem hiding this comment.
What exactly do you mean with using an import statement?
There was a problem hiding this comment.
I mean it's uncommon to use some basic collection class with fully-qualified name:
new java.util.ArrayList<>();Because using FQN here implies that there might be a naming conflict with some custom class that's named ArrayList which would very likely result in confusions and mixups: It has a code smell.
Normally, devs would just put an import statement upfront.
import java.util.ArrayList;
new ArrayList<>();There was a problem hiding this comment.
Ah, sorry... I was thinking you ment something else (ich stand auf dem Schlauch) 😅
| var statusCode = 0; | ||
| final var deletions = new java.util.ArrayList<>(List.of()); | ||
|
|
||
| for (final var collection : collectionsList) { | ||
| if (COLLECTION_TITLE.equals(collection.getTitle())) { | ||
| final var deletion = this.deleteDocuments(collection.getId(), "json"); | ||
| if (deletion instanceof OpenApiResponse) { | ||
| final var status = ((OpenApiResponse) deletion).getStatusCode(); | ||
| deletions.add(deletion); | ||
| statusCode = Math.max(status, statusCode); | ||
| } | ||
| } | ||
| } | ||
| if ("json".equals(format)) { | ||
| return deletions; | ||
| } | ||
| return statusCode >= 400 ? "Failed to delete collections" : "Deletion successful"; |
There was a problem hiding this comment.
I like your loop! For comparison, this is what it would've looked with Stream API
| var statusCode = 0; | |
| final var deletions = new java.util.ArrayList<>(List.of()); | |
| for (final var collection : collectionsList) { | |
| if (COLLECTION_TITLE.equals(collection.getTitle())) { | |
| final var deletion = this.deleteDocuments(collection.getId(), "json"); | |
| if (deletion instanceof OpenApiResponse) { | |
| final var status = ((OpenApiResponse) deletion).getStatusCode(); | |
| deletions.add(deletion); | |
| statusCode = Math.max(status, statusCode); | |
| } | |
| } | |
| } | |
| if ("json".equals(format)) { | |
| return deletions; | |
| } | |
| return statusCode >= 400 ? "Failed to delete collections" : "Deletion successful"; | |
| final var deletions = new java.util.ArrayList<>(); | |
| var statusCode = | |
| collectionsList.stream() | |
| .filter(c -> COLLECTION_TITLE.equals(c.getTitle())) | |
| .map(collection -> this.deleteDocuments(collection.getId(), "json")) | |
| .peek(deletions::add) | |
| .mapToInt(deletion -> ((OpenApiResponse) deletion).getStatusCode()) | |
| .max(); | |
| if ("json".equals(format)) { | |
| return deletions; | |
| } | |
| return statusCode.orElse(0) >= 400 ? "Failed to delete collections" : "Deletion successful"; |
However you could still reduce the code a little by dropping the instanceof check
| var statusCode = 0; | |
| final var deletions = new java.util.ArrayList<>(List.of()); | |
| for (final var collection : collectionsList) { | |
| if (COLLECTION_TITLE.equals(collection.getTitle())) { | |
| final var deletion = this.deleteDocuments(collection.getId(), "json"); | |
| if (deletion instanceof OpenApiResponse) { | |
| final var status = ((OpenApiResponse) deletion).getStatusCode(); | |
| deletions.add(deletion); | |
| statusCode = Math.max(status, statusCode); | |
| } | |
| } | |
| } | |
| if ("json".equals(format)) { | |
| return deletions; | |
| } | |
| return statusCode >= 400 ? "Failed to delete collections" : "Deletion successful"; | |
| var statusCode = 0; | |
| final var deletions = new java.util.ArrayList<>(List.of()); | |
| for (final var collection : collectionsList) { | |
| if (COLLECTION_TITLE.equals(collection.getTitle())) { | |
| final var deletion = (OpenApiResponse) this.deleteDocuments(collection.getId(), "json"); | |
| deletions.add(deletion); | |
| statusCode = Math.max(deletion.getStatusCode(), statusCode); | |
| } | |
| } | |
| if ("json".equals(format)) { | |
| return deletions; | |
| } | |
| return statusCode >= 400 ? "Failed to delete collections" : "Deletion successful"; |
(Minor)
- For consistency, please remove the throw statement in
deleteDocuments, the rest of the controller is not throwing (explicitly). - Would you mind renaming
deleteDocumentstodeleteCollection(singular)´? To me it looks like it would tie in better withdeleteCollections(plural).
Context
Fix the failing grounding e2e test and also fix a small problem in the sample code.