Skip to content

Commit

Permalink
add fromJSON method (#13573)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Oct 3, 2022
1 parent 922f978 commit c30ae75
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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><invokerPackage>/</invokerPackage><#modelPackage><modelPackage>/</modelPackage><&classname>}<={{ }}=> An instance of {{classname}}.
*/
{{/emitJSDoc}}
static fromJSON = function(json_string){
return {{classname}}.constructFromObject(JSON.parse(json_string));
}
}

{{#vars}}{{#emitJSDoc}}/**{{#description}}
Expand Down
8 changes: 8 additions & 0 deletions samples/client/petstore/javascript-apollo/src/model/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}


Expand Down
8 changes: 8 additions & 0 deletions samples/client/petstore/javascript-apollo/src/model/Pig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions samples/client/petstore/javascript-es6/src/model/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}


Expand Down
8 changes: 8 additions & 0 deletions samples/client/petstore/javascript-es6/src/model/Pig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

/**
Expand Down
16 changes: 16 additions & 0 deletions samples/client/petstore/javascript-es6/test/PetstoreTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

/**
Expand Down

0 comments on commit c30ae75

Please sign in to comment.