[JavaScript] add oneOf support (#13561)

* JS add oneOf support

* add validate json method

* add oneOf support to JS client

* update samples

* add todo in anyof

* switch to composed.oneOf

* update oneOf to support primitive types

* update messages

* minor fix, add more tests

* update samples

* fix npe in from model

* fix syntax error in length check

* update samples
This commit is contained in:
William Cheng
2022-10-02 13:34:50 +08:00
committed by GitHub
parent 2920c7bf91
commit 9cc23dd09d
193 changed files with 6766 additions and 4 deletions

View File

@@ -0,0 +1,199 @@
if (typeof module === 'object' && module.exports) {
var expect = require('expect.js');
var OpenAPIPetstore = require('../src/index');
var sinon = require('sinon');
}
var apiClient = OpenAPIPetstore.ApiClient.instance;
var getProperty = function(object, getter, property) {
// Use getter method if present; otherwise, get the property directly.
if (typeof object[getter] === 'function')
return object[getter]();
else
return object[property];
}
var setProperty = function(object, setter, property, value) {
// Use setter method if present; otherwise, set the property directly.
if (typeof object[setter] === 'function')
object[setter](value);
else
object[property] = value;
}
describe('Petstore', function() {
describe('models', function() {
it('should serialize oneOf models correctly', function() {
var pig = new OpenAPIPetstore.Pig();
expect(JSON.stringify(pig)).to.be('null');
// set basque big as the payload
var bpig = new OpenAPIPetstore.BasquePig();
setProperty(bpig, "setClassName", "className", "BasquePig");
setProperty(bpig, "setColor", "color", "red");
expect(JSON.stringify(bpig)).to.be('{"className":"BasquePig","color":"red"}');
pig.setActualInstance(bpig);
expect(JSON.stringify(pig)).to.be('{"className":"BasquePig","color":"red"}');
});
it('should serialize nested oneOf models correctly', function() {
var nested_one_of = new OpenAPIPetstore.NestedOneOf();
setProperty(nested_one_of, "setSize", "size", 28);
expect(JSON.stringify(nested_one_of)).to.be('{"size":28}');
// set nested oneOf `Pig`
var pig = new OpenAPIPetstore.Pig();
// set basque big as the payload
var bpig = new OpenAPIPetstore.BasquePig();
setProperty(bpig, "setClassName", "className", "BasquePig");
setProperty(bpig, "setColor", "color", "red");
pig.setActualInstance(bpig);
setProperty(nested_one_of, "setNestedPig", "nested_pig", pig);
expect(JSON.stringify(nested_one_of)).to.be('{"size":28,"nested_pig":{"className":"BasquePig","color":"red"}}');
});
it('should run BasquePig constructFromObject correctly', function() {
var bpig_json = '{"className":"BasquePig","color":"red"}';
var bpig = OpenAPIPetstore.BasquePig.constructFromObject(JSON.parse(bpig_json), null);
expect(JSON.stringify(bpig)).to.be('{"className":"BasquePig","color":"red"}');
OpenAPIPetstore.BasquePig.validateJSON(JSON.parse(bpig_json)); // should not throw error
});
it('should throw error from Pet.validateJSON', function() {
var bpig_json = '{"className":"BasquePig","color":"red"}';
try {
OpenAPIPetstore.Pet.validateJSON(JSON.parse(bpig_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('The required field `name` is not found in the JSON data: {"className":"BasquePig","color":"red"}'));
}
});
it('should run Pig constructFromObject correctly', function() {
var bpig = '{"className":"BasquePig","color":"red"}';
var pig = OpenAPIPetstore.Pig.constructFromObject(JSON.parse(bpig));
expect(JSON.stringify(pig)).to.be('{"className":"BasquePig","color":"red"}');
});
it('should throw an error when running Pig constructFromObject with incorrect data', function() {
try {
var bpig = '[1,2,3]';
var pig = OpenAPIPetstore.Pig.constructFromObject(JSON.parse(bpig));
} catch (err) {
expect(err).to.be.eql(new Error('No match found constructing `Pig` with oneOf schemas BasquePig, DanishPig. Details: Failed to construct BasquePig: Error: The required field `className` is not found in the JSON data: [1,2,3], Failed to construct DanishPig: Error: The required field `className` is not found in the JSON data: [1,2,3]'));
}
});
it('should deserialize simple models correctly', function() {
var tag_json = '{"id":1,"name":"tag_name"}';
var tag_result = OpenAPIPetstore.ApiClient.convertToType(tag_json, OpenAPIPetstore.Tag);
expect(tag_result).to.be.a(OpenAPIPetstore.Tag);
//expect(tag_result.id).to.be(1);
//expect(JSON.stringify(tag_result)).to.be(tag_json);
});
it('should deserialize nested oneOf models correctly', function() {
var nested_one_of_json = '{"size":28,"nested_pig":{"className":"BasquePig","color":"red"}}'
var result = OpenAPIPetstore.ApiClient.convertToType(JSON.parse(nested_one_of_json), OpenAPIPetstore.NestedOneOf);
expect(result).to.be.a(OpenAPIPetstore.NestedOneOf);
expect(JSON.stringify(result)).to.be(nested_one_of_json);
});
it('should run Color constructFromObject correctly from array', function() {
// construct from RgbColor
let array_integer = [0,128,255];
let color = OpenAPIPetstore.Color.constructFromObject(array_integer, null);
expect(color).to.be.a(OpenAPIPetstore.Color);
expect(color.getActualInstance()).to.be.eql(array_integer);
});
it('should throw an error when running Color constructFromObject with invalid array', function() {
// construct from RgbColor
let array_integer = [0,128,9255];
try {
let color = OpenAPIPetstore.Color.constructFromObject(array_integer, null);
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('No match found constructing `Color` with oneOf schemas String, [Number]. Details: Failed to construct [Number]: Error: Invalid integer value in an array items. Max.: 255. Min.: 0. Data: 0,128,9255, Failed to construct [Number]: Error: Invalid array size. Minimim: 4. Maximum: 4. Data: 0,128,9255, Failed to construct String: Error: Invalid data. Must be string. Data: [0,128,9255]'));
}
});
it('should run Color constructFromObject correctly', function() {
// valid hex color
var json = '"#00FF00"';
var color = OpenAPIPetstore.Color.constructFromObject(JSON.parse(json), null);
expect(JSON.stringify(color)).to.be(json);
// valid RgbColor
json = '[0,128,255]';
color = OpenAPIPetstore.Color.constructFromObject(JSON.parse(json), null);
expect(JSON.stringify(color)).to.be(json);
// valid RgbaColor
json = '[0,128,200,255]';
color = OpenAPIPetstore.Color.constructFromObject(JSON.parse(json), null);
expect(JSON.stringify(color)).to.be(json);
});
it('should thrown an error when running Color constructFromObject with invalid data', function() {
// invalid hex color
try {
let json = '"#00FF00ZZZZ"';
OpenAPIPetstore.Color.constructFromObject(JSON.parse(json), null);
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('No match found constructing Color with oneOf schemas String, [Number]. Details: Failed to desserialize JSON data into [Number]: Error: Invalid data type. Expecting array. Data: #00FF00ZZZZ, Failed to desserialize JSON data into [Number]: Error: Invalid data type. Expecting array. Data: #00FF00ZZZZ, Failed to desserialize JSON data into String: Error: Invalid string value in an array items. Must conform to /^#(?:[0-9a-fA-F]{3}){1,2}$/. Data: "#00FF00ZZZZ"'));
}
// invalid RgbColor <0
try {
let json = '[-1,128,255]';
OpenAPIPetstore.Color.constructFromObject(JSON.parse(json), null);
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('No match found constructing Color with oneOf schemas String, [Number]. Details: Failed to desserialize JSON data into [Number]: Error: Invalid integer value in an array items. Max.: 255. Min.: 0. Data: -1,128,255, Failed to desserialize JSON data into [Number]: Error: Invalid array size. Minimim: 4. Maximum: 4. Data: -1,128,255, Failed to desserialize JSON data into String: Error: Invalid data. Must be string. Data: [-1,128,255]'));
}
// invalid RgbColor >255
try {
let json = '[1,128,256]';
OpenAPIPetstore.Color.constructFromObject(JSON.parse(json), null);
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('No match found constructing Color with oneOf schemas String, [Number]. Details: Failed to desserialize JSON data into [Number]: Error: Invalid integer value in an array items. Max.: 255. Min.: 0. Data: 1,128,256, Failed to desserialize JSON data into [Number]: Error: Invalid array size. Minimim: 4. Maximum: 4. Data: 1,128,256, Failed to desserialize JSON data into String: Error: Invalid data. Must be string. Data: [1,128,256]'));
}
// invalid RgbaColor <0
try {
let json = '[-1,1,128,255]';
OpenAPIPetstore.Color.constructFromObject(JSON.parse(json), null);
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('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,1,128,255, Failed to desserialize JSON data into [Number]: Error: Invalid integer value in an array items. Max.: 255. Min.: 0. Data: -1,1,128,255, Failed to desserialize JSON data into String: Error: Invalid data. Must be string. Data: [-1,1,128,255]'));
}
// invalid RgbaColor >255
try {
let json = '[1,11,128,256]';
OpenAPIPetstore.Color.constructFromObject(JSON.parse(json), null);
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]'));
}
});
it('should deserialize nested oneOf models correctly', function() {
var json = '{"nested":"#00FF00","size":256}'
var result = OpenAPIPetstore.ApiClient.convertToType(JSON.parse(json), OpenAPIPetstore.NestedColor);
expect(result).to.be.a(OpenAPIPetstore.NestedColor);
expect(JSON.stringify(result)).to.be('{"size":256,"nested":"#00FF00"}');
});
});
});

