Fix: allow colons in TS interface property names (#1152)

* Allow colons in interface property names: https://github.com/OpenAPITools/openapi-generator/issues/1080

* replace tabs with spaces

* add docs

* add example in doc

* update docs

* update docs

* remove language specific docs in DefaultCodegen

* Delete addPet-BodyParams.csv

* remove toPropertyName and update toVarName instead for TS
This commit is contained in:
Steven Masala 2018-10-03 09:56:58 +02:00 committed by William Cheng
parent 4e8844273b
commit 1916025a53
2 changed files with 30 additions and 7 deletions

View File

@ -29,6 +29,8 @@ import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen implements CodegenConfig { public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen implements CodegenConfig {
@ -156,12 +158,6 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
@Override @Override
public String toParamName(String name) { public String toParamName(String name) {
// should be the same as variable name
return toVarName(name);
}
@Override
public String toVarName(String name) {
// sanitize name // sanitize name
name = sanitizeName(name, "\\W-[\\$]"); name = sanitizeName(name, "\\W-[\\$]");
@ -184,6 +180,33 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
return name; return name;
} }
@Override
public String toVarName(String name) {
name = this.toParamName(name);
// if the proprty name has any breaking characters such as :, ;, . etc.
// then wrap the name within single quotes.
// my:interface:property: string; => 'my:interface:property': string;
if (propertyHasBreakingCharacters(name)) {
name = "\'" + name + "\'";
}
return name;
}
/**
* Checks whether property names have breaking characters like ':', '-'.
* @param str string to check for breaking characters
* @return <code>true</code> if breaking characters are present and <code>false</code> if not
*/
private boolean propertyHasBreakingCharacters(String str) {
final String regex = "^.*[+*:;,.()-]+.*$";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(str);
boolean matches = matcher.matches();
return matches;
}
@Override @Override
public String toModelName(String name) { public String toModelName(String name) {
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'. name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.

View File

@ -5,6 +5,6 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{ {{>m
* {{{description}}} * {{{description}}}
*/ */
{{/description}} {{/description}}
{{#isReadOnly}}readonly {{/isReadOnly}}{{name}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}; {{#isReadOnly}}readonly {{/isReadOnly}}{{{name}}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
{{/vars}} {{/vars}}
}{{>modelGenericEnums}} }{{>modelGenericEnums}}