Skip to content

Commit

Permalink
Merge branch 'master' into enable-kotlin-client-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoMario authored Jun 30, 2021
2 parents 7f85e59 + a2ffea4 commit 8f55f7e
Show file tree
Hide file tree
Showing 63 changed files with 2,461 additions and 116 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-generators</artifactId>
<version>1.0.26-SNAPSHOT</version>
<version>1.0.28-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
Expand Down Expand Up @@ -252,10 +252,10 @@
</dependency>
</dependencies>
<properties>
<swagger-codegen-version>3.0.26-SNAPSHOT</swagger-codegen-version>
<swagger-parser-version>2.0.23</swagger-parser-version>
<swagger-core-version>2.1.4</swagger-core-version>
<jackson-version>2.11.4</jackson-version>
<swagger-codegen-version>3.0.28-SNAPSHOT</swagger-codegen-version>
<swagger-parser-version>2.0.27</swagger-parser-version>
<swagger-core-version>2.1.10</swagger-core-version>
<jackson-version>2.12.1</jackson-version>
<scala-version>2.11.1</scala-version>
<felix-version>3.3.0</felix-version>
<commons-io-version>2.4</commons-io-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,21 +396,25 @@ private String findEnumName(int truncateIdx, Object value) {
}

/**
* Returns the common prefix of variables for enum naming
* Returns the common prefix of variables for enum naming if
* two or more variables are present.
*
* @param vars List of variable names
* @return the common prefix for naming
*/
public String findCommonPrefixOfVars(List<Object> vars) {
try {
String[] listStr = vars.toArray(new String[vars.size()]);
String prefix = StringUtils.getCommonPrefix(listStr);
// exclude trailing characters that should be part of a valid variable
// e.g. ["status-on", "status-off"] => "status-" (not "status-o")
return prefix.replaceAll("[a-zA-Z0-9]+\\z", "");
} catch (ArrayStoreException e) {
return "";
if (vars.size() > 1) {
try {
String[] listStr = vars.toArray(new String[vars.size()]);
String prefix = StringUtils.getCommonPrefix(listStr);
// exclude trailing characters that should be part of a valid variable
// e.g. ["status-on", "status-off"] => "status-" (not "status-o")
return prefix.replaceAll("[a-zA-Z0-9]+\\z", "");
} catch (ArrayStoreException e) {
// do nothing, just return default value
}
}
return "";
}

/**
Expand Down Expand Up @@ -2235,11 +2239,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
}
}

for (String i : imports) {
if (needToImport(i)) {
codegenOperation.imports.add(i);
}
}
addOperationImports(codegenOperation, imports);

codegenOperation.bodyParam = bodyParam;
codegenOperation.httpMethod = httpMethod.toUpperCase();
Expand Down Expand Up @@ -2293,6 +2293,14 @@ public int compare(CodegenParameter one, CodegenParameter another) {
return codegenOperation;
}

protected void addOperationImports(CodegenOperation codegenOperation, Set<String> operationImports) {
for (String operationImport : operationImports) {
if (needToImport(operationImport)) {
codegenOperation.imports.add(operationImport);
}
}
}

/**
* Convert Swagger Response object to Codegen Response object
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ protected CodegenModel processArrayItemSchema(String codegenModelName, CodegenPr
return null;
}
this.updatePropertyDataType(codegenProperty, composedModel.name, arraySchema);
this.updatePropertyDataType(codegenProperty.items, composedModel);
return composedModel;
}
return null;
Expand Down Expand Up @@ -208,6 +209,7 @@ protected void updatePropertyDataType(CodegenProperty codegenProperty, String sc
arraySchema.setItems(refSchema);
codegenProperty.setDatatype(this.codegenConfig.getTypeDeclaration(arraySchema));
codegenProperty.setDatatypeWithEnum(codegenProperty.getDatatype());
codegenProperty.vendorExtensions.put("x-is-composed", true);

codegenProperty.defaultValue = this.codegenConfig.toDefaultValue(arraySchema);
codegenProperty.defaultValueWithParam = this.codegenConfig.toDefaultValueWithParam(codegenProperty.baseName, arraySchema);
Expand Down Expand Up @@ -235,5 +237,6 @@ private void updatePropertyDataType(CodegenProperty codegenProperty, CodegenMode
codegenProperty.datatypeWithEnum = composedModel.getClassname();
codegenProperty.baseType = composedModel.getClassname();
codegenProperty.complexType = composedModel.getClassname();
codegenProperty.vendorExtensions.put("x-is-composed", true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ private Object resolveSchemaToExample(String propertyName, String mediaType, Sch
Schema innerType = ((ArraySchema) schema).getItems();
if (innerType != null) {
int arrayLength = schema.getMaxItems() != null ? schema.getMaxItems() : 2;
if (arrayLength > 10) {
logger.warn("value of maxItems of property {} is {}; limiting to 10 examples", schema, arrayLength);
arrayLength = 10;
}
Object[] objectProperties = new Object[arrayLength];
Object objProperty = resolveSchemaToExample(propertyName, mediaType, innerType, processedModels);
for(int i=0; i < arrayLength; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,16 @@ public void processOpts() {
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
} else if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
// guess from api package
String derviedInvokerPackage = deriveInvokerPackageName((String)additionalProperties.get(CodegenConstants.API_PACKAGE));
this.additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, derviedInvokerPackage);
String derivedInvokerPackage = deriveInvokerPackageName((String)additionalProperties.get(CodegenConstants.API_PACKAGE));
this.additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, derivedInvokerPackage);
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
LOGGER.info("Invoker Package Name, originally not set, is now dervied from api package name: " + derviedInvokerPackage);
LOGGER.info("Invoker Package Name, originally not set, is now derived from api package name: " + derivedInvokerPackage);
} else if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
// guess from model package
String derviedInvokerPackage = deriveInvokerPackageName((String)additionalProperties.get(CodegenConstants.MODEL_PACKAGE));
this.additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, derviedInvokerPackage);
String derivedInvokerPackage = deriveInvokerPackageName((String)additionalProperties.get(CodegenConstants.MODEL_PACKAGE));
this.additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, derivedInvokerPackage);
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
LOGGER.info("Invoker Package Name, originally not set, is now dervied from model package name: " + derviedInvokerPackage);
LOGGER.info("Invoker Package Name, originally not set, is now derived from model package name: " + derivedInvokerPackage);
} else if (StringUtils.isNotEmpty(invokerPackage)) {
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
Expand Down Expand Up @@ -492,6 +492,15 @@ private void sanitizeConfig() {
}
}

protected String escapeUnderscore(String name) {
// Java 8 discourages naming things _, but Java 9 does not allow it.
if("_".equals(name)) {
return "_u";
} else {
return name;
}
}

@Override
public String escapeReservedWord(String name) {
if(this.reservedWordsMappings().containsKey(name)) {
Expand Down Expand Up @@ -562,11 +571,9 @@ public String toVarName(String name) {
return "propertyClass";
}

if("_".equals(name)) {
name = "_u";
}
name = escapeUnderscore(name);

// if it's all uppper case, do nothing
// if it's all upper case, do nothing
if (name.matches("^[A-Z_]*$")) {
return name;
}
Expand Down Expand Up @@ -1233,15 +1240,6 @@ protected void replaceDuplicatedInModelProperties(Map<String, Schema> definition
schema.set$ref(schema.get$ref().replace(modelName, newModelName));
});
}
/*
@Override
public String findCommonPrefixOfVars(List<String> vars) {
String prefix = StringUtils.getCommonPrefix(vars.toArray(new String[vars.size()]));
// exclude trailing characters that should be part of a valid variable
// e.g. ["status-on", "status-off"] => "status-" (not "status-o")
return prefix.replaceAll("[a-zA-Z0-9]+\\z", "");
}
*/

@Override
public String toEnumName(CodegenProperty property) {
Expand Down Expand Up @@ -1273,7 +1271,7 @@ public String toEnumVarName(String value, String datatype) {
if (var.matches("\\d.*")) {
return "_" + var;
} else {
return var;
return escapeUnderscore(var).toUpperCase();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,11 @@ protected List<Map<String, Object>> modelInheritanceSupport(List<?> allModels) {
Map<String, Object> parent = new HashMap<>();
parent.put("classname", parentModel.classname);
List<CodegenModel> childrenModels = byParent.get(parentModel);

if (childrenModels == null || childrenModels.isEmpty()) {
continue;
}

for (CodegenModel model : childrenModels) {
Map<String, Object> child = new HashMap<>();
child.put("name", model.name);
Expand Down
Loading

0 comments on commit 8f55f7e

Please sign in to comment.