Skip to content

Commit

Permalink
bug: ruby to use url instead of mount_name for mapping (#622)
Browse files Browse the repository at this point in the history
<!--
We appreciate the effort for this pull request but before that please
make sure you read the contribution guidelines, then fill out the blanks
below.

Please format the PR title appropriately based on the type of change:
  <type>[!]: <description>
Where <type> is one of: docs, chore, feat, fix, test, misc.
Add a '!' after the type for breaking changes (e.g. feat!: new breaking
feature).

**All third-party contributors acknowledge that any contributions they
provide will be made under the same open-source license that the
open-source project is provided under.**

Please enter each Issue number you are resolving in your PR after one of
the following words [Fixes, Closes, Resolves]. This will auto-link these
issues and close them when this PR is merged!
e.g.
Fixes #1
Closes #2
-->

# Fixes #

This implementation stops using mount_name and uses url to do the
mapping
Validation check pipeline:
https://buildkite.com/twilio/twilio-librarian-status-checks/builds/11963#_
### Checklist
- [x] I acknowledge that all my contributions will be made under the
project's license
- [x] Run `make test-docker`
- [x] Verify affected language:
- [x] Generate [twilio-go](https://github.com/twilio/twilio-go) from our
[OpenAPI specification](https://github.com/twilio/twilio-oai) using the
[build_twilio_go.py](./examples/build_twilio_go.py) using `python
examples/build_twilio_go.py path/to/twilio-oai/spec/yaml
path/to/twilio-go` and inspect the diff
    - [x] Run `make test` in `twilio-go`
    - [x] Create a pull request in `twilio-go`
    - [ ] Provide a link below to the pull request
- [x] I have made a material change to the repo (functionality, testing,
spelling, grammar)
- [x] I have read the [Contribution
Guidelines](https://github.com/twilio/twilio-oai-generator/blob/main/CONTRIBUTING.md)
and my PR follows them
- [x] I have titled the PR appropriately
- [x] I have updated my branch with the main branch
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] I have added the necessary documentation about the functionality
in the appropriate .md file
- [x] I have added inline documentation to the code I modified

If you have questions, please create a GitHub Issue in this repository.
  • Loading branch information
manisha1997 authored Nov 26, 2024
1 parent ba4a80c commit f9896d9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/ruby/lib/twilio-ruby/rest/api/v2010/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def create(
data = Twilio::Values.of({
'RecordingStatusCallback' => recording_status_callback,
'RecordingStatusCallbackEvent' => Twilio.serialize_list(recording_status_callback_event) { |e|
e
},
e
},
'Twiml' => twiml,
})

Expand Down
4 changes: 2 additions & 2 deletions examples/ruby/lib/twilio-ruby/rest/api/v2010/account/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def create(
'RequiredStringProperty' => required_string_property,
'TestMethod' => test_method,
'TestArrayOfStrings' => Twilio.serialize_list(test_array_of_strings) { |e|
e
},
e
},
'TestArrayOfUri' => Twilio.serialize_list(test_array_of_uri) { |e| e },
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ def create(
'TestDate' => Twilio.serialize_iso8601_date(test_date),
'TestEnum' => test_enum,
'TestObjectArray' => Twilio.serialize_list(test_object_array) { |e|
Twilio.serialize_object(e)
},
Twilio.serialize_object(e)
},
'TestAnyType' => Twilio.serialize_object(test_any_type),
'TestAnyArray' => Twilio.serialize_list(test_any_array) { |e|
Twilio.serialize_object(e)
},
Twilio.serialize_object(e)
},
'Permissions' => Twilio.serialize_list(permissions) { |e| e },
'SomeA2PThing' => some_a2p_thing,
})
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/twilio/oai/DirectoryStructureService.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static class ContextResource {
private String mountName;
private String parent;
private String dependentProperties;
private String url;
}

