swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.08k stars 6.03k forks source link

[JavaScript][ES6]Special characters in model break integration test #5978

Open CodeNinjai opened 7 years ago

CodeNinjai commented 7 years ago

Description

The Petstore integration tests for JS ES6 failed

SyntaxError: swagger-codegen/samples/client/petstore/javascript-promise-es6/src/model/EnumClass.js: Unexpected token (36:8)                                                                                                                          
  34 |          * @const                                                                                                                 
  35 |          */                                                                                                                       
  36 |         -efg = "-efg";                                                                                                            
     |         ^                                                                                                                         
  37 |                                                                                                                                   
  38 |                                                                                                                                   
  39 |         /**                    
SyntaxError: swagger-codegen/samples/client/petstore/javascript-promise-es6/src/model/EnumClass.js: Unexpected token (43:8)                                                                                                                          
  41 |          * @const                                                                                                                 
  42 |          */                                                                                                                       
> 43 |         (xyz) = "(xyz)";                                                                                                          
     |         ^                                                                                                                         
  44 |                                                                                                                                   
  45 |       
SyntaxError: swagger-codegen/samples/client/petstore/javascript-promise-es6/src/model/List.js: Unexpected token (69:7)                                                                                                                               
  67 |     * @member {String} 123-list                                                                                                   
  68 |     */                                                                                                                            
> 69 |     123-list = undefined;                                                                                                         
     |        ^                                                                                                                          
  70 |                                                                                                                                   
  71 |                                                                                                                                   
  72 |                                                                                                                                   
SyntaxError: swagger-codegen/samples/client/petstore/javascript-promise-es6/src/model/Name.js: Identifier directly after number (92:7)                                                                                                               
  90 |     * @member {Number} 123Number                                                                                                  
  91 |     */                                                                                                                            
> 92 |     123Number = undefined;                                                                                                        
     |        ^                                                                                                                          
  93 |                                                                                                                                   
  94 |                                                                                                                                   
  95 |                                                                                                                                   
SyntaxError: swagger-codegen/samples/client/petstore/javascript-promise-es6/src/model/SpecialModelName.js: Unexpected token (69:12)                                                                                                                  
  67 |     * @member {Number} $special[property.name]                                                                                    
  68 |     */                                                                                                                            
> 69 |     $special[property.name] = undefined;                                                                                          
     |             ^                                                                                                                     
  70 |                                                                                                                                   

Swagger-codegen version

Latest master.

Steps to reproduce

To repeat the issue for javascript-promise-es6

cd samples/client/petstore/javascript-promise-es6 npm install npm test

To repeat the issue for javascript-es6

cd samples/client/petstore/javascript-es6 npm install npm test

CodeNinjai commented 7 years ago

I'll have a look on those issues.

CodeNinjai commented 7 years ago

I'll need some advice:

I changed behaviour in io/swagger/codegen/languages/JavascriptClientCodegen.java Line 488 toVarName(String name)

and added Logger output.

When generating swagger-codegen/samples/client/petstore/javascript-es6/src/model/List.js I can see that 123-List will be renamed to _123List:

[main] INFO io.swagger.codegen.languages.JavascriptClientCodegen - JL: toVarName() = 123-list
[main] INFO io.swagger.codegen.languages.JavascriptClientCodegen - JL _AFTER: toVarName() = _123_list
[main] INFO io.swagger.codegen.languages.JavascriptClientCodegen - JL _FINAL: toVarName() = _123List
[main] INFO io.swagger.codegen.languages.JavascriptClientCodegen - JL: toVarName() = 123-list
[main] INFO io.swagger.codegen.languages.JavascriptClientCodegen - JL _AFTER: toVarName() = _123_list
[main] INFO io.swagger.codegen.languages.JavascriptClientCodegen - JL _FINAL: toVarName() = _123List
[main] INFO io.swagger.codegen.languages.JavascriptClientCodegen - JL: toVarName() = 123-list
[main] INFO io.swagger.codegen.languages.JavascriptClientCodegen - JL _AFTER: toVarName() = _123_list
[main] INFO io.swagger.codegen.languages.JavascriptClientCodegen - JL _FINAL: toVarName() = _123List

But still the List.js will be generated like this:

export default class List {                                                                                                        
    /**                                                                                                                            
    * Constructs a new <code>List</code>.                                                                                          
    * @alias module:model/List                                                                                                     
    * @class                                                                                                                       
    */                                                                                                                             

    constructor() {                                                                                                                
    }                                                                                                                              

    /**                                                                                                                            
    * Constructs a <code>List</code> from a plain JavaScript object, optionally creating a new instance.                           
    * Copies all relevant properties from <code>data</code> to <code>obj</code> if supplied or a new instance if not.              
    * @param {Object} data The plain JavaScript object bearing properties of interest.                                             
    * @param {module:model/List} obj Optional instance to populate.                                                                
    * @return {module:model/List} The populated <code>List</code> instance.                                                        
    */                                                                                                                             
    static constructFromObject(data, obj) {                                                                                        
        if (data) {                                                                                                                
            obj = obj || new List();                                                                                               
            if (data.hasOwnProperty('123-list')) {                                                                                 
                obj['123-list'] = ApiClient.convertToType(data['123-list'], 'String');                                             
            }                                                                                                                      
        }                                                                                                                          
        return obj;                                                                                                                
    }                                                                                                                              

    /**                                                                                                                            
    * @member {String} 123-list                                                                                                    
    */                                                                                                                             
    123-list = undefined;                                                                                                                                                                                                                                             
}                                                                                                                                  

Any ideas where to look?

For the enum gen the fix worked out straight forward.

@wing328 @frol

wing328 commented 7 years ago

@CodeNinjai can you please submit a PR so that we can more easily review the code and clone locally to to repeat the issue?

CodeNinjai commented 7 years ago

@wing328 Sure, stand by.

wing328 commented 7 years ago

@CodeNinjai when you've time, please file the PR so that we can help review the change.