Skip to content

Commit

Permalink
fix: update dependent logic to utilize parent resource names (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
childish-sambino authored Oct 28, 2022
1 parent f591440 commit 0bd9d6f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 27 deletions.
4 changes: 2 additions & 2 deletions examples/spec/twilio_api_v2010.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ paths:
servers:
- url: https://api.twilio.com
x-twilio:
parent: /Accounts.json
parent: /Accounts/{Sid}.json
pathType: list
/2010-04-01/Accounts/{AccountSid}/Calls/{TestInteger}.json:
description: A nested resource instance that cannot be updated
Expand Down Expand Up @@ -435,7 +435,7 @@ paths:
servers:
- url: https://api.twilio.com
x-twilio:
parent: /Accounts.json
parent: /Accounts/{Sid}.json
pathType: instance
/2010-04-01/Accounts/{AccountSid}/Calls/Feedback/Summary.json:
description: A nested resource with sub-dirs and without operations
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/twilio/oai/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,10 @@ public static String removeExtension(final String path) {
return path.replaceAll("\\.[^/]+$", "");
}

public static String removePathParamIds(final String path) {
return removeExtension(path).replaceAll("\\{[^}]+", "{");
}

public static String removeTrailingPathParam(final String path) {
return path.replaceFirst("/\\{[^}]+}[^/]*$", "");
}

public static String escapeRegex(final String regex) {
return regex.replace("{", "\\{");
}

public static String fetchLastElement(final String path, final String delimiter) {
return Streams.findLast(Arrays.stream(path.split(delimiter))).get();
}
Expand Down
19 changes: 2 additions & 17 deletions src/main/java/com/twilio/oai/resource/ResourceMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,11 @@ public List<String> ancestors(final String resourceName, final Operation operati
*/
@Override
public List<Resource> dependents(final String name) {
// First get a list of all the descendants.
final List<Resource> descendants = urlResourceMap
return urlResourceMap
.values()
.stream()
.filter(resource -> isDescendent(name, resource.getName()))
.filter(resource -> name.equals(resource.getParentResource(this).map(Resource::getName).orElse(null)))
.collect(Collectors.toList());

// Then filter out any that are descendents of any descendants.
return descendants
.stream()
.filter(descendent -> descendants
.stream()
.noneMatch(current -> isDescendent(current.getName(), descendent.getName())))
.collect(Collectors.toList());
}

private boolean isDescendent(final String resource, final String descendent) {
return PathUtils
.removePathParamIds(descendent)
.matches(PathUtils.escapeRegex(PathUtils.removePathParamIds(resource) + "/[^{]+"));
}

@Override
Expand Down

0 comments on commit 0bd9d6f

Please sign in to comment.