From 3331bbecf92fece0543a9294991b20733bd6f38d Mon Sep 17 00:00:00 2001 From: Sam Harrison Date: Thu, 2 Mar 2023 10:45:23 -0600 Subject: [PATCH] fix: correct the enum naming in node, python, and ruby --- examples/node/src/rest/api/v2010/account.ts | 22 +++++----- .../node/src/rest/api/v2010/account/call.ts | 20 ++++----- .../v2010/account/call/feedbackCallSummary.ts | 20 ++++----- .../flexApi/v1/credential/newCredentials.ts | 4 +- .../twilio/rest/api/v2010/account/__init__.py | 10 ++--- .../rest/api/v2010/account/call/__init__.py | 6 +-- .../account/call/feedback_call_summary.py | 6 +-- .../flex_api/v1/credential/new_credentials.py | 2 +- .../lib/twilio-ruby/rest/api/v2010/account.rb | 8 ++-- .../rest/api/v2010/account/call.rb | 4 +- .../account/call/feedback_call_summary.rb | 4 +- .../flex_api/v1/credential/new_credentials.rb | 2 +- .../twilio/oai/DirectoryStructureService.java | 1 - .../java/com/twilio/oai/StringHelper.java | 5 --- .../com/twilio/oai/TwilioGoGenerator.java | 6 +-- .../com/twilio/oai/TwilioJavaGenerator.java | 22 ++++------ .../twilio/oai/api/ApiResourceBuilder.java | 32 ++++++++------ .../oai/api/FluentApiResourceBuilder.java | 42 +++++++++++++++++-- .../twilio/oai/api/FluentApiResources.java | 2 +- .../oai/api/NodeApiResourceBuilder.java | 11 +++++ .../java/com/twilio/oai/common/Utility.java | 25 ++--------- .../oai/resolver/LanguageParamResolver.java | 5 --- .../resolver/LanguagePropertyResolver.java | 5 --- .../CsharpCodegenModelDataTypeResolver.java | 21 +++++----- ...sharpCodegenParameterDataTypeResolver.java | 23 +++++----- src/main/resources/twilio-node/model.mustache | 6 +-- .../twilio-node/requestParam.mustache | 2 +- .../twilio-node/responseModel.mustache | 9 +--- .../twilio-python/instance.handlebars | 2 +- .../resources/twilio-python/model.handlebars | 2 +- .../resources/twilio-ruby/instance.mustache | 2 +- 31 files changed, 168 insertions(+), 163 deletions(-) diff --git a/examples/node/src/rest/api/v2010/account.ts b/examples/node/src/rest/api/v2010/account.ts index e4d3c63de..f2632485b 100644 --- a/examples/node/src/rest/api/v2010/account.ts +++ b/examples/node/src/rest/api/v2010/account.ts @@ -23,12 +23,7 @@ import { CallListInstance } from "./account/call"; import { PhoneNumberCapabilities } from "../../../interfaces"; import TwiML from "../../../twiml/TwiML"; -export class TestResponseObjectTestArrayOfObjects { - "count"?: number; - "description"?: string; -} - -export type TestStatus = +export type AccountStatus = | "in-progress" | "paused" | "stopped" @@ -36,12 +31,17 @@ export type TestStatus = | "completed" | "absent"; +export class TestResponseObjectTestArrayOfObjects { + "count"?: number; + "description"?: string; +} + /** * Options to pass to update a AccountInstance */ export interface AccountContextUpdateOptions { /** */ - status: TestStatus; + status: AccountStatus; /** */ pauseBehavior?: string; } @@ -300,12 +300,12 @@ interface AccountResource { price_unit: string; test_number_float: number; test_number_decimal: number; - test_enum: TestStatus; + test_enum: AccountStatus; a2p_profile_bundle_sid: string; test_array_of_integers: Array; test_array_of_array_of_integers: Array>; test_array_of_objects: Array; - test_array_of_enum: Array; + test_array_of_enum: Array; } export class AccountInstance { @@ -347,7 +347,7 @@ export class AccountInstance { priceUnit: string; testNumberFloat: number; testNumberDecimal: number; - testEnum: TestStatus; + testEnum: AccountStatus; /** * A2P Messaging Profile Bundle BundleSid */ @@ -358,7 +358,7 @@ export class AccountInstance { /** * Permissions authorized to the app */ - testArrayOfEnum: Array; + testArrayOfEnum: Array; private get _proxy(): AccountContext { this._context = diff --git a/examples/node/src/rest/api/v2010/account/call.ts b/examples/node/src/rest/api/v2010/account/call.ts index ce1bd8eea..08e625c5e 100644 --- a/examples/node/src/rest/api/v2010/account/call.ts +++ b/examples/node/src/rest/api/v2010/account/call.ts @@ -20,12 +20,7 @@ import { isValidPathParam } from "../../../../base/utility"; import { FeedbackCallSummaryListInstance } from "./call/feedbackCallSummary"; import { PhoneNumberCapabilities } from "../../../../interfaces"; -export class TestResponseObjectTestArrayOfObjects { - "count"?: number; - "description"?: string; -} - -export type TestStatus = +export type CallStatus = | "in-progress" | "paused" | "stopped" @@ -33,6 +28,11 @@ export type TestStatus = | "completed" | "absent"; +export class TestResponseObjectTestArrayOfObjects { + "count"?: number; + "description"?: string; +} + /** * Options to pass to create a CallInstance */ @@ -174,12 +174,12 @@ interface CallResource { price_unit: string; test_number_float: number; test_number_decimal: number; - test_enum: TestStatus; + test_enum: CallStatus; a2p_profile_bundle_sid: string; test_array_of_integers: Array; test_array_of_array_of_integers: Array>; test_array_of_objects: Array; - test_array_of_enum: Array; + test_array_of_enum: Array; } export class CallInstance { @@ -225,7 +225,7 @@ export class CallInstance { priceUnit: string; testNumberFloat: number; testNumberDecimal: number; - testEnum: TestStatus; + testEnum: CallStatus; /** * A2P Messaging Profile Bundle BundleSid */ @@ -236,7 +236,7 @@ export class CallInstance { /** * Permissions authorized to the app */ - testArrayOfEnum: Array; + testArrayOfEnum: Array; private get _proxy(): CallContext { this._context = diff --git a/examples/node/src/rest/api/v2010/account/call/feedbackCallSummary.ts b/examples/node/src/rest/api/v2010/account/call/feedbackCallSummary.ts index 6d08fe8f2..9527d30a4 100644 --- a/examples/node/src/rest/api/v2010/account/call/feedbackCallSummary.ts +++ b/examples/node/src/rest/api/v2010/account/call/feedbackCallSummary.ts @@ -19,12 +19,7 @@ const serialize = require("../../../../../base/serialize"); import { isValidPathParam } from "../../../../../base/utility"; import { PhoneNumberCapabilities } from "../../../../../interfaces"; -export class TestResponseObjectTestArrayOfObjects { - "count"?: number; - "description"?: string; -} - -export type TestStatus = +export type FeedbackCallSummaryStatus = | "in-progress" | "paused" | "stopped" @@ -32,6 +27,11 @@ export type TestStatus = | "completed" | "absent"; +export class TestResponseObjectTestArrayOfObjects { + "count"?: number; + "description"?: string; +} + /** * Options to pass to update a FeedbackCallSummaryInstance */ @@ -170,12 +170,12 @@ interface FeedbackCallSummaryResource { price_unit: string; test_number_float: number; test_number_decimal: number; - test_enum: TestStatus; + test_enum: FeedbackCallSummaryStatus; a2p_profile_bundle_sid: string; test_array_of_integers: Array; test_array_of_array_of_integers: Array>; test_array_of_objects: Array; - test_array_of_enum: Array; + test_array_of_enum: Array; } export class FeedbackCallSummaryInstance { @@ -218,7 +218,7 @@ export class FeedbackCallSummaryInstance { priceUnit: string; testNumberFloat: number; testNumberDecimal: number; - testEnum: TestStatus; + testEnum: FeedbackCallSummaryStatus; /** * A2P Messaging Profile Bundle BundleSid */ @@ -229,7 +229,7 @@ export class FeedbackCallSummaryInstance { /** * Permissions authorized to the app */ - testArrayOfEnum: Array; + testArrayOfEnum: Array; private get _proxy(): FeedbackCallSummaryContext { this._context = diff --git a/examples/node/src/rest/flexApi/v1/credential/newCredentials.ts b/examples/node/src/rest/flexApi/v1/credential/newCredentials.ts index bb81baa33..179992879 100644 --- a/examples/node/src/rest/flexApi/v1/credential/newCredentials.ts +++ b/examples/node/src/rest/flexApi/v1/credential/newCredentials.ts @@ -18,7 +18,7 @@ const deserialize = require("../../../../base/deserialize"); const serialize = require("../../../../base/serialize"); import { isValidPathParam } from "../../../../base/utility"; -export type TestStatus = +export type NewCredentialsStatus = | "in-progress" | "paused" | "stopped" @@ -53,7 +53,7 @@ export interface NewCredentialsListInstanceCreateOptions { /** */ testDate?: Date; /** */ - testEnum?: TestStatus; + testEnum?: NewCredentialsStatus; /** */ testObjectArray?: Array; /** */ diff --git a/examples/python/twilio/rest/api/v2010/account/__init__.py b/examples/python/twilio/rest/api/v2010/account/__init__.py index 05b981f12..ef0b6f983 100644 --- a/examples/python/twilio/rest/api/v2010/account/__init__.py +++ b/examples/python/twilio/rest/api/v2010/account/__init__.py @@ -309,7 +309,7 @@ def update(self, status, pause_behavior=values.unset): """ Update the AccountInstance - :params TestStatus status: + :params Status status: :params str pause_behavior: :returns: The updated AccountInstance @@ -354,7 +354,7 @@ def __repr__(self): class AccountInstance(InstanceResource): - class TestStatus(object): + class Status(object): IN_PROGRESS = "in-progress" PAUSED = "paused" STOPPED = "stopped" @@ -489,7 +489,7 @@ def test_number_decimal(self): def test_enum(self): """ :returns: - :rtype: TestStatus + :rtype: Status """ return self._properties['test_enum'] @@ -529,7 +529,7 @@ def test_array_of_objects(self): def test_array_of_enum(self): """ :returns: Permissions authorized to the app - :rtype: list[TestStatus] + :rtype: list[Status] """ return self._properties['test_array_of_enum'] @@ -557,7 +557,7 @@ def update(self, status, pause_behavior=values.unset): """ Update the AccountInstance - :params TestStatus status: + :params Status status: :params str pause_behavior: :returns: The updated AccountInstance diff --git a/examples/python/twilio/rest/api/v2010/account/call/__init__.py b/examples/python/twilio/rest/api/v2010/account/call/__init__.py index 015aab710..8a19221b1 100644 --- a/examples/python/twilio/rest/api/v2010/account/call/__init__.py +++ b/examples/python/twilio/rest/api/v2010/account/call/__init__.py @@ -176,7 +176,7 @@ def __repr__(self): class CallInstance(InstanceResource): - class TestStatus(object): + class Status(object): IN_PROGRESS = "in-progress" PAUSED = "paused" STOPPED = "stopped" @@ -311,7 +311,7 @@ def test_number_decimal(self): def test_enum(self): """ :returns: - :rtype: TestStatus + :rtype: Status """ return self._properties['test_enum'] @@ -351,7 +351,7 @@ def test_array_of_objects(self): def test_array_of_enum(self): """ :returns: Permissions authorized to the app - :rtype: list[TestStatus] + :rtype: list[Status] """ return self._properties['test_array_of_enum'] diff --git a/examples/python/twilio/rest/api/v2010/account/call/feedback_call_summary.py b/examples/python/twilio/rest/api/v2010/account/call/feedback_call_summary.py index a41483a70..01e3c0a9a 100644 --- a/examples/python/twilio/rest/api/v2010/account/call/feedback_call_summary.py +++ b/examples/python/twilio/rest/api/v2010/account/call/feedback_call_summary.py @@ -135,7 +135,7 @@ def __repr__(self): class FeedbackCallSummaryInstance(InstanceResource): - class TestStatus(object): + class Status(object): IN_PROGRESS = "in-progress" PAUSED = "paused" STOPPED = "stopped" @@ -270,7 +270,7 @@ def test_number_decimal(self): def test_enum(self): """ :returns: - :rtype: TestStatus + :rtype: Status """ return self._properties['test_enum'] @@ -310,7 +310,7 @@ def test_array_of_objects(self): def test_array_of_enum(self): """ :returns: Permissions authorized to the app - :rtype: list[TestStatus] + :rtype: list[Status] """ return self._properties['test_array_of_enum'] diff --git a/examples/python/twilio/rest/flex_api/v1/credential/new_credentials.py b/examples/python/twilio/rest/flex_api/v1/credential/new_credentials.py index 809d2d4a0..3dee12965 100644 --- a/examples/python/twilio/rest/flex_api/v1/credential/new_credentials.py +++ b/examples/python/twilio/rest/flex_api/v1/credential/new_credentials.py @@ -57,7 +57,7 @@ def create(self, test_string, test_boolean=values.unset, test_integer=values.uns :param dict test_object: :param datetime test_date_time: :param date test_date: - :param TestStatus test_enum: + :param Status test_enum: :param list[object] test_object_array: :param object test_any_type: :param list[object] test_any_array: diff --git a/examples/ruby/lib/twilio-ruby/rest/api/v2010/account.rb b/examples/ruby/lib/twilio-ruby/rest/api/v2010/account.rb index 1bd60a0c5..bcd21b7f8 100644 --- a/examples/ruby/lib/twilio-ruby/rest/api/v2010/account.rb +++ b/examples/ruby/lib/twilio-ruby/rest/api/v2010/account.rb @@ -220,7 +220,7 @@ def fetch ## # Update the AccountInstance # @param [String] pause_behavior - # @param [TestStatus] status + # @param [Status] status # @return [AccountInstance] Updated AccountInstance def update( pause_behavior: :unset, @@ -415,7 +415,7 @@ def test_number_decimal end ## - # @return [TestStatus] + # @return [Status] def test_enum @properties['test_enum'] end @@ -445,7 +445,7 @@ def test_array_of_objects end ## - # @return [Array] Permissions authorized to the app + # @return [Array] Permissions authorized to the app def test_array_of_enum @properties['test_array_of_enum'] end @@ -467,7 +467,7 @@ def fetch ## # Update the AccountInstance # @param [String] pause_behavior - # @param [TestStatus] status + # @param [Status] status # @return [AccountInstance] Updated AccountInstance def update( pause_behavior: :unset, diff --git a/examples/ruby/lib/twilio-ruby/rest/api/v2010/account/call.rb b/examples/ruby/lib/twilio-ruby/rest/api/v2010/account/call.rb index 9e63b5567..b1867165e 100644 --- a/examples/ruby/lib/twilio-ruby/rest/api/v2010/account/call.rb +++ b/examples/ruby/lib/twilio-ruby/rest/api/v2010/account/call.rb @@ -294,7 +294,7 @@ def test_number_decimal end ## - # @return [TestStatus] + # @return [Status] def test_enum @properties['test_enum'] end @@ -324,7 +324,7 @@ def test_array_of_objects end ## - # @return [Array] Permissions authorized to the app + # @return [Array] Permissions authorized to the app def test_array_of_enum @properties['test_array_of_enum'] end diff --git a/examples/ruby/lib/twilio-ruby/rest/api/v2010/account/call/feedback_call_summary.rb b/examples/ruby/lib/twilio-ruby/rest/api/v2010/account/call/feedback_call_summary.rb index f65029c42..eedd1b73f 100644 --- a/examples/ruby/lib/twilio-ruby/rest/api/v2010/account/call/feedback_call_summary.rb +++ b/examples/ruby/lib/twilio-ruby/rest/api/v2010/account/call/feedback_call_summary.rb @@ -232,7 +232,7 @@ def test_number_decimal end ## - # @return [TestStatus] + # @return [Status] def test_enum @properties['test_enum'] end @@ -262,7 +262,7 @@ def test_array_of_objects end ## - # @return [Array] Permissions authorized to the app + # @return [Array] Permissions authorized to the app def test_array_of_enum @properties['test_array_of_enum'] end diff --git a/examples/ruby/lib/twilio-ruby/rest/flex_api/v1/credential/new_credentials.rb b/examples/ruby/lib/twilio-ruby/rest/flex_api/v1/credential/new_credentials.rb index 5bdd135a3..2bcd67c1f 100644 --- a/examples/ruby/lib/twilio-ruby/rest/flex_api/v1/credential/new_credentials.rb +++ b/examples/ruby/lib/twilio-ruby/rest/flex_api/v1/credential/new_credentials.rb @@ -42,7 +42,7 @@ def initialize(version) # @param [Hash] test_object # @param [Time] test_date_time # @param [Date] test_date - # @param [TestStatus] test_enum + # @param [Status] test_enum # @param [Array[Hash]] test_object_array # @param [Object] test_any_type # @param [Array[Hash]] test_any_array diff --git a/src/main/java/com/twilio/oai/DirectoryStructureService.java b/src/main/java/com/twilio/oai/DirectoryStructureService.java index 4c7ddb79f..c7b9e5b97 100644 --- a/src/main/java/com/twilio/oai/DirectoryStructureService.java +++ b/src/main/java/com/twilio/oai/DirectoryStructureService.java @@ -214,7 +214,6 @@ public Optional getApiVersionClass() { public void postProcessAllModels(final Map models, final Map modelFormatMap) { Utility.addModelsToLocalModelList(models, allModels); Utility.setComplexDataMapping(allModels, modelFormatMap); - allModels.forEach(model -> model.setClassname(model.getClassname().replace("Enum", ""))); } public List processOperations(final OperationsMap results) { diff --git a/src/main/java/com/twilio/oai/StringHelper.java b/src/main/java/com/twilio/oai/StringHelper.java index a2e0e8aa0..44f047564 100644 --- a/src/main/java/com/twilio/oai/StringHelper.java +++ b/src/main/java/com/twilio/oai/StringHelper.java @@ -1,7 +1,6 @@ package com.twilio.oai; import java.util.Arrays; -import java.util.Set; import java.util.function.UnaryOperator; import java.util.stream.Collectors; @@ -49,8 +48,4 @@ private String convertFirstChar(final String inputWord, final UnaryOperator set) { - return set.stream().anyMatch(target -> target.equalsIgnoreCase(item)); - } } diff --git a/src/main/java/com/twilio/oai/TwilioGoGenerator.java b/src/main/java/com/twilio/oai/TwilioGoGenerator.java index c2dac97f0..cfbb2e806 100644 --- a/src/main/java/com/twilio/oai/TwilioGoGenerator.java +++ b/src/main/java/com/twilio/oai/TwilioGoGenerator.java @@ -78,8 +78,7 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert property.isEnum = property.dataFormat == null; property.isNullable = true; } else if (property.dataType.contains("Enum")) { - String[] value = property.dataType.split("Enum"); - property.datatypeWithEnum = value[value.length-1]; + property.datatypeWithEnum = Utility.removeEnumName(property.dataType); property.dataType = STRING; property.isEnum = property.dataFormat == null; property.isNullable = true; @@ -94,8 +93,7 @@ private void processEnumParameters(final CodegenParameter parameter) { parameter.isEnum = parameter.dataFormat == null; parameter.isNullable = true; } else if (parameter.dataType.contains("Enum")) { - String[] value = parameter.dataType.split("Enum"); - parameter.datatypeWithEnum = value[value.length-1]; + parameter.datatypeWithEnum = Utility.removeEnumName(parameter.dataType); parameter.dataType = STRING; parameter.isEnum = parameter.dataFormat == null; parameter.isNullable = true; diff --git a/src/main/java/com/twilio/oai/TwilioJavaGenerator.java b/src/main/java/com/twilio/oai/TwilioJavaGenerator.java index 8b79c42de..924759e91 100644 --- a/src/main/java/com/twilio/oai/TwilioJavaGenerator.java +++ b/src/main/java/com/twilio/oai/TwilioJavaGenerator.java @@ -71,15 +71,13 @@ public void postProcessParameter(final CodegenParameter parameter) { super.postProcessParameter(parameter); if (parameter.dataType.startsWith(LIST_START) && parameter.dataType.contains("Enum")) { parameter.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, true); - String[] value = parameter.dataType.split("Enum"); - String lastValue = value[value.length-1]; + String lastValue = Utility.removeEnumName(parameter.dataType); parameter.dataType = LIST_START+lastValue; parameter.baseType = lastValue.substring(0, lastValue.length()-1); } else if(parameter.dataType.contains("Enum")) { parameter.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, true); - String[] value = parameter.dataType.split("Enum"); - parameter.dataType = value[value.length-1]; - parameter.baseType = value[value.length-1]; + parameter.dataType = Utility.removeEnumName(parameter.dataType); + parameter.baseType = Utility.removeEnumName(parameter.dataType); } else if (parameter.isEnum) { parameter.enumName = parameter.paramName; @@ -96,23 +94,21 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert super.postProcessModelProperty(model, property); if (property.dataType.startsWith(LIST_START) && property.dataType.contains("Enum")) { property.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, true); - String[] value = property.dataType.split("Enum"); - String lastValue = value[value.length-1]; + String lastValue = Utility.removeEnumName(property.dataType); property.dataType = LIST_START + lastValue; - property.complexType = lastValue.substring(0, lastValue.length()-1); - property.baseType = lastValue.substring(0, lastValue.length()-1); - property.isEnum = true; + property.complexType = lastValue.substring(0, lastValue.length() - 1); + property.baseType = lastValue.substring(0, lastValue.length() - 1); + property.isEnum = true; property.allowableValues = property.items.allowableValues; property._enum = (List) property.items.allowableValues.get(VALUES); } else if (property.dataType.contains("Enum")) { property.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, true); - String[] value = property.dataType.split("Enum"); - property.dataType = value[value.length - 1]; + property.dataType = Utility.removeEnumName(property.dataType); property.complexType = property.dataType; property.baseType = property.dataType; property.isEnum = true; property._enum = (List) property.allowableValues.get(VALUES); - } else if (property.isEnum ) { + } else if (property.isEnum) { property.enumName = property.baseName; } property.isEnum = property.isEnum && property.dataFormat == null; diff --git a/src/main/java/com/twilio/oai/api/ApiResourceBuilder.java b/src/main/java/com/twilio/oai/api/ApiResourceBuilder.java index 530d0e19d..f7529e96d 100644 --- a/src/main/java/com/twilio/oai/api/ApiResourceBuilder.java +++ b/src/main/java/com/twilio/oai/api/ApiResourceBuilder.java @@ -23,12 +23,13 @@ public abstract class ApiResourceBuilder implements IApiResourceBuilder { public static final String META_LIST_PARAMETER_KEY = "x-list-parameters"; public static final String META_CONTEXT_PARAMETER_KEY = "x-context-parameters"; - protected IApiActionTemplate template; - protected List allModels; - protected List codegenOperationList; - protected List requiredPathParams = new ArrayList<>(); + protected final IApiActionTemplate template; + protected final List allModels; + protected final List codegenOperationList; + protected final Map modelTree = new TreeMap<>(); + protected final List requiredPathParams = new ArrayList<>(); protected Set apiResponseModels = new LinkedHashSet<>(); - protected Map metaAPIProperties = new HashMap<>(); + protected final Map metaAPIProperties = new HashMap<>(); protected final List listOperations = new ArrayList<>(); protected final List instanceOperations = new ArrayList<>(); protected String version = ""; @@ -46,13 +47,15 @@ protected ApiResourceBuilder(IApiActionTemplate template, List @Override public ApiResourceBuilder updateOperations(Resolver codegenParameterIResolver) { this.codegenOperationList.forEach(codegenOperation -> { - codegenOperation.pathParams.forEach(codegenParameterIResolver::resolve); - codegenOperation.allParams.forEach(codegenParameterIResolver::resolve); - codegenOperation.queryParams.forEach(codegenParameterIResolver::resolve); - codegenOperation.formParams.forEach(codegenParameterIResolver::resolve); - codegenOperation.bodyParams.forEach(codegenParameterIResolver::resolve); - codegenOperation.requiredParams.forEach(codegenParameterIResolver::resolve); - codegenOperation.optionalParams.forEach(codegenParameterIResolver::resolve); + codegenOperation.allParams.forEach(param -> addModel(modelTree, param.baseType, param.dataType)); + + codegenOperation.allParams.forEach(param -> resolveParam(codegenParameterIResolver, param)); + codegenOperation.pathParams.forEach(param -> resolveParam(codegenParameterIResolver, param)); + codegenOperation.queryParams.forEach(param -> resolveParam(codegenParameterIResolver, param)); + codegenOperation.formParams.forEach(param -> resolveParam(codegenParameterIResolver, param)); + codegenOperation.bodyParams.forEach(param -> resolveParam(codegenParameterIResolver, param)); + codegenOperation.requiredParams.forEach(param -> resolveParam(codegenParameterIResolver, param)); + codegenOperation.optionalParams.forEach(param -> resolveParam(codegenParameterIResolver, param)); if (codegenOperation.vendorExtensions.containsKey("x-ignore")) { requiredPathParams.addAll(codegenOperation.pathParams @@ -76,6 +79,11 @@ public ApiResourceBuilder updateOperations(Resolver codegenPar return this; } + protected void resolveParam(final Resolver codegenParameterIResolver, + final CodegenParameter param) { + codegenParameterIResolver.resolve(param); + } + @Override public ApiResourceBuilder updateResponseModel(Resolver codegenPropertyResolver) { codegenOperationList.forEach(codegenOperation -> { diff --git a/src/main/java/com/twilio/oai/api/FluentApiResourceBuilder.java b/src/main/java/com/twilio/oai/api/FluentApiResourceBuilder.java index e3da149a4..eba5f0376 100644 --- a/src/main/java/com/twilio/oai/api/FluentApiResourceBuilder.java +++ b/src/main/java/com/twilio/oai/api/FluentApiResourceBuilder.java @@ -2,6 +2,7 @@ import com.twilio.oai.DirectoryStructureService; import com.twilio.oai.PathUtils; +import com.twilio.oai.common.Utility; import com.twilio.oai.resolver.Resolver; import com.twilio.oai.template.IApiActionTemplate; @@ -10,6 +11,7 @@ import java.util.Map; import java.util.Objects; import java.util.TreeMap; +import java.util.function.BiConsumer; import java.util.stream.Collectors; import org.openapitools.codegen.CodegenModel; @@ -30,7 +32,6 @@ public abstract class FluentApiResourceBuilder extends ApiResourceBuilder { CodegenModel responseModel; protected final Map dependents = new TreeMap<>(); - protected final Map models = new TreeMap<>(); protected FluentApiResourceBuilder(final IApiActionTemplate template, final List codegenOperations, @@ -54,7 +55,6 @@ public ApiResourceBuilder updateOperations(final Resolver code super.updateOperations(codegenParameterIResolver); for (final CodegenOperation co : codegenOperationList) { - co.allParams.forEach(param -> addModel(models, param.baseType, param.dataType)); co.allParams.removeAll(co.pathParams); co.requiredParams.removeAll(co.pathParams); co.hasParams = !co.allParams.isEmpty(); @@ -144,7 +144,6 @@ public ApiResourceBuilder updateResponseModel(final Resolver co model.setName(resourceName); model.getVars().forEach(variable -> { codegenPropertyResolver.resolve(variable); - variable.vendorExtensions.put("x-name", resourceName + variable.getNameInCamelCase()); instancePathParams .stream() @@ -164,12 +163,47 @@ public ApiResourceBuilder updateResponseModel(final Resolver co } }); - responseModel.getVars().forEach(variable -> addModel(models, variable.complexType, variable.dataType)); + responseModel.getVars().forEach(variable -> { + addModel(modelTree, variable.complexType, variable.dataType); + + updateDataType(variable.complexType, variable.dataType, (dataTypeWithEnum, dataType) -> { + variable.datatypeWithEnum = dataTypeWithEnum; + variable.baseType = dataType; + }); + }); }); + modelTree.values().forEach(model -> model.setName(removeEnumName(model.getClassname()))); + return this; } + @Override + protected void resolveParam(final Resolver codegenParameterIResolver, + final CodegenParameter param) { + super.resolveParam(codegenParameterIResolver, param); + + updateDataType(param.baseType, param.dataType, (dataTypeWithEnum, dataType) -> { + param.datatypeWithEnum = dataTypeWithEnum; + param.dataType = dataType; + }); + } + + private void updateDataType(final String baseType, + final String dataType, + final BiConsumer consumer) { + consumer.accept(baseType, removeEnumName(dataType)); + + if (baseType != null) { + final String datatypeWithEnum = removeEnumName(baseType); + consumer.accept(datatypeWithEnum, dataType.replace(baseType, datatypeWithEnum)); + } + } + + protected String removeEnumName(final String dataType) { + return Utility.removeEnumName(dataType); + } + @Override public FluentApiResources build() { return new FluentApiResources(this); diff --git a/src/main/java/com/twilio/oai/api/FluentApiResources.java b/src/main/java/com/twilio/oai/api/FluentApiResources.java index 3d9b49712..f1626d89a 100644 --- a/src/main/java/com/twilio/oai/api/FluentApiResources.java +++ b/src/main/java/com/twilio/oai/api/FluentApiResources.java @@ -34,7 +34,7 @@ public FluentApiResources(final FluentApiResourceBuilder apiResourceBuilder) { instancePathParams = apiResourceBuilder.instancePathParams; listPathParams = apiResourceBuilder.listPathParams; responseModel = apiResourceBuilder.responseModel; - models = apiResourceBuilder.models.values(); + models = apiResourceBuilder.modelTree.values(); dependents = apiResourceBuilder.dependents.values(); } diff --git a/src/main/java/com/twilio/oai/api/NodeApiResourceBuilder.java b/src/main/java/com/twilio/oai/api/NodeApiResourceBuilder.java index 918ce48ce..e83c903af 100644 --- a/src/main/java/com/twilio/oai/api/NodeApiResourceBuilder.java +++ b/src/main/java/com/twilio/oai/api/NodeApiResourceBuilder.java @@ -1,6 +1,8 @@ package com.twilio.oai.api; import com.twilio.oai.DirectoryStructureService; +import com.twilio.oai.common.ApplicationConstants; +import com.twilio.oai.common.Utility; import com.twilio.oai.resolver.Resolver; import com.twilio.oai.template.IApiActionTemplate; @@ -49,4 +51,13 @@ public ApiResourceBuilder updateOperations(final Resolver code return this; } + + @Override + protected String removeEnumName(final String dataType) { + if (dataType != null && dataType.contains(ApplicationConstants.ENUM)) { + return getApiName() + Utility.removeEnumName(dataType); + } + + return dataType; + } } diff --git a/src/main/java/com/twilio/oai/common/Utility.java b/src/main/java/com/twilio/oai/common/Utility.java index ba639d840..da0d35c58 100644 --- a/src/main/java/com/twilio/oai/common/Utility.java +++ b/src/main/java/com/twilio/oai/common/Utility.java @@ -3,18 +3,14 @@ import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import lombok.experimental.UtilityClass; import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenOperation; @@ -40,16 +36,6 @@ public void setComplexDataMapping(final List allModels, Map> getConventionalMap() { - try { - return new ObjectMapper().readValue(Thread.currentThread().getContextClassLoader().getResourceAsStream(ApplicationConstants.CONFIG_CSHARP_JSON_PATH), new TypeReference<>() { - }); - } catch (Exception e) { - e.printStackTrace(); - } - return Collections.emptyMap(); - } - public void addModelsToLocalModelList(final Map modelMap, List localModels){ for (final ModelsMap mods : modelMap.values()) { final List modList = mods.getModels(); @@ -62,14 +48,9 @@ public void addModelsToLocalModelList(final Map modelMap, Lis } public String removeEnumName(final String dataType) { - return dataType == null ? null : dataType.replace("Enum", ""); - } - - @SuppressWarnings("unchecked") - public List getOperations(final Map resource) { - return (ArrayList) resource.computeIfAbsent( - "operations", - k -> new ArrayList<>()); + return dataType == null + ? null + : Arrays.stream(dataType.split(ApplicationConstants.ENUM)).reduce((first, second) -> second).orElseThrow(); } public String getMd5(String input){ diff --git a/src/main/java/com/twilio/oai/resolver/LanguageParamResolver.java b/src/main/java/com/twilio/oai/resolver/LanguageParamResolver.java index 3fe9c5ed4..3a4106946 100644 --- a/src/main/java/com/twilio/oai/resolver/LanguageParamResolver.java +++ b/src/main/java/com/twilio/oai/resolver/LanguageParamResolver.java @@ -2,7 +2,6 @@ import com.twilio.oai.StringHelper; import com.twilio.oai.common.ApplicationConstants; -import com.twilio.oai.common.Utility; import lombok.AllArgsConstructor; import org.openapitools.codegen.CodegenParameter; @@ -18,10 +17,6 @@ public CodegenParameter resolve(CodegenParameter codegenParameter) { resolveProperties(codegenParameter); resolvePrefixedMap(codegenParameter); resolveSerialize(codegenParameter); - - codegenParameter.dataType = Utility.removeEnumName(codegenParameter.dataType); - codegenParameter.baseType = Utility.removeEnumName(codegenParameter.baseType); - return codegenParameter; } diff --git a/src/main/java/com/twilio/oai/resolver/LanguagePropertyResolver.java b/src/main/java/com/twilio/oai/resolver/LanguagePropertyResolver.java index 4da6762d1..16942d94c 100644 --- a/src/main/java/com/twilio/oai/resolver/LanguagePropertyResolver.java +++ b/src/main/java/com/twilio/oai/resolver/LanguagePropertyResolver.java @@ -1,7 +1,6 @@ package com.twilio.oai.resolver; import com.twilio.oai.common.ApplicationConstants; -import com.twilio.oai.common.Utility; import lombok.AllArgsConstructor; import org.openapitools.codegen.CodegenProperty; @@ -17,10 +16,6 @@ public CodegenProperty resolve(CodegenProperty codegenProperty) { resolveProperties(codegenProperty); resolveDeSerialize(codegenProperty); resolvePrefixedMap(codegenProperty); - - codegenProperty.dataType = Utility.removeEnumName(codegenProperty.dataType); - codegenProperty.complexType = Utility.removeEnumName(codegenProperty.complexType); - return codegenProperty; } diff --git a/src/main/java/com/twilio/oai/resolver/csharp/CsharpCodegenModelDataTypeResolver.java b/src/main/java/com/twilio/oai/resolver/csharp/CsharpCodegenModelDataTypeResolver.java index ac3cbf20f..44fdd65d6 100644 --- a/src/main/java/com/twilio/oai/resolver/csharp/CsharpCodegenModelDataTypeResolver.java +++ b/src/main/java/com/twilio/oai/resolver/csharp/CsharpCodegenModelDataTypeResolver.java @@ -2,13 +2,15 @@ import com.twilio.oai.StringHelper; import com.twilio.oai.common.ApplicationConstants; +import com.twilio.oai.common.Utility; import com.twilio.oai.resolver.IConventionMapper; import com.twilio.oai.resolver.common.CodegenModelDataTypeResolver; -import org.openapitools.codegen.CodegenProperty; import java.util.Map; import java.util.Optional; +import org.openapitools.codegen.CodegenProperty; + // Overriding default behavior and adding "x-jsonConverter" to enum public class CsharpCodegenModelDataTypeResolver extends CodegenModelDataTypeResolver { @@ -16,6 +18,7 @@ public CsharpCodegenModelDataTypeResolver(IConventionMapper mapper, Map importStm = mapper.libraries().get(StringHelper.toSnakeCase(property.dataType).replace("_", "-")); + if (importStm.isPresent() && importStm.get() instanceof String && importStm.get().equals("Twilio.Types")) { // If the datatype found in libraries(csharp.json) is Twilio.Types, import enum into the resource file. operationStore.setEnumPresentInResource(true); } } - return property; + return; } String className = OperationStore.getInstance().getClassName(); property.isEnum = true; - String[] value = property.complexType.split(ApplicationConstants.ENUM); - property.enumName = value[value.length-1] + ApplicationConstants.ENUM; + property.enumName = Utility.removeEnumName(property.complexType) + ApplicationConstants.ENUM; // In case enum is an array if (property.items != null) { - property.items.enumName = value[value.length-1] + ApplicationConstants.ENUM; + property.items.enumName = property.enumName; } property.dataType = className + ApplicationConstants.RESOURCE + ApplicationConstants.DOT + property.enumName; property.vendorExtensions.put("x-jsonConverter", "StringEnumConverter"); // TODO: Remove this. operationStore.getEnums().put(property.enumName, property); // Import enum into the resource if it contains enum class. operationStore.setEnumPresentInResource(true); - return property; } } diff --git a/src/main/java/com/twilio/oai/resolver/csharp/CsharpCodegenParameterDataTypeResolver.java b/src/main/java/com/twilio/oai/resolver/csharp/CsharpCodegenParameterDataTypeResolver.java index 9b90ad71e..45c6ac09c 100644 --- a/src/main/java/com/twilio/oai/resolver/csharp/CsharpCodegenParameterDataTypeResolver.java +++ b/src/main/java/com/twilio/oai/resolver/csharp/CsharpCodegenParameterDataTypeResolver.java @@ -2,6 +2,7 @@ import com.twilio.oai.StringHelper; import com.twilio.oai.common.ApplicationConstants; +import com.twilio.oai.common.Utility; import com.twilio.oai.resolver.IConventionMapper; import com.twilio.oai.resolver.common.CodegenParameterDataTypeResolver; import org.openapitools.codegen.CodegenParameter; @@ -16,32 +17,30 @@ public CsharpCodegenParameterDataTypeResolver(IConventionMapper mapper, CsharpSe this.csharpSerializer = csharpSerializer; } + @Override public CodegenParameter resolve(CodegenParameter parameter) { super.resolve(parameter); - // Resolve Enums resolveEnum(parameter); csharpSerializer.serialize(parameter); return parameter; } - private CodegenParameter resolveEnum(CodegenParameter parameter) { - if (!mapper.properties().getString(parameter.dataFormat).isEmpty()) { + private void resolveEnum(CodegenParameter parameter) { + if (mapper.properties().getString(parameter.dataFormat).isPresent()) { // If the dataformat found in libraries(csharp.json) is Twilio.Types, import enum into the options file. - Optional importStm = mapper.libraries().get(StringHelper.toSnakeCase(parameter.dataFormat).replaceAll("_", "-")); - if (!importStm.isEmpty() && importStm.get() instanceof String && importStm.get().equals("Twilio.Types")) { + Optional importStm = mapper.libraries().get(StringHelper.toSnakeCase(parameter.dataFormat).replace("_", "-")); + if (importStm.isPresent() && importStm.get() instanceof String && importStm.get().equals("Twilio.Types")) { OperationStore.getInstance().setEnumPresentInOptions(true); } - return parameter; + return; } if (parameter.dataType.contains(ApplicationConstants.ENUM)) { parameter.isEnum = true; - String[] value = parameter.dataType.split(ApplicationConstants.ENUM); - parameter.enumName = value[value.length-1] + ApplicationConstants.ENUM; - setDataType(parameter); - } else if (parameter.isEnum) { + parameter.enumName = Utility.removeEnumName(parameter.dataType) + ApplicationConstants.ENUM; + } + if (parameter.isEnum) { setDataType(parameter); } - return parameter; } private void setDataType(CodegenParameter parameter) { @@ -54,6 +53,4 @@ private void setDataType(CodegenParameter parameter) { OperationStore.getInstance().getEnums().putIfAbsent(parameter.enumName, parameter); OperationStore.getInstance().setEnumPresentInResource(true); } - - } diff --git a/src/main/resources/twilio-node/model.mustache b/src/main/resources/twilio-node/model.mustache index a5407697f..d3ba41889 100644 --- a/src/main/resources/twilio-node/model.mustache +++ b/src/main/resources/twilio-node/model.mustache @@ -6,19 +6,19 @@ */ {{/description}} {{^isEnum}} -export class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{ +export class {{name}} {{#parent}}extends {{{parent}}} {{/parent}}{ {{#vars}} {{#description}} /** * {{{description}}} */ {{/description}} - "{{name}}"{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}}; + "{{name}}"{{^required}}?{{/required}}: {{{dataType}}}{{#isNullable}} | null{{/isNullable}}; {{/vars}} } {{/isEnum}} {{#isEnum}} -export type {{classname}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}|{{/-last}}{{/enumVars}}{{/allowableValues}}; +export type {{name}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}|{{/-last}}{{/enumVars}}{{/allowableValues}}; {{/isEnum}} {{/models}} diff --git a/src/main/resources/twilio-node/requestParam.mustache b/src/main/resources/twilio-node/requestParam.mustache index e438c76ae..3e96d8754 100644 --- a/src/main/resources/twilio-node/requestParam.mustache +++ b/src/main/resources/twilio-node/requestParam.mustache @@ -4,7 +4,7 @@ {{/vendorExtensions.x-prefixed-collapsible-map}} {{^vendorExtensions.x-prefixed-collapsible-map}} {{#vendorExtensions.x-serialize}} - data["{{{baseName}}}"] = {{.}}(params["{{paramName}}"]{{#vendorExtensions.x-transform}}, (e: {{baseType}}) => {{.}}{{/vendorExtensions.x-transform}}); + data["{{{baseName}}}"] = {{.}}(params["{{paramName}}"]{{#vendorExtensions.x-transform}}, (e: {{datatypeWithEnum}}) => {{.}}{{/vendorExtensions.x-transform}}); {{/vendorExtensions.x-serialize}} {{^vendorExtensions.x-serialize}} data["{{{baseName}}}"] = params["{{paramName}}"]; diff --git a/src/main/resources/twilio-node/responseModel.mustache b/src/main/resources/twilio-node/responseModel.mustache index 7bdd7b806..2c52b0118 100644 --- a/src/main/resources/twilio-node/responseModel.mustache +++ b/src/main/resources/twilio-node/responseModel.mustache @@ -1,9 +1,4 @@ {{#responseModel}} -{{#vars}} -{{#isEnum}} -export type {{vendorExtensions.x-name}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}|{{/-last}}{{/enumVars}}{{/allowableValues}}; -{{/isEnum}} -{{/vars}} interface {{apiName}}Payload extends {{^hasPaginationOperation}}{{apiName}}Resource {}{{/hasPaginationOperation}}{{#hasPaginationOperation}}TwilioResponsePayload { {{recordKey}}: {{apiName}}Resource[]; @@ -11,7 +6,7 @@ interface {{apiName}}Payload extends {{^hasPaginationOperation}}{{apiName}}Resou interface {{apiName}}Resource { {{#vars}} - {{baseName}}: {{#isEnum}}{{{vendorExtensions.x-name}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}; + {{baseName}}: {{{baseType}}}; {{/vars}} } @@ -42,7 +37,7 @@ export class {{instanceName}} { * {{{description}}} */ {{/description}} - {{name}}: {{#isEnum}}{{{vendorExtensions.x-name}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}; + {{name}}: {{{baseType}}}; {{/vars}} {{#instancePath}} diff --git a/src/main/resources/twilio-python/instance.handlebars b/src/main/resources/twilio-python/instance.handlebars index f6585fe2b..4e95ba8d5 100644 --- a/src/main/resources/twilio-python/instance.handlebars +++ b/src/main/resources/twilio-python/instance.handlebars @@ -32,7 +32,7 @@ class {{instanceName}}(InstanceResource): def {{name}}(self): """ :returns: {{{description}}} - :rtype: {{dataType}} + :rtype: {{baseType}} """ return self._properties['{{name}}'] {{/vars}}{{#operations}}{{#vendorExtensions.x-is-context-operation}} diff --git a/src/main/resources/twilio-python/model.handlebars b/src/main/resources/twilio-python/model.handlebars index a348bc6b2..975bd18a4 100644 --- a/src/main/resources/twilio-python/model.handlebars +++ b/src/main/resources/twilio-python/model.handlebars @@ -1,4 +1,4 @@ {{#models}}{{#isEnum}} - class {{classname}}(object):{{#allowableValues}}{{#values}} + class {{name}}(object):{{#allowableValues}}{{#values}} {{{upper (snake_case (lower this))}}} = "{{{.}}}"{{/values}}{{/allowableValues}} {{/isEnum}}{{/models}} \ No newline at end of file diff --git a/src/main/resources/twilio-ruby/instance.mustache b/src/main/resources/twilio-ruby/instance.mustache index 89914363a..8d6a54181 100644 --- a/src/main/resources/twilio-ruby/instance.mustache +++ b/src/main/resources/twilio-ruby/instance.mustache @@ -37,7 +37,7 @@ {{/metaProperties.x-is-context-operation}} {{#responseModel}}{{#vars}} ## - # @return [{{{dataType}}}] {{{description}}} + # @return [{{{baseType}}}] {{{description}}} def {{name}} @properties['{{name}}'] end