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

Fix: Remove spec 3 references keys in imports for TypeScript #1932

Merged
merged 31 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
dd105e2
fix name sanitation when using kebab case filenaming
smasala Oct 25, 2018
b2029ee
remove whitespaces
smasala Oct 25, 2018
6474972
Merge branch 'master' of https://github.com/smasala/openapi-generator
smasala Nov 16, 2018
0d51a62
Merge branch 'master' of https://github.com/smasala/openapi-generator
smasala Dec 21, 2018
0f800db
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Jan 14, 2019
3105e17
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Jan 17, 2019
7e58719
sanitize names by removing spec 3 ref keys
smasala Jan 17, 2019
fd6f244
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Feb 4, 2019
3cdfd48
Merge branch 'master' into name-ref-fix
smasala Feb 4, 2019
c9cc783
Revert "sanitize names by removing spec 3 ref keys"
smasala Feb 4, 2019
bc94f03
add pipes to datatype names
smasala Feb 4, 2019
f7ebc44
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Mar 13, 2019
39f815a
Merge branch 'master' into name-ref-fix
smasala Mar 13, 2019
75f2c07
split imports
smasala Mar 14, 2019
91e7a1e
remove comment
smasala Mar 14, 2019
6cc6ea7
fix when using a mixture of (any|one) and standard imports
smasala Mar 28, 2019
3b0c60c
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Mar 28, 2019
9b3b9d0
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala May 21, 2019
d548564
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Jul 26, 2019
22ab490
sanitize names by removing spec 3 ref keys
smasala Jan 17, 2019
ed2d71b
Revert "sanitize names by removing spec 3 ref keys"
smasala Feb 4, 2019
815c58d
Merge conflict DefMerge branch 'master' into name-ref-fix
smasala Jul 26, 2019
c307a4b
split imports
smasala Mar 14, 2019
bbfe797
remove comment
smasala Mar 14, 2019
f680a2c
fix when using a mixture of (any|one) and standard imports
smasala Mar 28, 2019
3885f1f
Merge branch 'name-ref-fix' of https://github.com/smasala/openapi-gen…
smasala Jul 26, 2019
4dabb56
Fix merge error
smasala Jul 26, 2019
87303b1
format
smasala Jul 26, 2019
080113f
fix tests
smasala Jul 26, 2019
4978024
create test, fi regex
smasala Jul 26, 2019
a774df2
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Jul 29, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -4017,6 +4017,19 @@ public TemplatingEngineAdapter getTemplatingEngine() {
* @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
Expand All @@ -4034,27 +4047,27 @@ public String sanitizeName(String name, String removeCharRegEx) {
}

// 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
Expand All @@ -4072,6 +4085,13 @@ public String sanitizeName(String name, String removeCharRegEx) {
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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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;
Expand Down Expand Up @@ -266,7 +267,8 @@ private boolean propertyHasBreakingCharacters(String str) {

@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;
Expand Down Expand Up @@ -696,4 +698,14 @@ public void postProcessFile(File file, String fileType) {
}
}
}

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

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.*;
Expand Down Expand Up @@ -458,12 +459,33 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
}
}
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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;
Expand Down Expand Up @@ -109,4 +111,25 @@ public void testRemovePrefixSuffix() {
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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void setUp() {

@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");
Expand All @@ -30,6 +31,7 @@ public void convertVarName() throws Exception {
@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);
Expand All @@ -52,6 +54,7 @@ public void testSnapshotVersion() {
@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);
Expand Down