View File

@@ -26,6 +26,7 @@
'use strict';
var api_instance;
var id = 129038120;
beforeEach(function() {
api_instance = new OpenApiPetstore.PetApi();
@@ -48,7 +49,6 @@
}
var createRandomPet = function() {
var id = new Date().getTime();
var pet = new OpenApiPetstore.Pet();
setProperty(pet, "setId", "id", id);
setProperty(pet, "setName", "name", "pet" + id);
@@ -81,6 +81,13 @@
expect(response.get('Content-Type')).to.be('application/json');
expect(fetched).to.be.a(OpenApiPetstore.Pet);
expect(JSON.stringify(fetched)).to.be('{"name":"pet129038120","photoUrls":["http://foo.bar.com/1","http://foo.bar.com/2"],"id":129038120,"category":{"name":"category129038120","id":129038120},"tags":[],"status":"available"}');
// test ApiClient.convertToType
var tag_result = OpenApiPetstore.ApiClient.convertToType(JSON.stringify(fetched), OpenApiPetstore.Pet);
expect(tag_result).to.be.a(OpenApiPetstore.Pet);
// test returned object `Pet`
expect(fetched.id).to.be(pet.id);
expect(getProperty(fetched, "getPhotoUrls", "photoUrls"))
.to.eql(getProperty(pet, "getPhotoUrls", "photoUrls"));

View File

@@ -0,0 +1,71 @@
/**
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*
*/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD.
define(['expect.js', process.cwd()+'/src/index'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
factory(require('expect.js'), require(process.cwd()+'/src/index'));
} else {
// Browser globals (root is window)
factory(root.expect, root.OpenApiPetstore);
}
}(this, function(expect, OpenApiPetstore) {
'use strict';
var instance;
beforeEach(function() {
instance = new OpenApiPetstore.BasquePig();
});
var getProperty = function(object, getter, property) {
// Use getter method if present; otherwise, get the property directly.
if (typeof object[getter] === 'function')
return object[getter]();
else
return object[property];
}
var setProperty = function(object, setter, property, value) {
// Use setter method if present; otherwise, set the property directly.
if (typeof object[setter] === 'function')
object[setter](value);
else
object[property] = value;
}
describe('BasquePig', function() {
it('should create an instance of BasquePig', function() {
// uncomment below and update the code to test BasquePig
//var instance = new OpenApiPetstore.BasquePig();
//expect(instance).to.be.a(OpenApiPetstore.BasquePig);
});
it('should have the property className (base name: "className")', function() {
// uncomment below and update the code to test the property className
//var instance = new OpenApiPetstore.BasquePig();
//expect(instance).to.be();
});
it('should have the property color (base name: "color")', function() {
// uncomment below and update the code to test the property color
//var instance = new OpenApiPetstore.BasquePig();
//expect(instance).to.be();
});
});
}));

View File

@@ -0,0 +1,59 @@
/**
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*
*/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD.
define(['expect.js', process.cwd()+'/src/index'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
factory(require('expect.js'), require(process.cwd()+'/src/index'));
} else {
// Browser globals (root is window)
factory(root.expect, root.OpenApiPetstore);
}
}(this, function(expect, OpenApiPetstore) {
'use strict';
var instance;
beforeEach(function() {
//instance = new OpenApiPetstore.Color('#00FF00');
});
var getProperty = function(object, getter, property) {
// Use getter method if present; otherwise, get the property directly.
if (typeof object[getter] === 'function')
return object[getter]();
else
return object[property];
}
var setProperty = function(object, setter, property, value) {
// Use setter method if present; otherwise, set the property directly.
if (typeof object[setter] === 'function')
object[setter](value);
else
object[property] = value;
}
describe('Color', function() {
it('should create an instance of Color', function() {
// uncomment below and update the code to test Color
//var instance = new OpenApiPetstore.Color();
//expect(instance).to.be.a(OpenApiPetstore.Color);
});
});
}));

