diff --git a/src/main/java/com/twilio/oai/JsonRequestBodyResolver.java b/src/main/java/com/twilio/oai/JsonRequestBodyResolver.java index 5d44add61..f85379dd1 100644 --- a/src/main/java/com/twilio/oai/JsonRequestBodyResolver.java +++ b/src/main/java/com/twilio/oai/JsonRequestBodyResolver.java @@ -5,7 +5,7 @@ import com.twilio.oai.common.EnumConstants; import com.twilio.oai.resolver.Resolver; import com.twilio.oai.resolver.java.ContainerResolver; -import com.twilio.oai.resolver.java.JavaConventionResolver; +import com.twilio.oai.resolver.java.JavaCodegenParameterDataTypeResolver; import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenParameter; import org.openapitools.codegen.CodegenProperty; @@ -21,7 +21,7 @@ public class JsonRequestBodyResolver { final private ContainerResolver containerResolver = new ContainerResolver(Arrays.asList(EnumConstants.JavaDataTypes.values())); - private final JavaConventionResolver conventionResolver; + private final JavaCodegenParameterDataTypeResolver javaCodegenParameterDataTypeResolver = new JavaCodegenParameterDataTypeResolver(null); public void setResourceName(String resourceName) { this.resourceName = resourceName; @@ -32,7 +32,6 @@ public void setResourceName(String resourceName) { public JsonRequestBodyResolver(ApiResourceBuilder apiResourceBuilder, final Resolver codegenPropertyResolver) { this.codegenPropertyResolver = codegenPropertyResolver; this.apiResourceBuilder = apiResourceBuilder; - this.conventionResolver = new JavaConventionResolver(); } public void resolve(final CodegenParameter codegenParameter, final Resolver codegenParameterResolver) { @@ -42,7 +41,7 @@ public void resolve(final CodegenParameter codegenParameter, final Resolver allModels = new ArrayList<>(); + private JavaCodegenParameterDataTypeResolver javaCodegenParameterDataTypeResolver = new JavaCodegenParameterDataTypeResolver(conventionMapper); + public TwilioJavaGenerator() { super(); twilioCodegen = new TwilioCodegenAdapter(this, getName()); @@ -75,52 +78,14 @@ public String toParamName(final String name) { @Override public void postProcessParameter(final CodegenParameter parameter) { super.postProcessParameter(parameter); - if (parameter.dataType.startsWith(LIST_START) && CodegenUtils.isParameterSchemaEnumJava(parameter)) { - if (parameter.dataType.contains(ENUM)) { - String lastValue = Utility.removeEnumName(parameter.dataType); - parameter.dataType = LIST_START + lastValue; - parameter.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, true); - parameter.baseType = lastValue.substring(0, lastValue.length() - 1); - } - } else if (CodegenUtils.isParameterSchemaEnumJava(parameter)) { - parameter.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, true); - parameter.enumName = parameter.dataType; - parameter.dataType = Utility.removeEnumName(parameter.dataType); - parameter.baseType = Utility.removeEnumName(parameter.dataType); - parameter.isEnum = true; - } else if (parameter.isEnum) { - parameter.enumName = parameter.paramName; - } else { - if (parameter.isPathParam) { - parameter.paramName = "Path" + parameter.paramName.substring(0, 1).toUpperCase() + parameter.paramName.substring(1); - } - } + javaCodegenParameterDataTypeResolver.processEnumParams(parameter); } @SuppressWarnings("unchecked") @Override public void postProcessModelProperty(CodegenModel model, CodegenProperty property) { super.postProcessModelProperty(model, property); - if (property.dataType.startsWith(LIST_START) && CodegenUtils.isPropertySchemaEnumJava(property)) { - String lastValue = Utility.removeEnumName(property.dataType); - property.dataType = LIST_START + lastValue; - property.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, 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 (CodegenUtils.isPropertySchemaEnumJava(property)) { - property.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, true); - 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) { - property.enumName = property.baseName; - } - property.isEnum = property.isEnum && property.dataFormat == null; + javaCodegenParameterDataTypeResolver.postProcessModelEnumProperty(property); } @Override diff --git a/src/main/java/com/twilio/oai/api/JavaApiResourceBuilder.java b/src/main/java/com/twilio/oai/api/JavaApiResourceBuilder.java index ed33d1a2b..f87f5882f 100644 --- a/src/main/java/com/twilio/oai/api/JavaApiResourceBuilder.java +++ b/src/main/java/com/twilio/oai/api/JavaApiResourceBuilder.java @@ -7,7 +7,6 @@ import com.twilio.oai.common.EnumConstants; import com.twilio.oai.common.Utility; import com.twilio.oai.resolver.Resolver; -import com.twilio.oai.resolver.java.JavaConventionResolver; import com.twilio.oai.template.IApiActionTemplate; import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenOperation; @@ -22,6 +21,7 @@ import static com.twilio.oai.common.ApplicationConstants.*; import static com.twilio.oai.template.AbstractApiActionTemplate.NESTED_MODELS; import static com.twilio.oai.template.JavaApiActionTemplate.API_TEMPLATE; +import com.twilio.oai.resolver.java.JavaCodegenParameterDataTypeResolver; public class JavaApiResourceBuilder extends ApiResourceBuilder{ @@ -31,7 +31,7 @@ public class JavaApiResourceBuilder extends ApiResourceBuilder{ protected long serialVersionUID; private Set headerParamModelList; - private final JavaConventionResolver conventionResolver; + private final JavaCodegenParameterDataTypeResolver javaCodegenParameterDataTypeResolver = new JavaCodegenParameterDataTypeResolver(null); private Resolver codegenPropertyIResolver; @@ -42,7 +42,6 @@ public class JavaApiResourceBuilder extends ApiResourceBuilder{ public JavaApiResourceBuilder(IApiActionTemplate template, List codegenOperations, List allModels) { super(template, codegenOperations, allModels); - this.conventionResolver = new JavaConventionResolver(); } public JavaApiResourceBuilder(IApiActionTemplate apiActionTemplate, List opList, @@ -82,7 +81,7 @@ public ApiResourceBuilder updateOperations(Resolver codegenPar co.allParams.stream() .filter(item -> !(item.getContent() != null && item.getContent().get("application/json") != null)) .map(item -> codegenParameterIResolver.resolve(item, this)) - .map(item -> conventionResolver.resolveEnumParameter(item, resourceName)) + .map(item -> javaCodegenParameterDataTypeResolver.resolveEnumParameter(item, resourceName)) .collect(Collectors.toList()); jsonRequestBodyResolver.setResourceName(resourceName); @@ -99,36 +98,36 @@ public ApiResourceBuilder updateOperations(Resolver codegenPar co.allParams.forEach(this::updateHeaderParamsList); co.pathParams = co.pathParams.stream() .map(item -> codegenParameterIResolver.resolve(item, this)) - .map(item -> conventionResolver.resolveEnumParameter(item, resourceName)) + .map(item -> javaCodegenParameterDataTypeResolver.resolveEnumParameter(item, resourceName)) .collect(Collectors.toList()); co.pathParams.stream(). map(item -> codegenParameterIResolver.resolve(item, this)) - .map(item -> conventionResolver.resolveEnumParameter(item, resourceName)) + .map(item -> javaCodegenParameterDataTypeResolver.resolveEnumParameter(item, resourceName)) .forEach(param -> param.paramName = "path"+param.paramName); co.queryParams = co.queryParams.stream() .map(item -> codegenParameterIResolver.resolve(item, this)) - .map(item -> conventionResolver.resolveEnumParameter(item, resourceName)) + .map(item -> javaCodegenParameterDataTypeResolver.resolveEnumParameter(item, resourceName)) .collect(Collectors.toList()); co.queryParams = preProcessQueryParameters(co); co.formParams = co.formParams.stream() .map(item -> codegenParameterIResolver.resolve(item, this)) - .map(item -> conventionResolver.resolveEnumParameter(item, resourceName)) + .map(item -> javaCodegenParameterDataTypeResolver.resolveEnumParameter(item, resourceName)) .collect(Collectors.toList()); processDataTypesForParams(co.formParams); co.headerParams = co.headerParams.stream() .map(item -> codegenParameterIResolver.resolve(item, this)) - .map(item -> conventionResolver.resolveEnumParameter(item, resourceName)) + .map(item -> javaCodegenParameterDataTypeResolver.resolveEnumParameter(item, resourceName)) .collect(Collectors.toList()); processDataTypesForParams(co.headerParams); co.optionalParams = co.optionalParams .stream() .map(item -> codegenParameterIResolver.resolve(item, this)) - .map(item -> conventionResolver.resolveEnumParameter(item, resourceName)) + .map(item -> javaCodegenParameterDataTypeResolver.resolveEnumParameter(item, resourceName)) .collect(Collectors.toList()); co.requiredParams = co.requiredParams .stream() .map(item -> codegenParameterIResolver.resolve(item, this)) - .map(item -> conventionResolver.resolveEnumParameter(item, resourceName)) + .map(item -> javaCodegenParameterDataTypeResolver.resolveEnumParameter(item, resourceName)) .collect(Collectors.toList()); co.hasParams = !co.allParams.isEmpty(); co.hasRequiredParams = !co.requiredParams.isEmpty(); diff --git a/src/main/java/com/twilio/oai/resolver/java/JavaCodegenParameterDataTypeResolver.java b/src/main/java/com/twilio/oai/resolver/java/JavaCodegenParameterDataTypeResolver.java new file mode 100644 index 000000000..06aa64774 --- /dev/null +++ b/src/main/java/com/twilio/oai/resolver/java/JavaCodegenParameterDataTypeResolver.java @@ -0,0 +1,179 @@ +package com.twilio.oai.resolver.java; + +import com.twilio.oai.CodegenUtils; +import com.twilio.oai.common.EnumConstants; +import com.twilio.oai.resolver.IConventionMapper; +import com.twilio.oai.resolver.common.CodegenParameterDataTypeResolver; +import org.openapitools.codegen.CodegenParameter; +import org.openapitools.codegen.CodegenProperty; +import static com.twilio.oai.common.ApplicationConstants.ENUM; +import static com.twilio.oai.common.ApplicationConstants.ENUM_VARS; +import static com.twilio.oai.common.ApplicationConstants.LIST_END; +import static com.twilio.oai.common.ApplicationConstants.LIST_START; +import static com.twilio.oai.common.ApplicationConstants.REF_ENUM_EXTENSION_NAME; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.Stack; +import com.twilio.oai.common.ApplicationConstants; +import com.twilio.oai.common.Utility; +import com.twilio.oai.StringHelper; +import org.openapitools.codegen.IJsonSchemaValidationProperties; + +public class JavaCodegenParameterDataTypeResolver extends CodegenParameterDataTypeResolver { + protected final IConventionMapper mapper; + private static final String VALUES = "values"; + + public Set enums = new HashSet<>(); + + public JavaCodegenParameterDataTypeResolver(IConventionMapper mapper) { + super(mapper); + this.mapper = mapper; + } + + /* Moved from TwilioJavaGenerator postProcessParameter function*/ + public CodegenParameter processEnumParams(CodegenParameter parameter){ + if (parameter.dataType.startsWith(LIST_START) && CodegenUtils.isParameterSchemaEnumJava(parameter)) { + if (parameter.dataType.contains(ENUM)) { + String lastValue = Utility.removeEnumName(parameter.dataType); + parameter.dataType = LIST_START + lastValue; + parameter.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, true); + parameter.baseType = lastValue.substring(0, lastValue.length() - 1); + } + } else if (CodegenUtils.isParameterSchemaEnumJava(parameter)) { + parameter.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, true); + parameter.enumName = parameter.dataType; + parameter.dataType = Utility.removeEnumName(parameter.dataType); + parameter.baseType = Utility.removeEnumName(parameter.dataType); + parameter.isEnum = true; + } else if (parameter.isEnum) { + parameter.enumName = parameter.paramName; + } else { + if (parameter.isPathParam) { + parameter.paramName = "Path" + parameter.paramName.substring(0, 1).toUpperCase() + parameter.paramName.substring(1); + } + } + return parameter; + } + + /* Moved from TwilioJavaGenerator postProcessModelProperty*/ + public CodegenProperty postProcessModelEnumProperty(CodegenProperty property){ + if (property.dataType.startsWith(LIST_START) && CodegenUtils.isPropertySchemaEnumJava(property)) { + String lastValue = Utility.removeEnumName(property.dataType); + property.dataType = LIST_START + lastValue; + property.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, 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 (CodegenUtils.isPropertySchemaEnumJava(property)) { + property.vendorExtensions.put(REF_ENUM_EXTENSION_NAME, true); + 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) { + property.enumName = property.baseName; + } + property.isEnum = property.isEnum && property.dataFormat == null; + return property; + } + + /* + * Mustache Object (property or parameter) + * enum class name = enumName + * data type = datatype (apiName.enumName) + * variable name = name + */ + @SuppressWarnings("unchecked") + public CodegenParameter resolveEnumParameter(CodegenParameter parameter, String resourceName) { + if( parameter.isEnum && !parameter.vendorExtensions.containsKey(REF_ENUM_EXTENSION_NAME)) { + if (parameter.enumName.contains(ApplicationConstants.ENUM)) { + parameter.enumName = Utility.removeEnumName(parameter.enumName); + } + parameter.enumName = StringHelper.camelize(parameter.enumName); + if (parameter.items != null && parameter.items.allowableValues != null && parameter.items.allowableValues.containsKey(VALUES)) { + parameter.dataType = LIST_START + resourceName+"."+ parameter.enumName + LIST_END; + parameter.baseType = resourceName + "." + parameter.enumName; + } else { + parameter.dataType = resourceName + "." + parameter.enumName; + } + + return parameter; + } + if (parameter.items != null && parameter.items.allowableValues != null && parameter.items.allowableValues.containsKey(VALUES) ) { + parameter.isEnum = true; + parameter.enumName = parameter.baseType; + parameter._enum = (List) parameter.items.allowableValues.get(VALUES); + parameter.dataType = LIST_START + resourceName + "." + parameter.baseType + LIST_END; + parameter.baseType = resourceName + "." + parameter.baseType; + parameter.allowableValues = parameter.items.allowableValues; + } + if (parameter.allowableValues != null && parameter.allowableValues.containsKey(ENUM_VARS)) { + parameter.isEnum = true; + parameter._enum = (List) parameter.allowableValues.get(VALUES); + // Check if parameter enum is already resolved or not. + if (parameter.enumName == null || !parameter.dataType.startsWith(resourceName + ".")) { + // What if datatype is null ? + // remove resource name from datatype + parameter.enumName = parameter.dataType; + } + if (parameter.dataType == null || !parameter.dataType.startsWith(resourceName + "." )) { + parameter.dataType = resourceName + "." + parameter.dataType; + } + } + return parameter; + } + + public CodegenProperty resolveEnumProperty(CodegenProperty property, String resourceName) { + ContainerResolver containerResolver = new ContainerResolver(Arrays.asList(EnumConstants.JavaDataTypes.values())); + if(CodegenUtils.isPropertySchemaEnumJava(property)) { + // complexType contains the class name, dataType contains the data type for enum which prefixes class name. + // name contains variable name + // This is new gen enums + Stack containerTypes = new Stack<>(); + if (property.isContainer) { + property.dataType = containerResolver.unwrapContainerType(property, containerTypes); + if (property.dataType != null && property.dataType.contains(ApplicationConstants.DOT)) { + // If models is resolved more than twice then we need to remove previous resource name + property.dataType = property.dataType.substring(property.dataType.indexOf(ApplicationConstants.DOT) + 1); + } + property.enumName = property.enumName == null ? property.dataType : property.enumName; + property.dataType = property.dataType != null && property.dataType.contains(resourceName + ApplicationConstants.DOT) + ? property.dataType: resourceName + ApplicationConstants.DOT + property.dataType; + if (property.complexType.contains(ApplicationConstants.ENUM)) { + property.complexType = Utility.removeEnumName(property.complexType); + property.dataType = Utility.removeEnumName(property.dataType); + } + containerResolver.rewrapContainerType(property, containerTypes); + property.isEnum = true; + property.allowableValues = property.items.allowableValues; + property._enum = (List) property.items.allowableValues.get(VALUES); + } else { + if (property.complexType.contains(ApplicationConstants.ENUM)) { + property.complexType = Utility.removeEnumName(property.complexType); + property.dataType = Utility.removeEnumName(property.dataType); + } + if (property.dataType != null && property.dataType.contains(ApplicationConstants.DOT)) { + // If models is resolved more than twice then we need to remove previous resource name + property.dataType = property.dataType.substring(property.dataType.indexOf(ApplicationConstants.DOT) + 1); + } + property.enumName = property.enumName == null ? property.dataType : property.enumName; + if (resourceName != null) { + // It will restrict the data type to be ResourceName.ResourceName.EnumName. + property.dataType = property.dataType != null && property.dataType.contains(resourceName + ApplicationConstants.DOT) + ? property.dataType: resourceName + ApplicationConstants.DOT + property.dataType; + } + + } + return property; + } + return property; + } + + +} diff --git a/src/main/java/com/twilio/oai/resolver/java/JavaConventionResolver.java b/src/main/java/com/twilio/oai/resolver/java/JavaConventionResolver.java index c992164a3..e9787c0bc 100644 --- a/src/main/java/com/twilio/oai/resolver/java/JavaConventionResolver.java +++ b/src/main/java/com/twilio/oai/resolver/java/JavaConventionResolver.java @@ -23,94 +23,4 @@ public class JavaConventionResolver { private ContainerResolver containerResolver = new ContainerResolver(Arrays.asList(EnumConstants.JavaDataTypes.values())); - /* - * Mustache Object (property or parameter) - * enum class name = enumName - * data type = datatype (apiName.enumName) - * variable name = name - */ - @SuppressWarnings("unchecked") - public CodegenParameter resolveEnumParameter(CodegenParameter parameter, String resourceName) { - if( parameter.isEnum && !parameter.vendorExtensions.containsKey(REF_ENUM_EXTENSION_NAME)) { - if (parameter.enumName.contains(ApplicationConstants.ENUM)) { - parameter.enumName = Utility.removeEnumName(parameter.enumName); - } - parameter.enumName = StringHelper.camelize(parameter.enumName); - if (parameter.items != null && parameter.items.allowableValues != null && parameter.items.allowableValues.containsKey(VALUES)) { - parameter.dataType = LIST_START + resourceName+"."+ parameter.enumName + LIST_END; - parameter.baseType = resourceName + "." + parameter.enumName; - } else { - parameter.dataType = resourceName + "." + parameter.enumName; - } - - return parameter; - } - if (parameter.items != null && parameter.items.allowableValues != null && parameter.items.allowableValues.containsKey(VALUES) ) { - parameter.isEnum = true; - parameter.enumName = parameter.baseType; - parameter._enum = (List) parameter.items.allowableValues.get(VALUES); - parameter.dataType = LIST_START + resourceName + "." + parameter.baseType + LIST_END; - parameter.baseType = resourceName + "." + parameter.baseType; - parameter.allowableValues = parameter.items.allowableValues; - } - if (parameter.allowableValues != null && parameter.allowableValues.containsKey(ENUM_VARS)) { - parameter.isEnum = true; - parameter._enum = (List) parameter.allowableValues.get(VALUES); - // Check if parameter enum is already resolved or not. - if (parameter.enumName == null || !parameter.dataType.startsWith(resourceName + ".")) { - // What if datatype is null ? - // remove resource name from datatype - parameter.enumName = parameter.dataType; - } - if (parameter.dataType == null || !parameter.dataType.startsWith(resourceName + "." )) { - parameter.dataType = resourceName + "." + parameter.dataType; - } - } - return parameter; - } - - public CodegenProperty resolveEnumProperty(CodegenProperty property, String resourceName) { - if(CodegenUtils.isPropertySchemaEnumJava(property)) { - // complexType contains the class name, dataType contains the data type for enum which prefixes class name. - // name contains variable name - // This is new gen enums - Stack containerTypes = new Stack<>(); - if (property.isContainer) { - property.dataType = containerResolver.unwrapContainerType(property, containerTypes); - if (property.dataType != null && property.dataType.contains(ApplicationConstants.DOT)) { - // If models is resolved more than twice then we need to remove previous resource name - property.dataType = property.dataType.substring(property.dataType.indexOf(ApplicationConstants.DOT) + 1); - } - property.enumName = property.enumName == null ? property.dataType : property.enumName; - property.dataType = property.dataType != null && property.dataType.contains(resourceName + ApplicationConstants.DOT) - ? property.dataType: resourceName + ApplicationConstants.DOT + property.dataType; - if (property.complexType.contains(ApplicationConstants.ENUM)) { - property.complexType = Utility.removeEnumName(property.complexType); - property.dataType = Utility.removeEnumName(property.dataType); - } - containerResolver.rewrapContainerType(property, containerTypes); - property.isEnum = true; - property.allowableValues = property.items.allowableValues; - property._enum = (List) property.items.allowableValues.get(VALUES); - } else { - if (property.complexType.contains(ApplicationConstants.ENUM)) { - property.complexType = Utility.removeEnumName(property.complexType); - property.dataType = Utility.removeEnumName(property.dataType); - } - if (property.dataType != null && property.dataType.contains(ApplicationConstants.DOT)) { - // If models is resolved more than twice then we need to remove previous resource name - property.dataType = property.dataType.substring(property.dataType.indexOf(ApplicationConstants.DOT) + 1); - } - property.enumName = property.enumName == null ? property.dataType : property.enumName; - if (resourceName != null) { - // It will restrict the data type to be ResourceName.ResourceName.EnumName. - property.dataType = property.dataType != null && property.dataType.contains(resourceName + ApplicationConstants.DOT) - ? property.dataType: resourceName + ApplicationConstants.DOT + property.dataType; - } - - } - return property; - } - return property; - } }