Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding data type resolver for java #578

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/main/java/com/twilio/oai/JsonRequestBodyResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -32,7 +32,6 @@ public void setResourceName(String resourceName) {
public JsonRequestBodyResolver(ApiResourceBuilder apiResourceBuilder, final Resolver<CodegenProperty> codegenPropertyResolver) {
this.codegenPropertyResolver = codegenPropertyResolver;
this.apiResourceBuilder = apiResourceBuilder;
this.conventionResolver = new JavaConventionResolver();
}

public void resolve(final CodegenParameter codegenParameter, final Resolver<CodegenParameter> codegenParameterResolver) {
Expand All @@ -42,7 +41,7 @@ public void resolve(final CodegenParameter codegenParameter, final Resolver<Code
final CodegenModel model = apiResourceBuilder.getModel(codegenParameter.dataType);
containerResolver.rewrapContainerType(codegenParameter, containerTypes);
if (CodegenUtils.isParameterSchemaEnumJava(codegenParameter)) {
conventionResolver.resolveEnumParameter(codegenParameter, resourceName);
javaCodegenParameterDataTypeResolver.resolveEnumParameter(codegenParameter, resourceName);
((JavaApiResourceBuilder)apiResourceBuilder).addEnums(codegenParameter);
} else if(model == null) {
// If parameter is not a model.
Expand All @@ -68,7 +67,7 @@ public void resolve(final CodegenProperty property) {
containerTypes.clear();

if (CodegenUtils.isPropertySchemaEnumJava(property)) {
conventionResolver.resolveEnumProperty(property, resourceName);
javaCodegenParameterDataTypeResolver.resolveEnumProperty(property, resourceName);
((JavaApiResourceBuilder)apiResourceBuilder).addEnums(property);
} else if (model == null) {
codegenPropertyResolver.resolve(property, apiResourceBuilder);
Expand Down
45 changes: 5 additions & 40 deletions src/main/java/com/twilio/oai/TwilioJavaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationsMap;
import com.twilio.oai.resolver.java.JavaCodegenParameterDataTypeResolver;

import java.util.*;

Expand All @@ -45,6 +46,8 @@ public class TwilioJavaGenerator extends JavaClientCodegen {
private final IConventionMapper conventionMapper = new LanguageConventionResolver(CONFIG_JAVA_JSON_PATH);
private final List<CodegenModel> allModels = new ArrayList<>();

private JavaCodegenParameterDataTypeResolver javaCodegenParameterDataTypeResolver = new JavaCodegenParameterDataTypeResolver(conventionMapper);

public TwilioJavaGenerator() {
super();
twilioCodegen = new TwilioCodegenAdapter(this, getName());
Expand Down Expand Up @@ -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<String>) 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<String>) property.allowableValues.get(VALUES);
} else if (property.isEnum) {
property.enumName = property.baseName;
}
property.isEnum = property.isEnum && property.dataFormat == null;
javaCodegenParameterDataTypeResolver.postProcessModelEnumProperty(property);
}

@Override
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/com/twilio/oai/api/JavaApiResourceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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{

Expand All @@ -31,7 +31,7 @@ public class JavaApiResourceBuilder extends ApiResourceBuilder{
protected long serialVersionUID;
private Set<CodegenModel> headerParamModelList;

private final JavaConventionResolver conventionResolver;
private final JavaCodegenParameterDataTypeResolver javaCodegenParameterDataTypeResolver = new JavaCodegenParameterDataTypeResolver(null);

private Resolver<CodegenProperty> codegenPropertyIResolver;

Expand All @@ -42,7 +42,6 @@ public class JavaApiResourceBuilder extends ApiResourceBuilder{
public JavaApiResourceBuilder(IApiActionTemplate template, List<CodegenOperation> codegenOperations,
List<CodegenModel> allModels) {
super(template, codegenOperations, allModels);
this.conventionResolver = new JavaConventionResolver();
}

public JavaApiResourceBuilder(IApiActionTemplate apiActionTemplate, List<CodegenOperation> opList,
Expand Down Expand Up @@ -82,7 +81,7 @@ public ApiResourceBuilder updateOperations(Resolver<CodegenParameter> 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);
Expand All @@ -99,36 +98,36 @@ public ApiResourceBuilder updateOperations(Resolver<CodegenParameter> 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();
Expand Down
Loading
Loading