View File

@@ -0,0 +1,71 @@
/**
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*
*/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD.
define(['expect.js', process.cwd()+'/src/index'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
factory(require('expect.js'), require(process.cwd()+'/src/index'));
} else {
// Browser globals (root is window)
factory(root.expect, root.OpenApiPetstore);
}
}(this, function(expect, OpenApiPetstore) {
'use strict';
var instance;
beforeEach(function() {
instance = new OpenApiPetstore.DanishPig();
});
var getProperty = function(object, getter, property) {
// Use getter method if present; otherwise, get the property directly.
if (typeof object[getter] === 'function')
return object[getter]();
else
return object[property];
}
var setProperty = function(object, setter, property, value) {
// Use setter method if present; otherwise, set the property directly.
if (typeof object[setter] === 'function')
object[setter](value);
else
object[property] = value;
}
describe('DanishPig', function() {
it('should create an instance of DanishPig', function() {
// uncomment below and update the code to test DanishPig
//var instance = new OpenApiPetstore.DanishPig();
//expect(instance).to.be.a(OpenApiPetstore.DanishPig);
});
it('should have the property className (base name: "className")', function() {
// uncomment below and update the code to test the property className
//var instance = new OpenApiPetstore.DanishPig();
//expect(instance).to.be();
});
it('should have the property size (base name: "size")', function() {
// uncomment below and update the code to test the property size
//var instance = new OpenApiPetstore.DanishPig();
//expect(instance).to.be();
});
});
}));