public void configure(final OpenAPI openAPI) {
Expand Down Expand Up @@ -186,12 +187,22 @@ public void addContextdependents(final List<Object> resourceList, final String p
String parent = String.join("\\", resourceTree.ancestors(path, operation));
List<Parameter> pathParamsList = fetchNonParentPathParams(operation);
List<String> pathParamNamesList = pathParamsList.stream().map(Parameter::getName).collect(Collectors.toList());
HashMap<String, String> mapTagToListPath = new HashMap<>();
this.getResourceTree().getResources().iterator().forEachRemaining(resource -> {
if (PathUtils.getTwilioExtension(resource.getPathItem(), "pathType").isPresent() && PathUtils.getTwilioExtension(resource.getPathItem(), "pathType").get().equals("list"))
mapTagToListPath.put(resource.getListTag(), resource.getName());
});
Optional<Resource> tagNameOptional = this.getResourceTree().findResource(path);
String listTag = "";
if( tagNameOptional.isPresent() )
listTag = mapTagToListPath.get(tagNameOptional.get().getListTag());
final ContextResource dependent = new ContextResource.ContextResourceBuilder()
.version(PathUtils.getFirstPathPart(path))
.params(pathParamNamesList)
.mountName(caseResolver.pathOperation(resourceAliases.getMountName()))
.filename(caseResolver.filenameOperation(resourceAliases.getClassName()))
.parent(parent)
.url(pathParamNamesList.isEmpty()?StringHelper.convertUrlToCamelCase(path):StringHelper.convertUrlToCamelCase(listTag))
.build();
if (!resourceList.contains(dependent))
resourceList.add(dependent);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/twilio/oai/StringHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Arrays;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import lombok.experimental.UtilityClass;
Expand Down Expand Up @@ -48,4 +50,27 @@ private String convertFirstChar(final String inputWord, final UnaryOperator<Stri
? inputWord
: firstCharFunction.apply(inputWord.substring(0, 1)) + inputWord.substring(1);
}

public static String convertUrlToCamelCase(String input) {
if (input == null || input.isEmpty()) {
return input;
}

Pattern pattern = Pattern.compile("\\{([a-z0-9_]+)\\}");
Matcher matcher = pattern.matcher(input);
StringBuffer result = new StringBuffer();

while (matcher.find()) {
String original = matcher.group(1);
String[] parts = original.split("_");
StringBuilder camelCase = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
camelCase.append(parts[i].substring(0, 1).toUpperCase()).append(parts[i].substring(1));
}
matcher.appendReplacement(result, "{" + camelCase.toString() + "}");
}
matcher.appendTail(result);

return result.toString();
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/twilio/oai/api/RubyApiResourceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.twilio.oai.DirectoryStructureService;
import com.twilio.oai.PathUtils;
import com.twilio.oai.StringHelper;
import com.twilio.oai.common.ApplicationConstants;
import com.twilio.oai.resolver.Resolver;
import com.twilio.oai.resolver.ruby.RubyCodegenModelResolver;
Expand All @@ -10,7 +11,6 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
Expand Down Expand Up @@ -195,8 +195,8 @@ private void createContextParamsList(List<CodegenOperation> opList) {
.filter(dependent -> dependentMethods.stream()
.map(DirectoryStructureService.ContextResource.class::cast).anyMatch(
methodDependent -> methodDependent.getMountName().equals(dependent.getMountName()))

).collect(Collectors.toList()));
)
.collect(Collectors.toList()));
if (PathUtils.isInstanceOperation(operation)) {
metaAPIProperties.put("contextImportProperties", dependentPropertiesList);
metaAPIProperties.put("contextImportMethods", dependentMethods);
Expand All @@ -209,6 +209,7 @@ private void createContextParamsList(List<CodegenOperation> opList) {
}
});
}

private void updateDependentProperties(List<CodegenOperation> opList){
for (CodegenOperation operation : opList) {
Map<String, String> dependentsForOperation = mapOperationsDependents(operation);
Expand All @@ -221,8 +222,7 @@ private void updateContextResourceDependents(Map<String, String> dependentsForOp
List<DirectoryStructureService.ContextResource> listObjs = (List<DirectoryStructureService.ContextResource>) listofContextResourceObjs;
if(listObjs != null) {
for (DirectoryStructureService.ContextResource cr : listObjs) {
String value = (dependentsForOperation.containsKey(cr.getFilename())) ? dependentsForOperation.get(cr.getFilename())
: dependentsForOperation.get(cr.getMountName());
String value = dependentsForOperation.get(cr.getUrl());
cr.setDependentProperties(value);
}
}
Expand All @@ -243,7 +243,7 @@ private Map<String, String> mapOperationsDependents(CodegenOperation operation){
for (Map.Entry<String, String> mappingEntry : mapping.entrySet()) {
String dependent = mappingEntry.getKey() + ": @solution[:" + mappingEntry.getValue()+ "], ";
dependentParams += dependent;
depMap.put(mountName, dependentParams);
depMap.put(StringHelper.convertUrlToCamelCase((String) propertiesDetails.getValue().get("resource_url")), dependentParams);
}
}
seenParams.add(mountName);
Expand Down

0 comments on commit f9896d9

Please sign in to comment.