mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-06-29 04:00:51 +00:00
Fix: Remove spec 3 references keys in imports for TypeScript (#1932)
* fix name sanitation when using kebab case filenaming * remove whitespaces * sanitize names by removing spec 3 ref keys * Revert "sanitize names by removing spec 3 ref keys" This reverts commit 7e58719317bb936884f5ed0049177ef961871464. * add pipes to datatype names * split imports * remove comment * fix when using a mixture of (any|one) and standard imports * sanitize names by removing spec 3 ref keys * Revert "sanitize names by removing spec 3 ref keys" This reverts commit 7e58719317bb936884f5ed0049177ef961871464. * Merge conflict DefMerge branch 'master' into name-ref-fix # Conflicts: # modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java * split imports * remove comment * fix when using a mixture of (any|one) and standard imports * Fix merge error import missing package, merge error. * format * fix tests * create test, fi regex
This commit is contained in:
parent
cf01bf4ae7
commit
b4f1581941
@ -4017,6 +4017,19 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
* @return sanitized string
|
||||
*/
|
||||
public String sanitizeName(String name, String removeCharRegEx) {
|
||||
return sanitizeName(name, removeCharRegEx, new ArrayList<String>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize name (parameter, property, method, etc)
|
||||
*
|
||||
* @param name string to be sanitize
|
||||
* @param removeCharRegEx a regex containing all char that will be removed
|
||||
* @param exceptionList a list of matches which should not be sanitized (i.e expections)
|
||||
* @return sanitized string
|
||||
*/
|
||||
@SuppressWarnings("static-method")
|
||||
public String sanitizeName(String name, String removeCharRegEx, ArrayList<String> exceptionList) {
|
||||
// NOTE: performance wise, we should have written with 2 replaceAll to replace desired
|
||||
// character with _ or empty character. Below aims to spell out different cases we've
|
||||
// encountered so far and hopefully make it easier for others to add more special
|
||||
@ -4034,27 +4047,27 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
|
||||
// input[] => input
|
||||
name = name.replaceAll("\\[\\]", ""); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
|
||||
name = this.sanitizeValue(name, "\\[\\]", "", exceptionList);
|
||||
|
||||
// input[a][b] => input_a_b
|
||||
name = name.replaceAll("\\[", "_");
|
||||
name = name.replaceAll("\\]", "");
|
||||
name = this.sanitizeValue(name, "\\[", "_", exceptionList);
|
||||
name = this.sanitizeValue(name, "\\]", "", exceptionList);
|
||||
|
||||
// input(a)(b) => input_a_b
|
||||
name = name.replaceAll("\\(", "_");
|
||||
name = name.replaceAll("\\)", "");
|
||||
name = this.sanitizeValue(name, "\\(", "_", exceptionList);
|
||||
name = this.sanitizeValue(name, "\\)", "", exceptionList);
|
||||
|
||||
// input.name => input_name
|
||||
name = name.replaceAll("\\.", "_");
|
||||
name = this.sanitizeValue(name, "\\.", "_", exceptionList);
|
||||
|
||||
// input-name => input_name
|
||||
name = name.replaceAll("-", "_");
|
||||
name = this.sanitizeValue(name, "-", "_", exceptionList);
|
||||
|
||||
// a|b => a_b
|
||||
name = name.replace("|", "_");
|
||||
name = this.sanitizeValue(name, "\\|", "_", exceptionList);
|
||||
|
||||
// input name and age => input_name_and_age
|
||||
name = name.replaceAll(" ", "_");
|
||||
name = this.sanitizeValue(name, " ", "_", exceptionList);
|
||||
|
||||
// /api/films/get => _api_films_get
|
||||
// \api\films\get => _api_films_get
|
||||
@ -4072,6 +4085,13 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
return name;
|
||||
}
|
||||
|
||||
private String sanitizeValue(String value, String replaceMatch, String replaceValue, ArrayList<String> exceptionList) {
|
||||
if (exceptionList.size() == 0 || !exceptionList.contains(replaceMatch)) {
|
||||
return value.replaceAll(replaceMatch, replaceValue);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize tag
|
||||
*
|
||||
|
@ -19,6 +19,7 @@ package org.openapitools.codegen.languages;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.media.ArraySchema;
|
||||
import io.swagger.v3.oas.models.media.ComposedSchema;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import io.swagger.v3.oas.models.parameters.Parameter;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
@ -266,7 +267,8 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
|
||||
@Override
|
||||
public String toModelName(String name) {
|
||||
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
|
||||
ArrayList<String> exceptions = new ArrayList<String>(Arrays.asList("\\|", " "));
|
||||
name = sanitizeName(name, "(?![| ])\\W", exceptions); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
|
||||
|
||||
if (!StringUtils.isEmpty(modelNamePrefix)) {
|
||||
name = modelNamePrefix + "_" + name;
|
||||
@ -696,4 +698,14 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toAnyOfName(List<String> names, ComposedSchema composedSchema) {
|
||||
return String.join(" | ", names);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toOneOfName(List<String> names, ComposedSchema composedSchema) {
|
||||
return String.join(" | ", names);
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.capitalize;
|
||||
import static org.openapitools.codegen.utils.StringUtils.*;
|
||||
@ -458,12 +459,33 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
}
|
||||
}
|
||||
// Add additional filename information for imports
|
||||
mo.put("tsImports", toTsImports(cm, cm.imports));
|
||||
Set<String> parsedImports = parseImports(cm);
|
||||
mo.put("tsImports", toTsImports(cm, parsedImports));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse imports
|
||||
*/
|
||||
private Set<String> parseImports(CodegenModel cm) {
|
||||
Set<String> newImports = new HashSet<String>();
|
||||
if (cm.imports.size() > 0) {
|
||||
for (String name : cm.imports) {
|
||||
if (name.indexOf(" | ") >= 0) {
|
||||
String[] parts = name.split(" \\| ");
|
||||
for (String s : parts) {
|
||||
newImports.add(s);
|
||||
}
|
||||
} else {
|
||||
newImports.add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
return newImports;
|
||||
}
|
||||
|
||||
private List<Map<String, String>> toTsImports(CodegenModel cm, Set<String> imports) {
|
||||
List<Map<String, String>> tsImports = new ArrayList<>();
|
||||
for (String im : imports) {
|
||||
|
@ -3,6 +3,8 @@ package org.openapitools.codegen.typescript.typescriptangular;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.Operation;
|
||||
import io.swagger.v3.oas.models.PathItem;
|
||||
import io.swagger.v3.oas.models.media.ComposedSchema;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import io.swagger.v3.oas.models.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.models.responses.ApiResponses;
|
||||
import org.openapitools.codegen.CodegenOperation;
|
||||
@ -109,4 +111,25 @@ public class TypeScriptAngularClientCodegenTest {
|
||||
Assert.assertEquals("TestName", codegen.removeModelPrefixSuffix("TestNameDefGhi"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchema() {
|
||||
TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen();
|
||||
|
||||
ComposedSchema composedSchema = new ComposedSchema();
|
||||
|
||||
Schema<Object> schema1 = new Schema<>();
|
||||
schema1.set$ref("SchemaOne");
|
||||
Schema<Object> schema2 = new Schema<>();
|
||||
schema2.set$ref("SchemaTwo");
|
||||
Schema<Object> schema3 = new Schema<>();
|
||||
schema3.set$ref("SchemaThree");
|
||||
|
||||
composedSchema.addAnyOfItem(schema1);
|
||||
composedSchema.addAnyOfItem(schema2);
|
||||
composedSchema.addAnyOfItem(schema3);
|
||||
|
||||
String schemaType = codegen.getSchemaType(composedSchema);
|
||||
Assert.assertEquals(schemaType, "SchemaOne | SchemaTwo | SchemaThree");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ public class TypeScriptNodeClientCodegenTest {
|
||||
|
||||
@Test
|
||||
public void convertVarName() throws Exception {
|
||||
TypeScriptNodeClientCodegen codegen = new TypeScriptNodeClientCodegen();
|
||||
Assert.assertEquals(codegen.toVarName("name"), "name");
|
||||
Assert.assertEquals(codegen.toVarName("$name"), "$name");
|
||||
Assert.assertEquals(codegen.toVarName("nam$$e"), "nam$$e");
|
||||
@ -30,6 +31,7 @@ public class TypeScriptNodeClientCodegenTest {
|
||||
@Test
|
||||
public void testSnapshotVersion() {
|
||||
OpenAPI api = TestUtils.createOpenAPI();
|
||||
TypeScriptNodeClientCodegen codegen = new TypeScriptNodeClientCodegen();
|
||||
|
||||
codegen.additionalProperties().put("npmName", "@openapi/typescript-angular-petstore");
|
||||
codegen.additionalProperties().put("snapshot", true);
|
||||
@ -52,6 +54,7 @@ public class TypeScriptNodeClientCodegenTest {
|
||||
@Test
|
||||
public void testWithoutSnapshotVersion() {
|
||||
OpenAPI api = TestUtils.createOpenAPI();
|
||||
TypeScriptNodeClientCodegen codegen = new TypeScriptNodeClientCodegen();
|
||||
|
||||
codegen.additionalProperties().put("npmName", "@openapi/typescript-angular-petstore");
|
||||
codegen.additionalProperties().put("snapshot", false);
|
||||
|
Loading…
x
Reference in New Issue
Block a user