View File

@@ -0,0 +1,71 @@
/**
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*
*/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD.
define(['expect.js', process.cwd()+'/src/index'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
factory(require('expect.js'), require(process.cwd()+'/src/index'));
} else {
// Browser globals (root is window)
factory(root.expect, root.OpenApiPetstore);
}
}(this, function(expect, OpenApiPetstore) {
'use strict';
var instance;
beforeEach(function() {
instance = new OpenApiPetstore.NestedColor();
});
var getProperty = function(object, getter, property) {
// Use getter method if present; otherwise, get the property directly.
if (typeof object[getter] === 'function')
return object[getter]();
else
return object[property];
}
var setProperty = function(object, setter, property, value) {
// Use setter method if present; otherwise, set the property directly.
if (typeof object[setter] === 'function')
object[setter](value);
else
object[property] = value;
}
describe('NestedColor', function() {
it('should create an instance of NestedColor', function() {
// uncomment below and update the code to test NestedColor
//var instance = new OpenApiPetstore.NestedColor();
//expect(instance).to.be.a(OpenApiPetstore.NestedColor);
});
it('should have the property size (base name: "size")', function() {
// uncomment below and update the code to test the property size
//var instance = new OpenApiPetstore.NestedColor();
//expect(instance).to.be();
});
it('should have the property nested (base name: "nested")', function() {
// uncomment below and update the code to test the property nested
//var instance = new OpenApiPetstore.NestedColor();
//expect(instance).to.be();
});
});
}));

