File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed
typescript-generator-core/src
main/java/cz/habarta/typescript/generator/emitter
test/java/cz/habarta/typescript/generator Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -117,7 +117,29 @@ private void emitProperty(TsPropertyModel property) {
117117 emitComments (property .getComments ());
118118 final TsType tsType = property .getTsType ();
119119 final String questionMark = settings .declarePropertiesAsOptional || (tsType instanceof TsType .OptionalType ) ? "?" : "" ;
120- writeIndentedLine (property .getName () + questionMark + ": " + tsType + ";" );
120+ writeIndentedLine (toPropertyName (property .getName ()) + questionMark + ": " + tsType + ";" );
121+ }
122+
123+ private String toPropertyName (String name ) {
124+ return isValidIdentifierName (name ) ? name : (settings .quotes + name + settings .quotes );
125+ }
126+
127+ // https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#2.2.2
128+ // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-names-and-keywords
129+ private static boolean isValidIdentifierName (String name ) {
130+ if (name == null || name .isEmpty ()) {
131+ return false ;
132+ }
133+ final char start = name .charAt (0 );
134+ if (!Character .isUnicodeIdentifierStart (start ) && start != '$' && start != '_' ) {
135+ return false ;
136+ }
137+ for (char c : name .substring (1 ).toCharArray ()) {
138+ if (!Character .isUnicodeIdentifierPart (c ) && c != '$' && c != '_' && c != '\u200C' && c != '\u200D' ) {
139+ return false ;
140+ }
141+ }
142+ return true ;
121143 }
122144
123145 private void emitTypeAliases (TsModel model , boolean exportKeyword ) {
Original file line number Diff line number Diff line change 1+
2+ package cz .habarta .typescript .generator ;
3+
4+ import com .fasterxml .jackson .annotation .JsonTypeInfo ;
5+ import org .junit .Assert ;
6+ import org .junit .Test ;
7+
8+
9+ public class Jackson2PolymorphismTest {
10+
11+ @ Test
12+ public void test () {
13+ final String output = new TypeScriptGenerator (TestUtils .settings ()).generateTypeScript (Input .from (BadFieldClass .class ));
14+ System .out .println (output );
15+ Assert .assertTrue (output .contains ("\" @class\" " ));
16+ }
17+
18+ @ JsonTypeInfo (use = JsonTypeInfo .Id .CLASS , include = JsonTypeInfo .As .PROPERTY , property = "@class" )
19+ public static interface BadFieldClass {
20+ }
21+
22+ }
You can’t perform that action at this time.
0 commit comments