add fromJSON method (#13573)

This commit is contained in:
William Cheng 2022-10-03 21:57:03 +08:00 committed by GitHub
parent 922f9783fd
commit c30ae752a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 74 additions and 0 deletions

View File

@ -249,6 +249,16 @@ class {{classname}} {
return this.getActualInstance(); 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}} {{#vars}}{{#emitJSDoc}}/**{{#description}}

View File

@ -145,6 +145,14 @@ class Color {
return this.getActualInstance(); 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));
}
} }

View File

@ -99,6 +99,14 @@ class Pig {
return this.getActualInstance(); 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));
}
} }
/** /**

View File

@ -145,6 +145,14 @@ class Color {
return this.getActualInstance(); 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));
}
} }

View File

@ -99,6 +99,14 @@ class Pig {
return this.getActualInstance(); 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));
}
} }
/** /**

View File

@ -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() { it('should deserialize nested oneOf models correctly', function() {
var json = '{"nested":"#00FF00","size":256}' var json = '{"nested":"#00FF00","size":256}'
var result = OpenAPIPetstore.ApiClient.convertToType(JSON.parse(json), OpenAPIPetstore.NestedColor); var result = OpenAPIPetstore.ApiClient.convertToType(JSON.parse(json), OpenAPIPetstore.NestedColor);

View File

@ -145,6 +145,14 @@ class Color {
return this.getActualInstance(); 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));
}
} }

View File

@ -99,6 +99,14 @@ class Pig {
return this.getActualInstance(); 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));
}
} }
/** /**