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: allow colons in TS interface property names #1152

Merged
merged 9 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -29,6 +29,8 @@

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen implements CodegenConfig {
Expand Down Expand Up @@ -156,12 +158,6 @@ public String modelFileFolder() {

@Override
public String toParamName(String name) {
// should be the same as variable name
return toVarName(name);
}

@Override
public String toVarName(String name) {
// sanitize name
name = sanitizeName(name, "\\W-[\\$]");

Expand All @@ -184,6 +180,33 @@ public String toVarName(String 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
public String toModelName(String name) {
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{ {{>m
* {{{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}}
}{{>modelGenericEnums}}