View File

@@ -0,0 +1,71 @@
/**
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*
*/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD.
define(['expect.js', process.cwd()+'/src/index'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
factory(require('expect.js'), require(process.cwd()+'/src/index'));
} else {
// Browser globals (root is window)
factory(root.expect, root.OpenApiPetstore);
}
}(this, function(expect, OpenApiPetstore) {
'use strict';
var instance;
beforeEach(function() {
instance = new OpenApiPetstore.NestedOneOf();
});
var getProperty = function(object, getter, property) {
// Use getter method if present; otherwise, get the property directly.
if (typeof object[getter] === 'function')
return object[getter]();
else
return object[property];
}
var setProperty = function(object, setter, property, value) {
// Use setter method if present; otherwise, set the property directly.
if (typeof object[setter] === 'function')
object[setter](value);
else
object[property] = value;
}
describe('NestedOneOf', function() {
it('should create an instance of NestedOneOf', function() {
// uncomment below and update the code to test NestedOneOf
//var instance = new OpenApiPetstore.NestedOneOf();
//expect(instance).to.be.a(OpenApiPetstore.NestedOneOf);
});
it('should have the property size (base name: "size")', function() {
// uncomment below and update the code to test the property size
//var instance = new OpenApiPetstore.NestedOneOf();
//expect(instance).to.be();
});
it('should have the property nestedPig (base name: "nested_pig")', function() {
// uncomment below and update the code to test the property nestedPig
//var instance = new OpenApiPetstore.NestedOneOf();
//expect(instance).to.be();
});
});
}));

View File

@@ -0,0 +1,77 @@
/**
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*
*/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD.
define(['expect.js', process.cwd()+'/src/index'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
factory(require('expect.js'), require(process.cwd()+'/src/index'));
} else {
// Browser globals (root is window)
factory(root.expect, root.OpenApiPetstore);
}
}(this, function(expect, OpenApiPetstore) {
'use strict';
var instance;
beforeEach(function() {
instance = new OpenApiPetstore.Pig();
});
var getProperty = function(object, getter, property) {
// Use getter method if present; otherwise, get the property directly.
if (typeof object[getter] === 'function')
return object[getter]();
else
return object[property];
}
var setProperty = function(object, setter, property, value) {
// Use setter method if present; otherwise, set the property directly.
if (typeof object[setter] === 'function')
object[setter](value);
else
object[property] = value;
}
describe('Pig', function() {
it('should create an instance of Pig', function() {
// uncomment below and update the code to test Pig
//var instance = new OpenApiPetstore.Pig();
//expect(instance).to.be.a(OpenApiPetstore.Pig);
});
it('should have the property className (base name: "className")', function() {
// uncomment below and update the code to test the property className
//var instance = new OpenApiPetstore.Pig();
//expect(instance).to.be();
});
it('should have the property color (base name: "color")', function() {
// uncomment below and update the code to test the property color
//var instance = new OpenApiPetstore.Pig();
//expect(instance).to.be();
});
it('should have the property size (base name: "size")', function() {
// uncomment below and update the code to test the property size
//var instance = new OpenApiPetstore.Pig();
//expect(instance).to.be();
});
});
}));