Skip to content

Commit b22bf0a

Browse files
authored
[js] fix boolean in oneOf, add tests (#14380)
1 parent c514dc3 commit b22bf0a

File tree

24 files changed

+646
-25
lines changed

24 files changed

+646
-25
lines changed

modules/openapi-generator/src/main/resources/Javascript/partial_model_oneof.mustache

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,59 +111,67 @@ class {{classname}} {
111111
}
112112
{{/items.isString}}
113113
{{#items.isNumber}}
114-
// validate array of string
114+
// validate array of number
115115
for (const item of instance) {
116116
if (!(typeof instance === 'number' && instance % 1 != 0)) {
117117
throw new Error("Invalid array items. Must be number. Input: " + JSON.stringify(instance));
118118
}
119119
}
120120
{{/items.isNumber}}
121+
{{#items.isBoolean}}
122+
// validate array of boolean
123+
for (const item of instance) {
124+
if (!(typeof instance === 'boolean')) {
125+
throw new Error("Invalid array items. Must be boolean. Input: " + JSON.stringify(instance));
126+
}
127+
}
128+
{{/items.isBoolean}}
121129
{{/isArray}}
122130
{{^isArray}}
123131
{{#isInteger}}
124-
// validate array of integer
132+
// validate integer
125133
if (!(typeof instance === 'number' && instance % 1 === 0)) {
126-
throw new Error("Invalid array items. Must be integer. Input: " + JSON.stringify(instance));
134+
throw new Error("Invalid value. Must be integer. Input: " + JSON.stringify(instance));
127135
}
128136
{{#maximum}}
129137
{{#minimum}}
130138
if (instance > {{maximum}} || instance < {{minimum}}) {
131-
throw new Error("Invalid integer value in an array items. Max.: {{maximum}}. Min.: {{minimum}}. Input: " + JSON.stringify(instance));
139+
throw new Error("Invalid integer value. Max.: {{maximum}}. Min.: {{minimum}}. Input: " + JSON.stringify(instance));
132140
}
133141
{{/minimum}}
134142
{{^minimum}}
135143
if (instance > {{maximum}}) {
136-
throw new Error("Invalid integer value in an array items. Max.: {{maximum}}. Input: " + JSON.stringify(instance));
144+
throw new Error("Invalid integer value. Max.: {{maximum}}. Input: " + JSON.stringify(instance));
137145
}
138146
{{/minimum}}
139147
{{/maximum}}
140148
{{^maximum}}
141149
{{#minimum}}
142150
if (instance < {{minimum}}) {
143-
throw new Error("Invalid integer value in an array items. Min.: {{minimum}}. Input: " + JSON.stringify(instance));
151+
throw new Error("Invalid integer value. Min.: {{minimum}}. Input: " + JSON.stringify(instance));
144152
}
145153
{{/minimum}}
146154
{{/maximum}}
147155
{{/isInteger}}
148156
{{#isString}}
149-
// validate array of string
157+
// validate string
150158
if (!(typeof instance === 'string')) {
151-
throw new Error("Invalid input. Must be string. Input: " + JSON.stringify(instance));
159+
throw new Error("Invalid value. Must be string. Input: " + JSON.stringify(instance));
152160
}
153161
{{#pattern}}
154162
if (!{{{pattern}}}.test(instance)) {
155-
throw new Error("Invalid string value in an array items. Must conform to {{{.}}}. Input: " + JSON.stringify(instance));
163+
throw new Error("Invalid string value. Must conform to {{{.}}}. Input: " + JSON.stringify(instance));
156164
}
157165
{{/pattern}}
158166
{{#minLength}}
159167
{{#maxLength}}
160168
if (instance.length > {{maxLength}} && instance.length < {{minLength}}) {
161-
throw new Error("Invalid string value in an array items. Max. length: {{{maxLength}}}. Min. length: {{{minLength}}}. Input: " + JSON.stringify(instance));
169+
throw new Error("Invalid string value. Max. length: {{{maxLength}}}. Min. length: {{{minLength}}}. Input: " + JSON.stringify(instance));
162170
}
163171
{{/maxLength}}
164172
{{^maxLength}}
165173
if (instance.length < {{minLength}}) {
166-
throw new Error("Invalid string value in an array items. Min. length: {{{minLength}}}. Input: " + instance);
174+
throw new Error("Invalid string value. Min. length: {{{minLength}}}. Input: " + instance);
167175
}
168176
{{/maxLength}}
169177
{{/minLength}}
@@ -176,11 +184,17 @@ class {{classname}} {
176184
{{/minLength}}
177185
{{/isString}}
178186
{{#isNumber}}
179-
// validate array of string
187+
// validate number
180188
if (!(typeof instance === 'number' && instance % 1 != 0)) {
181-
throw new Error("Invalid array items. Must be number. Input: " + JSON.stringify(instance));
189+
throw new Error("Invalid value. Must be number. Input: " + JSON.stringify(instance));
182190
}
183191
{{/isNumber}}
192+
{{#isBoolean}}
193+
// validate boolean
194+
if (!(typeof instance === 'boolean')) {
195+
throw new Error("Invalid value. Must be boolean. Input: " + JSON.stringify(instance));
196+
}
197+
{{/isBoolean}}
184198
{{/isArray}}
185199
this.actualInstance = instance;
186200
{{/isPrimitiveType}}

modules/openapi-generator/src/test/resources/3_0/javascript/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,3 +1977,8 @@ components:
19771977
- $ref: '#/components/schemas/RgbColor'
19781978
- $ref: '#/components/schemas/RgbaColor'
19791979
- $ref: '#/components/schemas/HexColor'
1980+
StringOrBoolean:
1981+
description: String or boolean
1982+
oneOf:
1983+
- type: string
1984+
- type: boolean

samples/client/petstore/javascript-apollo/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ docs/ReadOnlyFirst.md
5858
docs/Return.md
5959
docs/SpecialModelName.md
6060
docs/StoreApi.md
61+
docs/StringOrBoolean.md
6162
docs/Tag.md
6263
docs/User.md
6364
docs/UserApi.md
@@ -123,5 +124,6 @@ src/model/Pig.js
123124
src/model/ReadOnlyFirst.js
124125
src/model/Return.js
125126
src/model/SpecialModelName.js
127+
src/model/StringOrBoolean.js
126128
src/model/Tag.js
127129
src/model/User.js

samples/client/petstore/javascript-apollo/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Class | Method | HTTP request | Description
215215
- [OpenApiPetstore.ReadOnlyFirst](docs/ReadOnlyFirst.md)
216216
- [OpenApiPetstore.Return](docs/Return.md)
217217
- [OpenApiPetstore.SpecialModelName](docs/SpecialModelName.md)
218+
- [OpenApiPetstore.StringOrBoolean](docs/StringOrBoolean.md)
218219
- [OpenApiPetstore.Tag](docs/Tag.md)
219220
- [OpenApiPetstore.User](docs/User.md)
220221

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# OpenApiPetstore.StringOrBoolean
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
8+

samples/client/petstore/javascript-apollo/src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import Pig from './model/Pig';
6363
import ReadOnlyFirst from './model/ReadOnlyFirst';
6464
import Return from './model/Return';
6565
import SpecialModelName from './model/SpecialModelName';
66+
import StringOrBoolean from './model/StringOrBoolean';
6667
import Tag from './model/Tag';
6768
import User from './model/User';
6869
import AnotherFakeApi from './api/AnotherFakeApi';
@@ -412,6 +413,12 @@ export {
412413
*/
413414
SpecialModelName,
414415

416+
/**
417+
* The StringOrBoolean model constructor.
418+
* @property {module:model/StringOrBoolean}
419+
*/
420+
StringOrBoolean,
421+
415422
/**
416423
* The Tag model constructor.
417424
* @property {module:model/Tag}

samples/client/petstore/javascript-apollo/src/model/Color.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ class Color {
8484

8585
// Hex color string, such as #00FF00.
8686
try {
87-
// validate array of string
87+
// validate string
8888
if (!(typeof instance === 'string')) {
89-
throw new Error("Invalid input. Must be string. Input: " + JSON.stringify(instance));
89+
throw new Error("Invalid value. Must be string. Input: " + JSON.stringify(instance));
9090
}
9191
if (!/^#(?:[0-9a-fA-F]{3}){1,2}$/.test(instance)) {
92-
throw new Error("Invalid string value in an array items. Must conform to /^#(?:[0-9a-fA-F]{3}){1,2}$/. Input: " + JSON.stringify(instance));
92+
throw new Error("Invalid string value. Must conform to /^#(?:[0-9a-fA-F]{3}){1,2}$/. Input: " + JSON.stringify(instance));
9393
}
9494
if (instance.length > 7 && instance.length < 7) {
95-
throw new Error("Invalid string value in an array items. Max. length: 7. Min. length: 7. Input: " + JSON.stringify(instance));
95+
throw new Error("Invalid string value. Max. length: 7. Min. length: 7. Input: " + JSON.stringify(instance));
9696
}
9797
this.actualInstance = instance;
9898
match++;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* OpenAPI Petstore
3+
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
4+
*
5+
* The version of the OpenAPI document: 1.0.0
6+
*
7+
*
8+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9+
* https://openapi-generator.tech
10+
* Do not edit the class manually.
11+
*
12+
*/
13+
14+
import ApiClient from '../ApiClient';
15+
16+
/**
17+
* The StringOrBoolean model module.
18+
* @module model/StringOrBoolean
19+
* @version 1.0.0
20+
*/
21+
class StringOrBoolean {
22+
/**
23+
* Constructs a new <code>StringOrBoolean</code>.
24+
* String or boolean
25+
* @alias module:model/StringOrBoolean
26+
* @param {(module:model/Boolean|module:model/String)} instance The actual instance to initialize StringOrBoolean.
27+
*/
28+
constructor(instance = null) {
29+
if (instance === null) {
30+
this.actualInstance = null;
31+
return;
32+
}
33+
var match = 0;
34+
var errorMessages = [];
35+
try {
36+
// validate string
37+
if (!(typeof instance === 'string')) {
38+
throw new Error("Invalid value. Must be string. Input: " + JSON.stringify(instance));
39+
}
40+
this.actualInstance = instance;
41+
match++;
42+
} catch(err) {
43+
// json data failed to deserialize into String
44+
errorMessages.push("Failed to construct String: " + err)
45+
}
46+
47+
try {
48+
// validate boolean
49+
if (!(typeof instance === 'boolean')) {
50+
throw new Error("Invalid value. Must be boolean. Input: " + JSON.stringify(instance));
51+
}
52+
this.actualInstance = instance;
53+
match++;
54+
} catch(err) {
55+
// json data failed to deserialize into Boolean
56+
errorMessages.push("Failed to construct Boolean: " + err)
57+
}
58+
59+
if (match > 1) {
60+
throw new Error("Multiple matches found constructing `StringOrBoolean` with oneOf schemas Boolean, String. Input: " + JSON.stringify(instance));
61+
} else if (match === 0) {
62+
this.actualInstance = null; // clear the actual instance in case there are multiple matches
63+
throw new Error("No match found constructing `StringOrBoolean` with oneOf schemas Boolean, String. Details: " +
64+
errorMessages.join(", "));
65+
} else { // only 1 match
66+
// the input is valid
67+
}
68+
}
69+
70+
/**
71+
* Constructs a <code>StringOrBoolean</code> from a plain JavaScript object, optionally creating a new instance.
72+
* Copies all relevant properties from <code>data</code> to <code>obj</code> if supplied or a new instance if not.
73+
* @param {Object} data The plain JavaScript object bearing properties of interest.
74+
* @param {module:model/StringOrBoolean} obj Optional instance to populate.
75+
* @return {module:model/StringOrBoolean} The populated <code>StringOrBoolean</code> instance.
76+
*/
77+
static constructFromObject(data, obj) {
78+
return new StringOrBoolean(data);
79+
}
80+
81+
/**
82+
* Gets the actual instance, which can be <code>Boolean</code>, <code>String</code>.
83+
* @return {(module:model/Boolean|module:model/String)} The actual instance.
84+
*/
85+
getActualInstance() {
86+
return this.actualInstance;
87+
}
88+
89+
/**
90+
* Sets the actual instance, which can be <code>Boolean</code>, <code>String</code>.
91+
* @param {(module:model/Boolean|module:model/String)} obj The actual instance.
92+
*/
93+
setActualInstance(obj) {
94+
this.actualInstance = StringOrBoolean.constructFromObject(obj).getActualInstance();
95+
}
96+
97+
/**
98+
* Returns the JSON representation of the actual instance.
99+
* @return {string}
100+
*/
101+
toJSON = function(){
102+
return this.getActualInstance();
103+
}
104+
105+
/**
106+
* Create an instance of StringOrBoolean from a JSON string.
107+
* @param {string} json_string JSON string.
108+
* @return {module:model/StringOrBoolean} An instance of StringOrBoolean.
109+
*/
110+
static fromJSON = function(json_string){
111+
return StringOrBoolean.constructFromObject(JSON.parse(json_string));
112+
}
113+
}
114+
115+
116+
StringOrBoolean.OneOf = ["Boolean", "String"];
117+
118+
export default StringOrBoolean;
119+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* OpenAPI Petstore
3+
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
4+
*
5+
* The version of the OpenAPI document: 1.0.0
6+
*
7+
*
8+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9+
* https://openapi-generator.tech
10+
* Do not edit the class manually.
11+
*
12+
*/
13+
14+
(function(root, factory) {
15+
if (typeof define === 'function' && define.amd) {
16+
// AMD.
17+
define(['expect.js', process.cwd()+'/src/index'], factory);
18+
} else if (typeof module === 'object' && module.exports) {
19+
// CommonJS-like environments that support module.exports, like Node.
20+
factory(require('expect.js'), require(process.cwd()+'/src/index'));
21+
} else {
22+
// Browser globals (root is window)
23+
factory(root.expect, root.OpenApiPetstore);
24+
}
25+
}(this, function(expect, OpenApiPetstore) {
26+
'use strict';
27+
28+
var instance;
29+
30+
beforeEach(function() {
31+
instance = new OpenApiPetstore.StringOrBoolean();
32+
});
33+
34+
var getProperty = function(object, getter, property) {
35+
// Use getter method if present; otherwise, get the property directly.
36+
if (typeof object[getter] === 'function')
37+
return object[getter]();
38+
else
39+
return object[property];
40+
}
41+
42+
var setProperty = function(object, setter, property, value) {
43+
// Use setter method if present; otherwise, set the property directly.
44+
if (typeof object[setter] === 'function')
45+
object[setter](value);
46+
else
47+
object[property] = value;
48+
}
49+
50+
describe('StringOrBoolean', function() {
51+
it('should create an instance of StringOrBoolean', function() {
52+
// uncomment below and update the code to test StringOrBoolean
53+
//var instance = new OpenApiPetstore.StringOrBoolean();
54+
//expect(instance).to.be.a(OpenApiPetstore.StringOrBoolean);
55+
});
56+
57+
});
58+
59+
}));

samples/client/petstore/javascript-es6/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ docs/ReadOnlyFirst.md
5858
docs/Return.md
5959
docs/SpecialModelName.md
6060
docs/StoreApi.md
61+
docs/StringOrBoolean.md
6162
docs/Tag.md
6263
docs/User.md
6364
docs/UserApi.md
@@ -123,5 +124,6 @@ src/model/Pig.js
123124
src/model/ReadOnlyFirst.js
124125
src/model/Return.js
125126
src/model/SpecialModelName.js
127+
src/model/StringOrBoolean.js
126128
src/model/Tag.js
127129
src/model/User.js

0 commit comments

Comments
 (0)