Skip to content

Commit 1916025

Browse files
smasalawing328
authored andcommitted
Fix: allow colons in TS interface property names (#1152)
* Allow colons in interface property names: #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
1 parent 4e88442 commit 1916025

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
import java.io.File;
3131
import java.util.*;
32+
import java.util.regex.Matcher;
33+
import java.util.regex.Pattern;
3234

3335

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

157159
@Override
158160
public String toParamName(String name) {
159-
// should be the same as variable name
160-
return toVarName(name);
161-
}
162-
163-
@Override
164-
public String toVarName(String name) {
165161
// sanitize name
166162
name = sanitizeName(name, "\\W-[\\$]");
167163

@@ -184,6 +180,33 @@ public String toVarName(String name) {
184180
return name;
185181
}
186182

183+
@Override
184+
public String toVarName(String name) {
185+
name = this.toParamName(name);
186+
187+
// if the proprty name has any breaking characters such as :, ;, . etc.
188+
// then wrap the name within single quotes.
189+
// my:interface:property: string; => 'my:interface:property': string;
190+
if (propertyHasBreakingCharacters(name)) {
191+
name = "\'" + name + "\'";
192+
}
193+
194+
return name;
195+
}
196+
197+
/**
198+
* Checks whether property names have breaking characters like ':', '-'.
199+
* @param str string to check for breaking characters
200+
* @return <code>true</code> if breaking characters are present and <code>false</code> if not
201+
*/
202+
private boolean propertyHasBreakingCharacters(String str) {
203+
final String regex = "^.*[+*:;,.()-]+.*$";
204+
final Pattern pattern = Pattern.compile(regex);
205+
final Matcher matcher = pattern.matcher(str);
206+
boolean matches = matcher.matches();
207+
return matches;
208+
}
209+
187210
@Override
188211
public String toModelName(String name) {
189212
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.

modules/openapi-generator/src/main/resources/typescript-angular/modelGeneric.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{ {{>m
55
* {{{description}}}
66
*/
77
{{/description}}
8-
{{#isReadOnly}}readonly {{/isReadOnly}}{{name}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
8+
{{#isReadOnly}}readonly {{/isReadOnly}}{{{name}}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
99
{{/vars}}
1010
}{{>modelGenericEnums}}

0 commit comments

Comments
 (0)