Skip to content

Commit

Permalink
Organization api uptake changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AsabuHere committed Dec 11, 2024
1 parent 39aa6ac commit 97c9ca7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 11 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/twilio/oai/DirectoryStructureService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.twilio.oai;

import com.twilio.oai.api.ApiResourceBuilder;
import com.twilio.oai.common.ApplicationConstants;
import com.twilio.oai.common.Utility;
import com.twilio.oai.resolver.CaseResolver;
import com.twilio.oai.resource.IResourceTree;
Expand All @@ -14,6 +16,8 @@
import lombok.RequiredArgsConstructor;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
Expand All @@ -39,6 +43,8 @@ public class DirectoryStructureService {

@Getter
private boolean isVersionLess = false;

private String apiFileName;
private final Map<String, String> productMap = new HashMap<>();
private final List<CodegenModel> allModels = new ArrayList<>();
private final List<Object> dependentList = new ArrayList<>();
Expand Down Expand Up @@ -253,6 +259,16 @@ public List<CodegenOperation> processOperations(final OperationsMap results) {
additionalProperties.put("apiFilename",
caseResolver.pathOperation(getResourceAliases(firstOperation.path,
null).getClassName()));
apiFileName = caseResolver.pathOperation(getResourceAliases(firstOperation.path,
null).getClassName());
apiFileName = Utility.convertToPascalCase(apiFileName);

// for(CodegenOperation co : operations){
// for(CodegenParameter cp : co.allParams){
// if(cp.isModel)
// updateDataType(cp);
// }
// }

if (isVersionLess) {
additionalProperties.put(API_VERSION, caseResolver.productOperation(version));
Expand Down Expand Up @@ -311,6 +327,12 @@ public String toApiFilename(final String name) {
return String.join(File.separator, pathParts);
}

// private void updateDataType(CodegenParameter property) {
// property.dataType = apiFileName + ApplicationConstants.LIST + ApplicationConstants.DOT + property.dataType;
// property.baseType = property.dataType;
// property.datatypeWithEnum = property.dataType;
// }

public void configureAdditionalProps(Map<String, PathItem> pathMap, String domain, DirectoryStructureService directoryStructureService) {
List<Resource> dependents = new ArrayList<>();
if (domain.equals("api")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public ApiResourceBuilder updateResponseModel(Resolver<CodegenProperty> codegenP
model.setName(resourceName);
model.getVars().forEach(variable -> {
codegenPropertyResolver.resolve(variable, this);
variable.vendorExtensions.put("json-name", variable.baseName);

instancePathParams
.stream()
Expand Down Expand Up @@ -138,7 +139,8 @@ public ApiResourceBuilder updateResponseModel(Resolver<CodegenProperty> codegenP
getModelByClassname(variable.complexType).ifPresent(model -> {
variable.baseType = variable.baseType.replace(variable.datatypeWithEnum, "str");
variable.datatypeWithEnum = "str";
model.vendorExtensions.put("part-of-response-model", true);
if(!model.vendorExtensions.containsKey("part-of-request-model"))
model.vendorExtensions.put("part-of-response-model", true);
});
}
});
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/twilio/oai/common/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,19 @@ public static void resolveContentType(CodegenOperation co) {
}
}
}

public static String convertToPascalCase(String input) {

String[] words = input.split("_");

StringBuilder result = new StringBuilder();

for (String word : words) {
if (!word.isEmpty()) {
result.append(word.substring(0, 1).toUpperCase()).append(word.substring(1).toLowerCase());
}
}

return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public CodegenModel resolve(CodegenModel model, ApiResourceBuilder apiResourceBu
if (model == null) {
return null;
}

model.vendorExtensions.put("part-of-request-model", true);
for (CodegenProperty property : model.vars) {
CodegenModel nestedModel = resolveNestedModel(property, apiResourceBuilder);
if(nestedModel != null) {
Expand All @@ -63,6 +63,7 @@ public CodegenModel resolve(CodegenModel model, ApiResourceBuilder apiResourceBu
else {
model.vendorExtensions.put(key, value);
}

return value;
});
}
Expand Down
22 changes: 15 additions & 7 deletions src/main/resources/twilio-python/context.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ class {{apiName}}Context(InstanceContext):

:returns: The updated {{instanceName}}
"""
data = values.of({ {{#allParams}}{{#isFormParam}}
{{#bodyParam}}data = {{paramName}}.to_dict(){{/bodyParam}}
{{^bodyParam}}data = values.of({ {{#allParams}}{{#isFormParam}}
'{{{baseName}}}': {{#if vendorExtensions.x-serialize}}{{vendorExtensions.x-serialize}}({{paramName}}{{#if isArray}}, lambda e: e){{else}}){{/if}}{{else}}{{paramName}}{{/if}},{{/isFormParam}}{{/allParams}}
})

}){{/bodyParam}}
headers = values.of({})
{{#allParams}}{{#isHeaderParam}}headers['{{{baseName}}}'] = {{paramName}}{{/isHeaderParam}}{{/allParams}}
{{#allParams}}{{#isHeaderParam}}
{{^if required}}
if not ({{paramName}} is values.unset or (isinstance({{paramName}}, str) and not {{paramName}})):{{/if}}
headers['{{{baseName}}}'] = {{paramName}}{{/isHeaderParam}}{{/allParams}}
{{#consumes}}
headers["Content-Type"] = "{{{mediaType}}}"
{{/consumes}}
Expand All @@ -56,11 +59,16 @@ class {{apiName}}Context(InstanceContext):

:returns: The updated {{instanceName}}
"""
data = values.of({ {{#allParams}}{{#isFormParam}}
{{#bodyParam}}data = {{paramName}}.to_dict(){{/bodyParam}}
{{^bodyParam}}data = values.of({ {{#allParams}}{{#isFormParam}}
'{{{baseName}}}': {{#if vendorExtensions.x-serialize}}{{vendorExtensions.x-serialize}}({{paramName}}{{#if isArray}}, lambda e: e){{else}}){{/if}}{{else}}{{paramName}}{{/if}},{{/isFormParam}}{{/allParams}}
})
}){{/bodyParam}}
headers = values.of({})
{{#allParams}}{{#isHeaderParam}}headers['{{{baseName}}}'] = {{paramName}}{{/isHeaderParam}}{{/allParams}}
{{#allParams}}{{#isHeaderParam}}
{{^if required}}
if not ({{paramName}} is values.unset or (isinstance({{paramName}}, str) and not {{paramName}})):{{/if}}
headers['{{{baseName}}}'] = {{paramName}}
{{/isHeaderParam}}{{/allParams}}
{{#consumes}}
headers["Content-Type"] = "{{{mediaType}}}"
{{/consumes}}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/twilio-python/instance.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class {{instanceName}}(InstanceResource):
{{#operations}}{{#vendorExtensions.x-is-context-operation}}
{{#vendorExtensions.x-is-update-operation}}
def {{vendorExtensions.x-name-lower}}(self{{#allParams}}, {{paramName}}: {{#if required}}{{{dataType}}}{{else}}Union[{{{dataType}}}, object]=values.unset{{/if}}{{/allParams}}) -> "{{instanceName}}":
"""
"""hello3
Update the {{instanceName}}
{{#allParams}}
:param {{paramName}}: {{{description}}}{{/allParams}}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/twilio-python/modelClasses.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
def to_dict(self):
return {
{{#vars}}
"{{{vendorExtensions.json-name}}}": {{#if complexType}}{{#containerType}}[{{name}}.to_dict() for {{name}} in self.{{name}}]{{/containerType}}{{^containerType}}self.{{name}}{{^vendorExtensions.x-enum-object}}.to_dict(){{/vendorExtensions.x-enum-object}}{{/containerType}}{{else}}self.{{name}}{{/if}},{{/vars}}
"{{{vendorExtensions.json-name}}}": {{#if complexType}}{{#containerType}}[{{name}}.to_dict() for {{name}} in self.{{name}}] if self.{{name}} is not None else None{{/containerType}}{{^containerType}}self.{{name}}{{^vendorExtensions.x-enum-object}}.to_dict() if self.{{name}} is not None else None {{/vendorExtensions.x-enum-object}}{{/containerType}}{{else}}self.{{name}}{{/if}},{{/vars}}
}
{{/hasVars}}{{/vendorExtensions.part-of-response-model}}{{/models}}

0 comments on commit 97c9ca7

Please sign in to comment.