diff --git a/modules/openapi-generator/src/main/resources/Javascript/partial_model_oneof.mustache b/modules/openapi-generator/src/main/resources/Javascript/partial_model_oneof.mustache index 4a4e5982f11..307d265e983 100644 --- a/modules/openapi-generator/src/main/resources/Javascript/partial_model_oneof.mustache +++ b/modules/openapi-generator/src/main/resources/Javascript/partial_model_oneof.mustache @@ -249,6 +249,16 @@ class {{classname}} { return this.getActualInstance(); } + {{#emitJSDoc}} + /** + * Create an instance of {{classname}} from a JSON string. + * @param {string} json_string JSON string. + * @return {{=< >=}}{module:<#invokerPackage>/<#modelPackage>/<&classname>}<={{ }}=> An instance of {{classname}}. + */ + {{/emitJSDoc}} + static fromJSON = function(json_string){ + return {{classname}}.constructFromObject(JSON.parse(json_string)); + } } {{#vars}}{{#emitJSDoc}}/**{{#description}} diff --git a/samples/client/petstore/javascript-apollo/src/model/Color.js b/samples/client/petstore/javascript-apollo/src/model/Color.js index 759e5df97af..a8bfc635973 100644 --- a/samples/client/petstore/javascript-apollo/src/model/Color.js +++ b/samples/client/petstore/javascript-apollo/src/model/Color.js @@ -145,6 +145,14 @@ class Color { return this.getActualInstance(); } + /** + * Create an instance of Color from a JSON string. + * @param {string} json_string JSON string. + * @return {module:model/Color} An instance of Color. + */ + static fromJSON = function(json_string){ + return Color.constructFromObject(JSON.parse(json_string)); + } } diff --git a/samples/client/petstore/javascript-apollo/src/model/Pig.js b/samples/client/petstore/javascript-apollo/src/model/Pig.js index 45f822e0276..ac867a3525e 100644 --- a/samples/client/petstore/javascript-apollo/src/model/Pig.js +++ b/samples/client/petstore/javascript-apollo/src/model/Pig.js @@ -99,6 +99,14 @@ class Pig { return this.getActualInstance(); } + /** + * Create an instance of Pig from a JSON string. + * @param {string} json_string JSON string. + * @return {module:model/Pig} An instance of Pig. + */ + static fromJSON = function(json_string){ + return Pig.constructFromObject(JSON.parse(json_string)); + } } /** diff --git a/samples/client/petstore/javascript-es6/src/model/Color.js b/samples/client/petstore/javascript-es6/src/model/Color.js index 759e5df97af..a8bfc635973 100644 --- a/samples/client/petstore/javascript-es6/src/model/Color.js +++ b/samples/client/petstore/javascript-es6/src/model/Color.js @@ -145,6 +145,14 @@ class Color { return this.getActualInstance(); } + /** + * Create an instance of Color from a JSON string. + * @param {string} json_string JSON string. + * @return {module:model/Color} An instance of Color. + */ + static fromJSON = function(json_string){ + return Color.constructFromObject(JSON.parse(json_string)); + } } diff --git a/samples/client/petstore/javascript-es6/src/model/Pig.js b/samples/client/petstore/javascript-es6/src/model/Pig.js index 45f822e0276..ac867a3525e 100644 --- a/samples/client/petstore/javascript-es6/src/model/Pig.js +++ b/samples/client/petstore/javascript-es6/src/model/Pig.js @@ -99,6 +99,14 @@ class Pig { return this.getActualInstance(); } + /** + * Create an instance of Pig from a JSON string. + * @param {string} json_string JSON string. + * @return {module:model/Pig} An instance of Pig. + */ + static fromJSON = function(json_string){ + return Pig.constructFromObject(JSON.parse(json_string)); + } } /** diff --git a/samples/client/petstore/javascript-es6/test/PetstoreTest.js b/samples/client/petstore/javascript-es6/test/PetstoreTest.js index 61596656f14..56c8993781a 100644 --- a/samples/client/petstore/javascript-es6/test/PetstoreTest.js +++ b/samples/client/petstore/javascript-es6/test/PetstoreTest.js @@ -187,6 +187,22 @@ describe('Petstore', function() { } }); + it('should test fromJSON in oneOf models', function() { + // invalid RgbaColor >255 + try { + let json = '[1,11,128,256]'; + OpenAPIPetstore.Color.fromJSON(json); + expect(true).to.be(false); // this line should not run if the error is thrown correctly + } catch (err) { + expect(err).to.be.eql(new Error('[Error: No match found constructing Color with oneOf schemas String, [Number]. Details: Failed to desserialize JSON data into [Number]: Error: Invalid array size. Minimim: 3. Maximum: 3. Data: 1,11,128,256, Failed to desserialize JSON data into [Number]: Error: Invalid integer value in an array items. Max.: 255. Min.: 0. Data: 1,11,128,256, Failed to desserialize JSON data into String: Error: Invalid data. Must be string. Data: [1,11,128,256]')); + } + + // valid RgbColor + let json = '[0,128,255]'; + let color = OpenAPIPetstore.Color.fromJSON(json); + expect(JSON.stringify(color)).to.be(json); + }); + it('should deserialize nested oneOf models correctly', function() { var json = '{"nested":"#00FF00","size":256}' var result = OpenAPIPetstore.ApiClient.convertToType(JSON.parse(json), OpenAPIPetstore.NestedColor); diff --git a/samples/client/petstore/javascript-promise-es6/src/model/Color.js b/samples/client/petstore/javascript-promise-es6/src/model/Color.js index 759e5df97af..a8bfc635973 100644 --- a/samples/client/petstore/javascript-promise-es6/src/model/Color.js +++ b/samples/client/petstore/javascript-promise-es6/src/model/Color.js @@ -145,6 +145,14 @@ class Color { return this.getActualInstance(); } + /** + * Create an instance of Color from a JSON string. + * @param {string} json_string JSON string. + * @return {module:model/Color} An instance of Color. + */ + static fromJSON = function(json_string){ + return Color.constructFromObject(JSON.parse(json_string)); + } } diff --git a/samples/client/petstore/javascript-promise-es6/src/model/Pig.js b/samples/client/petstore/javascript-promise-es6/src/model/Pig.js index 45f822e0276..ac867a3525e 100644 --- a/samples/client/petstore/javascript-promise-es6/src/model/Pig.js +++ b/samples/client/petstore/javascript-promise-es6/src/model/Pig.js @@ -99,6 +99,14 @@ class Pig { return this.getActualInstance(); } + /** + * Create an instance of Pig from a JSON string. + * @param {string} json_string JSON string. + * @return {module:model/Pig} An instance of Pig. + */ + static fromJSON = function(json_string){ + return Pig.constructFromObject(JSON.parse(json_string)); + } } /**