[Dart][Jaguar] and proto serialization support (#1793)

* add proto format support for jaguar generator

* Add openApiType to CodegenProperties
Finish proto generation
This commit is contained in:
Jaumard
2019-03-27 02:48:09 +01:00
committed by William Cheng
parent 09d27e82e7
commit 41b5d0e8fc
231 changed files with 20560 additions and 406 deletions

View File

@@ -20,7 +20,7 @@ package org.openapitools.codegen;
import java.util.*;
public class CodegenProperty implements Cloneable {
public String baseName, complexType, getter, setter, description, dataType,
public String openApiType, baseName, complexType, getter, setter, description, dataType,
datatypeWithEnum, dataFormat, name, min, max, defaultValue, defaultValueWithParam,
baseType, containerType, title;
@@ -415,6 +415,7 @@ public class CodegenProperty implements Cloneable {
return Objects.hash(
_enum,
allowableValues,
openApiType,
baseName,
baseType,
complexType,
@@ -501,6 +502,7 @@ public class CodegenProperty implements Cloneable {
final CodegenProperty other = (CodegenProperty) obj;
return Objects.equals(baseName, other.baseName) &&
Objects.equals(openApiType, other.openApiType) &&
Objects.equals(complexType, other.complexType) &&
Objects.equals(getter, other.getter) &&
Objects.equals(setter, other.setter) &&
@@ -600,6 +602,7 @@ public class CodegenProperty implements Cloneable {
public java.lang.String toString() {
return "CodegenProperty{" +
"baseName='" + baseName + '\'' +
", openApiType='" + openApiType + '\'' +
", complexType='" + complexType + '\'' +
", getter='" + getter + '\'' +
", setter='" + setter + '\'' +

View File

@@ -1900,6 +1900,11 @@ public class DefaultCodegen implements CodegenConfig {
CodegenProperty property = CodegenModelFactory.newInstance(CodegenModelType.PROPERTY);
property.name = toVarName(name);
property.baseName = name;
if (p.getType() == null) {
property.openApiType = getSchemaType(p);
} else {
property.openApiType = p.getType();
}
property.nameInCamelCase = camelize(property.name, false);
property.nameInSnakeCase = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, property.nameInCamelCase);
property.description = escapeText(p.getDescription());

View File

@@ -16,10 +16,16 @@
package org.openapitools.codegen.languages;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.io.FilenameUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils;
import io.swagger.v3.oas.models.media.*;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.utils.ProcessUtils;
import java.io.File;
import java.util.*;
@@ -28,16 +34,25 @@ import static org.openapitools.codegen.utils.StringUtils.underscore;
public class DartJaguarClientCodegen extends DartClientCodegen {
private static final String NULLABLE_FIELDS = "nullableFields";
private static final String SERIALIZATION_FORMAT = "serialization";
private static final String IS_FORMAT_JSON = "jsonFormat";
private static final String IS_FORMAT_PROTO = "protoFormat";
private static Set<String> modelToIgnore = new HashSet<>();
private HashMap<String, String> protoTypeMapping = new HashMap<>();
static {
modelToIgnore.add("datetime");
modelToIgnore.add("map");
modelToIgnore.add("list");
modelToIgnore.add("file");
modelToIgnore.add("uint8list");
}
private static final String SERIALIZATION_JSON = "json";
private static final String SERIALIZATION_PROTO = "proto";
private boolean nullableFields = true;
private String serialization = SERIALIZATION_JSON;
public DartJaguarClientCodegen() {
super();
@@ -46,6 +61,33 @@ public class DartJaguarClientCodegen extends DartClientCodegen {
embeddedTemplateDir = templateDir = "dart-jaguar";
cliOptions.add(new CliOption(NULLABLE_FIELDS, "Is the null fields should be in the JSON payload"));
cliOptions.add(new CliOption(SERIALIZATION_FORMAT, "Choose serialization format JSON or PROTO is supported"));
typeMapping.put("file", "Uint8List");
typeMapping.put("binary", "Uint8List");
protoTypeMapping.put("Array", "repeated");
protoTypeMapping.put("array", "repeated");
protoTypeMapping.put("List", "repeated");
protoTypeMapping.put("boolean", "bool");
protoTypeMapping.put("string", "string");
protoTypeMapping.put("char", "string");
protoTypeMapping.put("int", "int32");
protoTypeMapping.put("long", "int64");
protoTypeMapping.put("short", "int32");
protoTypeMapping.put("number", "double");
protoTypeMapping.put("float", "float");
protoTypeMapping.put("double", "double");
protoTypeMapping.put("object", "google.protobuf.Any");
protoTypeMapping.put("integer", "int32");
protoTypeMapping.put("Date", "google.protobuf.Timestamp");
protoTypeMapping.put("date", "google.protobuf.Timestamp");
protoTypeMapping.put("File", "bytes");
protoTypeMapping.put("file", "bytes");
protoTypeMapping.put("binary", "bytes");
protoTypeMapping.put("UUID", "string");
protoTypeMapping.put("ByteArray", "bytes");
}
@Override
@@ -77,6 +119,20 @@ public class DartJaguarClientCodegen extends DartClientCodegen {
additionalProperties.put(NULLABLE_FIELDS, nullableFields);
}
if (additionalProperties.containsKey(SERIALIZATION_FORMAT)) {
serialization = ((String) additionalProperties.get(SERIALIZATION_FORMAT));
boolean isProto = serialization.equalsIgnoreCase(SERIALIZATION_PROTO);
additionalProperties.put(IS_FORMAT_JSON, serialization.equalsIgnoreCase(SERIALIZATION_JSON));
additionalProperties.put(IS_FORMAT_PROTO, isProto);
modelTemplateFiles.put("model.mustache", isProto ? ".proto" : ".dart");
} else {
//not set, use to be passed to template
additionalProperties.put(IS_FORMAT_JSON, true);
additionalProperties.put(IS_FORMAT_PROTO, false);
}
if (additionalProperties.containsKey(PUB_NAME)) {
this.setPubName((String) additionalProperties.get(PUB_NAME));
} else {
@@ -133,6 +189,7 @@ public class DartJaguarClientCodegen extends DartClientCodegen {
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
objs = super.postProcessModels(objs);
List<Object> models = (List<Object>) objs.get("models");
ProcessUtils.addIndexToProperties(models, 1);
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
Set<String> modelImports = new HashSet<>();
@@ -142,6 +199,16 @@ public class DartJaguarClientCodegen extends DartClientCodegen {
modelImports.add(underscore(modelImport));
}
}
for (CodegenProperty p : cm.vars) {
String protoType = protoTypeMapping.get(p.openApiType);
if (p.isListContainer) {
String innerType = protoTypeMapping.get(p.mostInnerItems.openApiType);
protoType = protoType + " " + (innerType == null ? p.mostInnerItems.openApiType : innerType);
}
p.vendorExtensions.put("x-proto-type", protoType == null ? p.openApiType : protoType);
}
cm.imports = modelImports;
cm.vendorExtensions.put("hasVars", cm.vars.size() > 0);
}
@@ -155,17 +222,20 @@ public class DartJaguarClientCodegen extends DartClientCodegen {
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
Set<String> modelImports = new HashSet<>();
Set<String> fullImports = new HashSet<>();
for (CodegenOperation op : operationList) {
op.httpMethod = StringUtils.capitalize(op.httpMethod.toLowerCase(Locale.ROOT));
boolean isJson = true; //default to JSON
boolean isForm = false;
boolean isProto = false;
boolean isMultipart = false;
if (op.consumes != null) {
for (Map<String, String> consume : op.consumes) {
if (consume.containsKey("mediaType")) {
String type = consume.get("mediaType");
isJson = type.equalsIgnoreCase("application/json");
isProto = type.equalsIgnoreCase("application/octet-stream");
isForm = type.equalsIgnoreCase("application/x-www-form-urlencoded");
isMultipart = type.equalsIgnoreCase("multipart/form-data");
break;
@@ -173,7 +243,27 @@ public class DartJaguarClientCodegen extends DartClientCodegen {
}
}
for (CodegenParameter param : op.allParams) {
if (param.baseType != null && param.baseType.equalsIgnoreCase("Uint8List") && isMultipart) {
param.baseType = "MultipartFile";
param.dataType = "MultipartFile";
}
}
for (CodegenParameter param : op.formParams) {
if (param.baseType != null && param.baseType.equalsIgnoreCase("Uint8List") && isMultipart) {
param.baseType = "MultipartFile";
param.dataType = "MultipartFile";
}
}
for (CodegenParameter param : op.bodyParams) {
if (param.baseType != null && param.baseType.equalsIgnoreCase("Uint8List") && isMultipart) {
param.baseType = "MultipartFile";
param.dataType = "MultipartFile";
}
}
op.vendorExtensions.put("isJson", isJson);
op.vendorExtensions.put("isProto", isProto);
op.vendorExtensions.put("isForm", isForm);
op.vendorExtensions.put("isMultipart", isMultipart);
@@ -181,6 +271,8 @@ public class DartJaguarClientCodegen extends DartClientCodegen {
for (String item : op.imports) {
if (!modelToIgnore.contains(item.toLowerCase(Locale.ROOT))) {
imports.add(underscore(item));
} else if (item.equalsIgnoreCase("Uint8List")) {
fullImports.add("dart:typed_data");
}
}
modelImports.addAll(imports);
@@ -205,6 +297,7 @@ public class DartJaguarClientCodegen extends DartClientCodegen {
}
objs.put("modelImports", modelImports);
objs.put("fullImports", fullImports);
return objs;
}

View File

@@ -14,19 +14,20 @@ public class ProcessUtils {
* Add x-index extension to the model's properties
*
* @param models List of models
* @param initialIndex starting index to use
*/
public static void addIndexToProperties(List<Object> models) {
public static void addIndexToProperties(List<Object> models, int initialIndex) {
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model");
int i = 0;
int i = initialIndex;
for (CodegenProperty var : cm.vars) {
var.vendorExtensions.put("x-index", i);
i++;
}
int j = 0;
int j = initialIndex;
for (CodegenProperty var : cm.allVars) {
var.vendorExtensions.put("x-index", j);
j++;
@@ -36,6 +37,15 @@ public class ProcessUtils {
}
/**
* Add x-index extension to the model's properties
*
* @param models List of models
*/
public static void addIndexToProperties(List<Object> models) {
addIndexToProperties(models, 0);
}
/**
* Returns true if at least one operation has OAuth security schema defined
*