@e22m4u/js-openapi 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +226 -146
- package/dist/cjs/index.cjs +2025 -2092
- package/package.json +10 -8
- package/schema/openapi-3-1/dialect/base.js +21 -0
- package/schema/openapi-3-1/meta/base.js +76 -0
- package/schema/openapi-3-1/schema-base.js +32 -0
- package/schema/openapi-3-1/schema.js +1403 -0
- package/src/ajv.js +32 -0
- package/src/errors/index.d.ts +1 -1
- package/src/errors/index.js +1 -1
- package/src/errors/oa-document-object-validation-error.d.ts +6 -0
- package/src/errors/oa-document-object-validation-error.js +6 -0
- package/src/errors/oa-document-object-validation-error.spec.js +10 -0
- package/src/index.d.ts +1 -3
- package/src/index.js +1 -3
- package/src/json-pointer/resolve-json-pointer.js +1 -1
- package/src/json-pointer/unescape-json-pointer.d.ts +1 -2
- package/src/oa-document-builder.d.ts +302 -111
- package/src/oa-document-builder.js +208 -142
- package/src/oa-document-builder.spec.js +1411 -0
- package/src/oa-document-object/index.d.ts +1 -1
- package/src/oa-document-object/index.js +1 -1
- package/src/oa-document-object/validate-oa-document-object.d.ts +18 -0
- package/src/oa-document-object/validate-oa-document-object.js +53 -0
- package/src/oa-document-object/validate-oa-document-object.spec.js +122 -0
- package/src/{oa-document-scope.d.ts → oa-operation-group.d.ts} +7 -7
- package/src/{oa-document-scope.js → oa-operation-group.js} +101 -46
- package/src/oa-operation-group.spec.js +544 -0
- package/src/oa-reference-object/is-oa-reference-object.js +1 -1
- package/src/oa-reference-object/is-oa-reference-object.spec.js +1 -1
- package/src/oa-reference-object/oa-ref.js +7 -0
- package/src/oa-reference-object/resolve-oa-reference-object.js +1 -11
- package/src/oa-reference-object/resolve-oa-reference-object.spec.js +0 -10
- package/src/oa-specification.d.ts +55 -66
- package/src/oa-specification.js +11 -22
- package/src/data-type/index.d.ts +0 -1
- package/src/data-type/index.js +0 -1
- package/src/data-type/infer-openapi-data-type.d.ts +0 -30
- package/src/data-type/infer-openapi-data-type.js +0 -38
- package/src/data-validation/data-format-validator-map.d.ts +0 -13
- package/src/data-validation/data-format-validator-map.js +0 -36
- package/src/data-validation/data-format-validator-map.spec.js +0 -39
- package/src/data-validation/data-format-validators.d.ts +0 -84
- package/src/data-validation/data-format-validators.js +0 -217
- package/src/data-validation/index.d.ts +0 -3
- package/src/data-validation/index.js +0 -3
- package/src/data-validation/validate-data-with-openapi-schema.d.ts +0 -46
- package/src/data-validation/validate-data-with-openapi-schema.js +0 -1913
- package/src/data-validation/validate-data-with-openapi-schema.spec.js +0 -6953
- package/src/errors/oa-data-validation-error.d.ts +0 -6
- package/src/errors/oa-data-validation-error.js +0 -6
- package/src/errors/oa-data-validation-error.spec.js +0 -17
- package/src/oa-document-object/validate-shallow-oa-document.d.ts +0 -10
- package/src/oa-document-object/validate-shallow-oa-document.js +0 -209
- package/src/oa-document-object/validate-shallow-oa-document.spec.js +0 -362
- package/src/utils/count-unicode.d.ts +0 -11
- package/src/utils/count-unicode.js +0 -15
- package/src/utils/index.d.ts +0 -5
- package/src/utils/index.js +0 -5
- package/src/utils/join-path.d.ts +0 -6
- package/src/utils/join-path.js +0 -36
- package/src/utils/join-path.spec.js +0 -104
- package/src/utils/normalize-path.d.ts +0 -12
- package/src/utils/normalize-path.js +0 -22
- package/src/utils/normalize-path.spec.js +0 -56
- package/src/utils/to-pascal-case.d.ts +0 -6
- package/src/utils/to-pascal-case.js +0 -26
- package/src/utils/to-pascal-case.spec.js +0 -15
- package/src/utils/to-spaced-json.d.ts +0 -17
- package/src/utils/to-spaced-json.js +0 -27
|
@@ -0,0 +1,1411 @@
|
|
|
1
|
+
import {expect} from 'chai';
|
|
2
|
+
import {format} from '@e22m4u/js-format';
|
|
3
|
+
import {ServiceContainer} from '@e22m4u/js-service';
|
|
4
|
+
import {OADocumentObjectValidationError} from './errors/index.js';
|
|
5
|
+
import {OAComponentType, OADocumentBuilder} from './oa-document-builder.js';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
OADataType,
|
|
9
|
+
OAMediaType,
|
|
10
|
+
OPENAPI_VERSION,
|
|
11
|
+
OAParameterLocation,
|
|
12
|
+
OASecuritySchemeType,
|
|
13
|
+
OAOperationMethod,
|
|
14
|
+
} from './oa-specification.js';
|
|
15
|
+
import {OAOperationGroup} from './oa-operation-group.js';
|
|
16
|
+
|
|
17
|
+
describe('OADocumentBuilder', function () {
|
|
18
|
+
describe('constructor', function () {
|
|
19
|
+
it('should not require any parameters', function () {
|
|
20
|
+
new OADocumentBuilder();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should pass the ServiceContainer to the super class', function () {
|
|
24
|
+
const cont = new ServiceContainer();
|
|
25
|
+
const S = new OADocumentBuilder(cont);
|
|
26
|
+
expect(S.container).to.be.eq(cont);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should require the parameter "containerOrDocument" to be an Object', function () {
|
|
30
|
+
const throwable = v => () => new OADocumentBuilder(v);
|
|
31
|
+
const error = s =>
|
|
32
|
+
format(
|
|
33
|
+
'Parameter "containerOrDocument" must be an Object, ' +
|
|
34
|
+
'but %s was given.',
|
|
35
|
+
s,
|
|
36
|
+
);
|
|
37
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
38
|
+
expect(throwable('')).to.throw(error('""'));
|
|
39
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
40
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
41
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
42
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
43
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
44
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
45
|
+
throwable({})();
|
|
46
|
+
throwable(undefined)();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should require the parameter "document" to be an Object', function () {
|
|
50
|
+
const throwable = v => () => new OADocumentBuilder(undefined, v);
|
|
51
|
+
const error = s =>
|
|
52
|
+
format('Parameter "document" must be an Object, but %s was given.', s);
|
|
53
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
54
|
+
expect(throwable('')).to.throw(error('""'));
|
|
55
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
56
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
57
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
58
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
59
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
60
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
61
|
+
throwable({})();
|
|
62
|
+
throwable(undefined)();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should merge the document from the first parameter with the default document', function () {
|
|
66
|
+
const doc = {
|
|
67
|
+
openapi: '3.1.2',
|
|
68
|
+
info: {
|
|
69
|
+
title: 'My OpenAPI Doc',
|
|
70
|
+
version: '1.0.0',
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
const S = new OADocumentBuilder(doc);
|
|
74
|
+
const res = S.getDocumentObjectRef();
|
|
75
|
+
expect(res).to.be.eql({...doc, paths: {}});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should merge the document from the second parameter with the default document', function () {
|
|
79
|
+
const doc = {
|
|
80
|
+
openapi: '3.1.2',
|
|
81
|
+
info: {
|
|
82
|
+
title: 'My OpenAPI Doc',
|
|
83
|
+
version: '1.0.0',
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
const S = new OADocumentBuilder(undefined, doc);
|
|
87
|
+
const res = S.getDocumentObjectRef();
|
|
88
|
+
expect(res).to.be.eql({...doc, paths: {}});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should validate the given document object', function () {
|
|
92
|
+
const doc = {openapi: 'abc'};
|
|
93
|
+
const throwable = () => new OADocumentBuilder(doc);
|
|
94
|
+
expect(throwable).to.throw(
|
|
95
|
+
'Value at "#/openapi" must match pattern "^3\\.1\\.\\d+(-.+)?$".',
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('getDocumentObjectRef', function () {
|
|
101
|
+
it('should return the document object reference', function () {
|
|
102
|
+
const S = new OADocumentBuilder();
|
|
103
|
+
const ref1 = S.getDocumentObjectRef();
|
|
104
|
+
const ref2 = S.getDocumentObjectRef();
|
|
105
|
+
expect(ref1).to.be.eql({
|
|
106
|
+
openapi: OPENAPI_VERSION,
|
|
107
|
+
info: {
|
|
108
|
+
title: 'API Documentation',
|
|
109
|
+
version: '0.0.1',
|
|
110
|
+
},
|
|
111
|
+
paths: {},
|
|
112
|
+
});
|
|
113
|
+
expect(ref1).to.be.eq(ref2);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('defineComponent', function () {
|
|
118
|
+
it('should require the parameter "type" to be a correct value', function () {
|
|
119
|
+
const throwable = v => () => {
|
|
120
|
+
const S = new OADocumentBuilder();
|
|
121
|
+
S.defineComponent(v, 'comp', {});
|
|
122
|
+
};
|
|
123
|
+
const error = s => format('Components type %s is not supported.', s);
|
|
124
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
125
|
+
expect(throwable('')).to.throw(error('""'));
|
|
126
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
127
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
128
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
129
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
130
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
131
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
132
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
133
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
134
|
+
throwable(OAComponentType.SCHEMA)();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('should require the parameter "name" to be a non-empty String', function () {
|
|
138
|
+
const throwable = v => () => {
|
|
139
|
+
const S = new OADocumentBuilder();
|
|
140
|
+
S.defineComponent(OAComponentType.SCHEMA, v, {});
|
|
141
|
+
};
|
|
142
|
+
const error = s =>
|
|
143
|
+
format(
|
|
144
|
+
'Component name must be a non-empty String, but %s was given.',
|
|
145
|
+
s,
|
|
146
|
+
);
|
|
147
|
+
expect(throwable('')).to.throw(error('""'));
|
|
148
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
149
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
150
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
151
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
152
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
153
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
154
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
155
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
156
|
+
throwable('comp')();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('should require the parameter "name" to not contain invalid characters', function () {
|
|
160
|
+
const throwable = v => () => {
|
|
161
|
+
const S = new OADocumentBuilder();
|
|
162
|
+
S.defineComponent(OAComponentType.SCHEMA, v, {});
|
|
163
|
+
};
|
|
164
|
+
const error = s =>
|
|
165
|
+
format('Component name %s contains invalid characters.', s);
|
|
166
|
+
expect(throwable('/')).to.throw(error('"/"'));
|
|
167
|
+
expect(throwable('$')).to.throw(error('"$"'));
|
|
168
|
+
expect(throwable('~')).to.throw(error('"~"'));
|
|
169
|
+
throwable('comp')();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('should require the parameter "component" to be an Object', function () {
|
|
173
|
+
const throwable = v => () => {
|
|
174
|
+
const S = new OADocumentBuilder();
|
|
175
|
+
S.defineComponent(OAComponentType.SCHEMA, 'comp', v);
|
|
176
|
+
};
|
|
177
|
+
const error = s =>
|
|
178
|
+
format('Component "comp" must be an Object, but %s was given.', s);
|
|
179
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
180
|
+
expect(throwable('')).to.throw(error('""'));
|
|
181
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
182
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
183
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
184
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
185
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
186
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
187
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
188
|
+
throwable({})();
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('should return the current instance', function () {
|
|
192
|
+
const S = new OADocumentBuilder();
|
|
193
|
+
const type = OAComponentType.SCHEMA;
|
|
194
|
+
const res = S.defineComponent(type, 'comp', {});
|
|
195
|
+
expect(res).to.be.eql(S);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
describe('when the type is "schema"', function () {
|
|
199
|
+
it('should define the schema object', function () {
|
|
200
|
+
const S = new OADocumentBuilder();
|
|
201
|
+
const type = OAComponentType.SCHEMA;
|
|
202
|
+
const comp = {type: OADataType.STRING};
|
|
203
|
+
S.defineComponent(type, 'comp', comp);
|
|
204
|
+
const res = S.getComponent(type, 'comp');
|
|
205
|
+
expect(res).to.be.eql(comp);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('should define the reference object', function () {
|
|
209
|
+
const S = new OADocumentBuilder();
|
|
210
|
+
const type = OAComponentType.SCHEMA;
|
|
211
|
+
const comp = {$ref: '#/components/schemes/target'};
|
|
212
|
+
S.defineComponent(type, 'comp', comp);
|
|
213
|
+
const res = S.getComponent(type, 'comp');
|
|
214
|
+
expect(res).to.be.eql(comp);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('should validate the schema object', function () {
|
|
218
|
+
const S = new OADocumentBuilder();
|
|
219
|
+
const type = OAComponentType.SCHEMA;
|
|
220
|
+
const invalidComp = {type: 123};
|
|
221
|
+
const throwable = () => {
|
|
222
|
+
S.defineComponent(type, 'comp', invalidComp);
|
|
223
|
+
};
|
|
224
|
+
expect(throwable).to.throw(OADocumentObjectValidationError);
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe('when the type is "response"', function () {
|
|
229
|
+
it('should define the response object', function () {
|
|
230
|
+
const S = new OADocumentBuilder();
|
|
231
|
+
const type = OAComponentType.RESPONSE;
|
|
232
|
+
const comp = {description: 'Response'};
|
|
233
|
+
S.defineComponent(type, 'comp', comp);
|
|
234
|
+
const res = S.getComponent(type, 'comp');
|
|
235
|
+
expect(res).to.be.eql(comp);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('should define the reference object', function () {
|
|
239
|
+
const S = new OADocumentBuilder();
|
|
240
|
+
const type = OAComponentType.RESPONSE;
|
|
241
|
+
const comp = {$ref: '#/components/responses/target'};
|
|
242
|
+
S.defineComponent(type, 'comp', comp);
|
|
243
|
+
const res = S.getComponent(type, 'comp');
|
|
244
|
+
expect(res).to.be.eql(comp);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('should validate the response object', function () {
|
|
248
|
+
const S = new OADocumentBuilder();
|
|
249
|
+
const type = OAComponentType.RESPONSE;
|
|
250
|
+
const invalidComp = {description: 123};
|
|
251
|
+
const throwable = () => {
|
|
252
|
+
S.defineComponent(type, 'comp', invalidComp);
|
|
253
|
+
};
|
|
254
|
+
expect(throwable).to.throw(OADocumentObjectValidationError);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
describe('when the type is "parameter"', function () {
|
|
259
|
+
it('should define the parameter object', function () {
|
|
260
|
+
const S = new OADocumentBuilder();
|
|
261
|
+
const type = OAComponentType.PARAMETER;
|
|
262
|
+
const comp = {
|
|
263
|
+
name: 'param',
|
|
264
|
+
in: OAParameterLocation.QUERY,
|
|
265
|
+
schema: {type: OADataType.STRING},
|
|
266
|
+
};
|
|
267
|
+
S.defineComponent(type, 'comp', comp);
|
|
268
|
+
const res = S.getComponent(type, 'comp');
|
|
269
|
+
expect(res).to.be.eql(comp);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('should define the reference object', function () {
|
|
273
|
+
const S = new OADocumentBuilder();
|
|
274
|
+
const type = OAComponentType.PARAMETER;
|
|
275
|
+
const comp = {$ref: '#/components/parameters/target'};
|
|
276
|
+
S.defineComponent(type, 'comp', comp);
|
|
277
|
+
const res = S.getComponent(type, 'comp');
|
|
278
|
+
expect(res).to.be.eql(comp);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('should validate the parameter object', function () {
|
|
282
|
+
const S = new OADocumentBuilder();
|
|
283
|
+
const type = OAComponentType.PARAMETER;
|
|
284
|
+
const invalidComp = {name: 123};
|
|
285
|
+
const throwable = () => {
|
|
286
|
+
S.defineComponent(type, 'comp', invalidComp);
|
|
287
|
+
};
|
|
288
|
+
expect(throwable).to.throw(OADocumentObjectValidationError);
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
describe('when the type is "example"', function () {
|
|
293
|
+
it('should define the example object', function () {
|
|
294
|
+
const S = new OADocumentBuilder();
|
|
295
|
+
const type = OAComponentType.EXAMPLE;
|
|
296
|
+
const comp = {value: 10};
|
|
297
|
+
S.defineComponent(type, 'comp', comp);
|
|
298
|
+
const res = S.getComponent(type, 'comp');
|
|
299
|
+
expect(res).to.be.eql(comp);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('should define the reference object', function () {
|
|
303
|
+
const S = new OADocumentBuilder();
|
|
304
|
+
const type = OAComponentType.EXAMPLE;
|
|
305
|
+
const comp = {$ref: '#/components/examples/target'};
|
|
306
|
+
S.defineComponent(type, 'comp', comp);
|
|
307
|
+
const res = S.getComponent(type, 'comp');
|
|
308
|
+
expect(res).to.be.eql(comp);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('should validate the example object', function () {
|
|
312
|
+
const S = new OADocumentBuilder();
|
|
313
|
+
const type = OAComponentType.EXAMPLE;
|
|
314
|
+
const invalidComp = {description: 123};
|
|
315
|
+
const throwable = () => {
|
|
316
|
+
S.defineComponent(type, 'comp', invalidComp);
|
|
317
|
+
};
|
|
318
|
+
expect(throwable).to.throw(OADocumentObjectValidationError);
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
describe('when the type is "requestBody"', function () {
|
|
323
|
+
it('should define the request body object', function () {
|
|
324
|
+
const S = new OADocumentBuilder();
|
|
325
|
+
const type = OAComponentType.REQUEST_BODY;
|
|
326
|
+
const comp = {content: {[OAMediaType.TEXT_PLAIN]: {}}};
|
|
327
|
+
S.defineComponent(type, 'comp', comp);
|
|
328
|
+
const res = S.getComponent(type, 'comp');
|
|
329
|
+
expect(res).to.be.eql(comp);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it('should define the reference object', function () {
|
|
333
|
+
const S = new OADocumentBuilder();
|
|
334
|
+
const type = OAComponentType.REQUEST_BODY;
|
|
335
|
+
const comp = {$ref: '#/components/requestBodies/target'};
|
|
336
|
+
S.defineComponent(type, 'comp', comp);
|
|
337
|
+
const res = S.getComponent(type, 'comp');
|
|
338
|
+
expect(res).to.be.eql(comp);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it('should validate the request body object', function () {
|
|
342
|
+
const S = new OADocumentBuilder();
|
|
343
|
+
const type = OAComponentType.REQUEST_BODY;
|
|
344
|
+
const invalidComp = {content: 123};
|
|
345
|
+
const throwable = () => {
|
|
346
|
+
S.defineComponent(type, 'comp', invalidComp);
|
|
347
|
+
};
|
|
348
|
+
expect(throwable).to.throw(OADocumentObjectValidationError);
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
describe('when the type is "header"', function () {
|
|
353
|
+
it('should define the header object', function () {
|
|
354
|
+
const S = new OADocumentBuilder();
|
|
355
|
+
const type = OAComponentType.HEADER;
|
|
356
|
+
const comp = {schema: {type: OADataType.STRING}};
|
|
357
|
+
S.defineComponent(type, 'comp', comp);
|
|
358
|
+
const res = S.getComponent(type, 'comp');
|
|
359
|
+
expect(res).to.be.eql(comp);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it('should define the reference object', function () {
|
|
363
|
+
const S = new OADocumentBuilder();
|
|
364
|
+
const type = OAComponentType.HEADER;
|
|
365
|
+
const comp = {$ref: '#/components/headers/target'};
|
|
366
|
+
S.defineComponent(type, 'comp', comp);
|
|
367
|
+
const res = S.getComponent(type, 'comp');
|
|
368
|
+
expect(res).to.be.eql(comp);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it('should validate the header object', function () {
|
|
372
|
+
const S = new OADocumentBuilder();
|
|
373
|
+
const type = OAComponentType.HEADER;
|
|
374
|
+
const invalidComp = {schema: 123};
|
|
375
|
+
const throwable = () => {
|
|
376
|
+
S.defineComponent(type, 'comp', invalidComp);
|
|
377
|
+
};
|
|
378
|
+
expect(throwable).to.throw(OADocumentObjectValidationError);
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
describe('when the type is "securityScheme"', function () {
|
|
383
|
+
it('should define the security scheme object', function () {
|
|
384
|
+
const S = new OADocumentBuilder();
|
|
385
|
+
const type = OAComponentType.SECURITY_SCHEME;
|
|
386
|
+
const comp = {type: OASecuritySchemeType.HTTP, scheme: 'basic'};
|
|
387
|
+
S.defineComponent(type, 'comp', comp);
|
|
388
|
+
const res = S.getComponent(type, 'comp');
|
|
389
|
+
expect(res).to.be.eql(comp);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it('should define the reference object', function () {
|
|
393
|
+
const S = new OADocumentBuilder();
|
|
394
|
+
const type = OAComponentType.SECURITY_SCHEME;
|
|
395
|
+
const comp = {$ref: '#/components/securitySchemes/target'};
|
|
396
|
+
S.defineComponent(type, 'comp', comp);
|
|
397
|
+
const res = S.getComponent(type, 'comp');
|
|
398
|
+
expect(res).to.be.eql(comp);
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
it('should validate the security scheme object', function () {
|
|
402
|
+
const S = new OADocumentBuilder();
|
|
403
|
+
const type = OAComponentType.SECURITY_SCHEME;
|
|
404
|
+
const invalidComp = {type: 123};
|
|
405
|
+
const throwable = () => {
|
|
406
|
+
S.defineComponent(type, 'comp', invalidComp);
|
|
407
|
+
};
|
|
408
|
+
expect(throwable).to.throw(OADocumentObjectValidationError);
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
describe('when the type is "link"', function () {
|
|
413
|
+
it('should define the link object', function () {
|
|
414
|
+
const S = new OADocumentBuilder();
|
|
415
|
+
const type = OAComponentType.LINK;
|
|
416
|
+
const comp = {operationId: 'id'};
|
|
417
|
+
S.defineComponent(type, 'comp', comp);
|
|
418
|
+
const res = S.getComponent(type, 'comp');
|
|
419
|
+
expect(res).to.be.eql(comp);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it('should define the reference object', function () {
|
|
423
|
+
const S = new OADocumentBuilder();
|
|
424
|
+
const type = OAComponentType.LINK;
|
|
425
|
+
const comp = {$ref: '#/components/links/target'};
|
|
426
|
+
S.defineComponent(type, 'comp', comp);
|
|
427
|
+
const res = S.getComponent(type, 'comp');
|
|
428
|
+
expect(res).to.be.eql(comp);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it('should validate the link object', function () {
|
|
432
|
+
const S = new OADocumentBuilder();
|
|
433
|
+
const type = OAComponentType.LINK;
|
|
434
|
+
const invalidComp = {operationId: 123};
|
|
435
|
+
const throwable = () => {
|
|
436
|
+
S.defineComponent(type, 'comp', invalidComp);
|
|
437
|
+
};
|
|
438
|
+
expect(throwable).to.throw(OADocumentObjectValidationError);
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
describe('when the type is "callback"', function () {
|
|
443
|
+
it('should define the callback object', function () {
|
|
444
|
+
const S = new OADocumentBuilder();
|
|
445
|
+
const type = OAComponentType.CALLBACK;
|
|
446
|
+
const comp = {
|
|
447
|
+
'{$request.query.queryUrl}': {
|
|
448
|
+
get: {
|
|
449
|
+
parameters: [
|
|
450
|
+
{
|
|
451
|
+
name: 'queryUrl',
|
|
452
|
+
in: OAParameterLocation.QUERY,
|
|
453
|
+
schema: {type: OADataType.STRING},
|
|
454
|
+
},
|
|
455
|
+
],
|
|
456
|
+
},
|
|
457
|
+
},
|
|
458
|
+
};
|
|
459
|
+
S.defineComponent(type, 'comp', comp);
|
|
460
|
+
const res = S.getComponent(type, 'comp');
|
|
461
|
+
expect(res).to.be.eql(comp);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it('should define the reference object', function () {
|
|
465
|
+
const S = new OADocumentBuilder();
|
|
466
|
+
const type = OAComponentType.CALLBACK;
|
|
467
|
+
const comp = {$ref: '#/components/callbacks/target'};
|
|
468
|
+
S.defineComponent(type, 'comp', comp);
|
|
469
|
+
const res = S.getComponent(type, 'comp');
|
|
470
|
+
expect(res).to.be.eql(comp);
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
it('should validate the callback object', function () {
|
|
474
|
+
const S = new OADocumentBuilder();
|
|
475
|
+
const type = OAComponentType.CALLBACK;
|
|
476
|
+
const invalidComp = {'{$request.query.queryUrl}': 123};
|
|
477
|
+
const throwable = () => {
|
|
478
|
+
S.defineComponent(type, 'comp', invalidComp);
|
|
479
|
+
};
|
|
480
|
+
expect(throwable).to.throw(OADocumentObjectValidationError);
|
|
481
|
+
});
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
describe('when the type is "pathItem"', function () {
|
|
485
|
+
it('should define the path item object', function () {
|
|
486
|
+
const S = new OADocumentBuilder();
|
|
487
|
+
const type = OAComponentType.PATH_ITEM;
|
|
488
|
+
const comp = {get: {}};
|
|
489
|
+
S.defineComponent(type, 'comp', comp);
|
|
490
|
+
const res = S.getComponent(type, 'comp');
|
|
491
|
+
expect(res).to.be.eql(comp);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it('should define the reference object', function () {
|
|
495
|
+
const S = new OADocumentBuilder();
|
|
496
|
+
const type = OAComponentType.PATH_ITEM;
|
|
497
|
+
const comp = {$ref: '#/components/pathItems/target'};
|
|
498
|
+
S.defineComponent(type, 'comp', comp);
|
|
499
|
+
const res = S.getComponent(type, 'comp');
|
|
500
|
+
expect(res).to.be.eql(comp);
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
it('should validate the path item object', function () {
|
|
504
|
+
const S = new OADocumentBuilder();
|
|
505
|
+
const type = OAComponentType.PATH_ITEM;
|
|
506
|
+
const invalidComp = {get: 123};
|
|
507
|
+
const throwable = () => {
|
|
508
|
+
S.defineComponent(type, 'comp', invalidComp);
|
|
509
|
+
};
|
|
510
|
+
expect(throwable).to.throw(OADocumentObjectValidationError);
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
describe('hasComponent', function () {
|
|
516
|
+
it('should require the parameter "type" to be a correct value', function () {
|
|
517
|
+
const S = new OADocumentBuilder();
|
|
518
|
+
const throwable = v => () => S.hasComponent(v, 'comp');
|
|
519
|
+
const error = s => format('Components type %s is not supported.', s);
|
|
520
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
521
|
+
expect(throwable('')).to.throw(error('""'));
|
|
522
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
523
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
524
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
525
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
526
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
527
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
528
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
529
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
530
|
+
Object.values(OAComponentType).forEach(type => throwable(type)());
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
it('should require the parameter "name" to be a non-empty String', function () {
|
|
534
|
+
const S = new OADocumentBuilder();
|
|
535
|
+
const throwable = v => () => S.hasComponent(OAComponentType.SCHEMA, v);
|
|
536
|
+
const error = s =>
|
|
537
|
+
format(
|
|
538
|
+
'Component name must be a non-empty String, but %s was given.',
|
|
539
|
+
s,
|
|
540
|
+
);
|
|
541
|
+
expect(throwable('')).to.throw(error('""'));
|
|
542
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
543
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
544
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
545
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
546
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
547
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
548
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
549
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
550
|
+
throwable('comp')();
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it('should require the parameter "name" to not contain invalid characters', function () {
|
|
554
|
+
const S = new OADocumentBuilder();
|
|
555
|
+
const throwable = v => () => S.hasComponent(OAComponentType.SCHEMA, v);
|
|
556
|
+
const error = s =>
|
|
557
|
+
format('Component name %s contains invalid characters.', s);
|
|
558
|
+
expect(throwable('/')).to.throw(error('"/"'));
|
|
559
|
+
expect(throwable('$')).to.throw(error('"$"'));
|
|
560
|
+
expect(throwable('~')).to.throw(error('"~"'));
|
|
561
|
+
throwable('comp')();
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
describe('when the type is "schema"', function () {
|
|
565
|
+
it('should return true when the schema object is registered', function () {
|
|
566
|
+
const S = new OADocumentBuilder();
|
|
567
|
+
const type = OAComponentType.SCHEMA;
|
|
568
|
+
const comp = {type: OADataType.STRING};
|
|
569
|
+
expect(S.hasComponent(type, 'comp')).to.be.false;
|
|
570
|
+
S.defineComponent(type, 'comp', comp);
|
|
571
|
+
expect(S.hasComponent(type, 'comp')).to.be.true;
|
|
572
|
+
});
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
describe('when the type is "response"', function () {
|
|
576
|
+
it('should return true when the response object is registered', function () {
|
|
577
|
+
const S = new OADocumentBuilder();
|
|
578
|
+
const type = OAComponentType.RESPONSE;
|
|
579
|
+
const comp = {description: 'Response'};
|
|
580
|
+
expect(S.hasComponent(type, 'comp')).to.be.false;
|
|
581
|
+
S.defineComponent(type, 'comp', comp);
|
|
582
|
+
expect(S.hasComponent(type, 'comp')).to.be.true;
|
|
583
|
+
});
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
describe('when the type is "parameter"', function () {
|
|
587
|
+
it('should return true when the parameter object is registered', function () {
|
|
588
|
+
const S = new OADocumentBuilder();
|
|
589
|
+
const type = OAComponentType.PARAMETER;
|
|
590
|
+
const comp = {
|
|
591
|
+
name: 'param',
|
|
592
|
+
in: OAParameterLocation.QUERY,
|
|
593
|
+
schema: {type: OADataType.STRING},
|
|
594
|
+
};
|
|
595
|
+
expect(S.hasComponent(type, 'comp')).to.be.false;
|
|
596
|
+
S.defineComponent(type, 'comp', comp);
|
|
597
|
+
expect(S.hasComponent(type, 'comp')).to.be.true;
|
|
598
|
+
});
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
describe('when the type is "example"', function () {
|
|
602
|
+
it('should return true when the example object is registered', function () {
|
|
603
|
+
const S = new OADocumentBuilder();
|
|
604
|
+
const type = OAComponentType.EXAMPLE;
|
|
605
|
+
const comp = {value: 10};
|
|
606
|
+
expect(S.hasComponent(type, 'comp')).to.be.false;
|
|
607
|
+
S.defineComponent(type, 'comp', comp);
|
|
608
|
+
expect(S.hasComponent(type, 'comp')).to.be.true;
|
|
609
|
+
});
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
describe('when the type is "requestBody"', function () {
|
|
613
|
+
it('should return true when the request body object is registered', function () {
|
|
614
|
+
const S = new OADocumentBuilder();
|
|
615
|
+
const type = OAComponentType.REQUEST_BODY;
|
|
616
|
+
const comp = {content: {[OAMediaType.TEXT_PLAIN]: {}}};
|
|
617
|
+
expect(S.hasComponent(type, 'comp')).to.be.false;
|
|
618
|
+
S.defineComponent(type, 'comp', comp);
|
|
619
|
+
expect(S.hasComponent(type, 'comp')).to.be.true;
|
|
620
|
+
});
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
describe('when the type is "header"', function () {
|
|
624
|
+
it('should return true when the header object is registered', function () {
|
|
625
|
+
const S = new OADocumentBuilder();
|
|
626
|
+
const type = OAComponentType.HEADER;
|
|
627
|
+
const comp = {schema: {type: OADataType.STRING}};
|
|
628
|
+
expect(S.hasComponent(type, 'comp')).to.be.false;
|
|
629
|
+
S.defineComponent(type, 'comp', comp);
|
|
630
|
+
expect(S.hasComponent(type, 'comp')).to.be.true;
|
|
631
|
+
});
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
describe('when the type is "securityScheme"', function () {
|
|
635
|
+
it('should return true when the security scheme object is registered', function () {
|
|
636
|
+
const S = new OADocumentBuilder();
|
|
637
|
+
const type = OAComponentType.SECURITY_SCHEME;
|
|
638
|
+
const comp = {type: OASecuritySchemeType.HTTP, scheme: 'basic'};
|
|
639
|
+
expect(S.hasComponent(type, 'comp')).to.be.false;
|
|
640
|
+
S.defineComponent(type, 'comp', comp);
|
|
641
|
+
expect(S.hasComponent(type, 'comp')).to.be.true;
|
|
642
|
+
});
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
describe('when the type is "link"', function () {
|
|
646
|
+
it('should return true when the link object is registered', function () {
|
|
647
|
+
const S = new OADocumentBuilder();
|
|
648
|
+
const type = OAComponentType.LINK;
|
|
649
|
+
const comp = {operationId: 'id'};
|
|
650
|
+
expect(S.hasComponent(type, 'comp')).to.be.false;
|
|
651
|
+
S.defineComponent(type, 'comp', comp);
|
|
652
|
+
expect(S.hasComponent(type, 'comp')).to.be.true;
|
|
653
|
+
});
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
describe('when the type is "callback"', function () {
|
|
657
|
+
it('should return true when the callback object is registered', function () {
|
|
658
|
+
const S = new OADocumentBuilder();
|
|
659
|
+
const type = OAComponentType.CALLBACK;
|
|
660
|
+
const comp = {
|
|
661
|
+
'{$request.query.queryUrl}': {
|
|
662
|
+
get: {
|
|
663
|
+
parameters: [
|
|
664
|
+
{
|
|
665
|
+
name: 'queryUrl',
|
|
666
|
+
in: OAParameterLocation.QUERY,
|
|
667
|
+
schema: {type: OADataType.STRING},
|
|
668
|
+
},
|
|
669
|
+
],
|
|
670
|
+
},
|
|
671
|
+
},
|
|
672
|
+
};
|
|
673
|
+
expect(S.hasComponent(type, 'comp')).to.be.false;
|
|
674
|
+
S.defineComponent(type, 'comp', comp);
|
|
675
|
+
expect(S.hasComponent(type, 'comp')).to.be.true;
|
|
676
|
+
});
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
describe('when the type is "pathItem"', function () {
|
|
680
|
+
it('should return true when the path item object is registered', function () {
|
|
681
|
+
const S = new OADocumentBuilder();
|
|
682
|
+
const type = OAComponentType.PATH_ITEM;
|
|
683
|
+
const comp = {get: {}};
|
|
684
|
+
expect(S.hasComponent(type, 'comp')).to.be.false;
|
|
685
|
+
S.defineComponent(type, 'comp', comp);
|
|
686
|
+
expect(S.hasComponent(type, 'comp')).to.be.true;
|
|
687
|
+
});
|
|
688
|
+
});
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
describe('getComponent', function () {
|
|
692
|
+
it('should require the parameter "type" to be a correct value', function () {
|
|
693
|
+
const S = new OADocumentBuilder();
|
|
694
|
+
S.defineComponent(OAComponentType.SCHEMA, 'comp', {
|
|
695
|
+
type: OADataType.STRING,
|
|
696
|
+
});
|
|
697
|
+
const throwable = v => () => S.getComponent(v, 'comp');
|
|
698
|
+
const error = s => format('Components type %s is not supported.', s);
|
|
699
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
700
|
+
expect(throwable('')).to.throw(error('""'));
|
|
701
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
702
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
703
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
704
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
705
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
706
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
707
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
708
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
709
|
+
throwable(OAComponentType.SCHEMA)();
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
it('should require the parameter "name" to be a non-empty String', function () {
|
|
713
|
+
const S = new OADocumentBuilder();
|
|
714
|
+
const type = OAComponentType.SCHEMA;
|
|
715
|
+
S.defineComponent(type, 'comp', {type: OADataType.STRING});
|
|
716
|
+
const throwable = v => () => S.getComponent(type, v);
|
|
717
|
+
const error = s =>
|
|
718
|
+
format(
|
|
719
|
+
'Component name must be a non-empty String, but %s was given.',
|
|
720
|
+
s,
|
|
721
|
+
);
|
|
722
|
+
expect(throwable('')).to.throw(error('""'));
|
|
723
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
724
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
725
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
726
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
727
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
728
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
729
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
730
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
731
|
+
throwable('comp')();
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
it('should require the parameter "name" to not contain invalid characters', function () {
|
|
735
|
+
const S = new OADocumentBuilder();
|
|
736
|
+
const type = OAComponentType.SCHEMA;
|
|
737
|
+
S.defineComponent(type, 'comp', {type: OADataType.STRING});
|
|
738
|
+
const throwable = v => () => S.getComponent(type, v);
|
|
739
|
+
const error = s =>
|
|
740
|
+
format('Component name %s contains invalid characters.', s);
|
|
741
|
+
expect(throwable('/')).to.throw(error('"/"'));
|
|
742
|
+
expect(throwable('$')).to.throw(error('"$"'));
|
|
743
|
+
expect(throwable('~')).to.throw(error('"~"'));
|
|
744
|
+
throwable('comp')();
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
describe('when the type is "schema"', function () {
|
|
748
|
+
it('should return the schema object for the registered name', function () {
|
|
749
|
+
const S = new OADocumentBuilder();
|
|
750
|
+
const type = OAComponentType.SCHEMA;
|
|
751
|
+
const comp = {type: OADataType.STRING};
|
|
752
|
+
S.defineComponent(type, 'comp', comp);
|
|
753
|
+
const res = S.getComponent(type, 'comp');
|
|
754
|
+
expect(res).to.be.eql(comp);
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
it('should throw an error when the component is not registered', function () {
|
|
758
|
+
const S = new OADocumentBuilder();
|
|
759
|
+
const type = OAComponentType.SCHEMA;
|
|
760
|
+
const throwable = () => S.getComponent(type, 'comp');
|
|
761
|
+
expect(throwable).to.throw(
|
|
762
|
+
'Component "#/components/schemas/comp" does not exist.',
|
|
763
|
+
);
|
|
764
|
+
});
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
describe('when the type is "response"', function () {
|
|
768
|
+
it('should return the response object for the registered name', function () {
|
|
769
|
+
const S = new OADocumentBuilder();
|
|
770
|
+
const type = OAComponentType.RESPONSE;
|
|
771
|
+
const comp = {description: 'Response'};
|
|
772
|
+
S.defineComponent(type, 'comp', comp);
|
|
773
|
+
const res = S.getComponent(type, 'comp');
|
|
774
|
+
expect(res).to.be.eql(comp);
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
it('should throw an error when the component is not registered', function () {
|
|
778
|
+
const S = new OADocumentBuilder();
|
|
779
|
+
const type = OAComponentType.RESPONSE;
|
|
780
|
+
const throwable = () => S.getComponent(type, 'comp');
|
|
781
|
+
expect(throwable).to.throw(
|
|
782
|
+
'Component "#/components/responses/comp" does not exist.',
|
|
783
|
+
);
|
|
784
|
+
});
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
describe('when the type is "parameter"', function () {
|
|
788
|
+
it('should return the parameter object for the registered name', function () {
|
|
789
|
+
const S = new OADocumentBuilder();
|
|
790
|
+
const type = OAComponentType.PARAMETER;
|
|
791
|
+
const comp = {
|
|
792
|
+
name: 'param',
|
|
793
|
+
in: OAParameterLocation.QUERY,
|
|
794
|
+
schema: {type: OADataType.STRING},
|
|
795
|
+
};
|
|
796
|
+
S.defineComponent(type, 'comp', comp);
|
|
797
|
+
const res = S.getComponent(type, 'comp');
|
|
798
|
+
expect(res).to.be.eql(comp);
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
it('should throw an error when the component is not registered', function () {
|
|
802
|
+
const S = new OADocumentBuilder();
|
|
803
|
+
const type = OAComponentType.PARAMETER;
|
|
804
|
+
const throwable = () => S.getComponent(type, 'comp');
|
|
805
|
+
expect(throwable).to.throw(
|
|
806
|
+
'Component "#/components/parameters/comp" does not exist.',
|
|
807
|
+
);
|
|
808
|
+
});
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
describe('when the type is "example"', function () {
|
|
812
|
+
it('should return the example object for the registered name', function () {
|
|
813
|
+
const S = new OADocumentBuilder();
|
|
814
|
+
const type = OAComponentType.EXAMPLE;
|
|
815
|
+
const comp = {value: 10};
|
|
816
|
+
S.defineComponent(type, 'comp', comp);
|
|
817
|
+
const res = S.getComponent(type, 'comp');
|
|
818
|
+
expect(res).to.be.eql(comp);
|
|
819
|
+
});
|
|
820
|
+
|
|
821
|
+
it('should throw an error when the component is not registered', function () {
|
|
822
|
+
const S = new OADocumentBuilder();
|
|
823
|
+
const type = OAComponentType.EXAMPLE;
|
|
824
|
+
const throwable = () => S.getComponent(type, 'comp');
|
|
825
|
+
expect(throwable).to.throw(
|
|
826
|
+
'Component "#/components/examples/comp" does not exist.',
|
|
827
|
+
);
|
|
828
|
+
});
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
describe('when the type is "requestBody"', function () {
|
|
832
|
+
it('should return the request body object for the registered name', function () {
|
|
833
|
+
const S = new OADocumentBuilder();
|
|
834
|
+
const type = OAComponentType.REQUEST_BODY;
|
|
835
|
+
const comp = {content: {[OAMediaType.TEXT_PLAIN]: {}}};
|
|
836
|
+
S.defineComponent(type, 'comp', comp);
|
|
837
|
+
const res = S.getComponent(type, 'comp');
|
|
838
|
+
expect(res).to.be.eql(comp);
|
|
839
|
+
});
|
|
840
|
+
|
|
841
|
+
it('should throw an error when the component is not registered', function () {
|
|
842
|
+
const S = new OADocumentBuilder();
|
|
843
|
+
const type = OAComponentType.REQUEST_BODY;
|
|
844
|
+
const throwable = () => S.getComponent(type, 'comp');
|
|
845
|
+
expect(throwable).to.throw(
|
|
846
|
+
'Component "#/components/requestBodies/comp" does not exist.',
|
|
847
|
+
);
|
|
848
|
+
});
|
|
849
|
+
});
|
|
850
|
+
|
|
851
|
+
describe('when the type is "header"', function () {
|
|
852
|
+
it('should return the header object for the registered name', function () {
|
|
853
|
+
const S = new OADocumentBuilder();
|
|
854
|
+
const type = OAComponentType.HEADER;
|
|
855
|
+
const comp = {schema: {type: OADataType.STRING}};
|
|
856
|
+
S.defineComponent(type, 'comp', comp);
|
|
857
|
+
const res = S.getComponent(type, 'comp');
|
|
858
|
+
expect(res).to.be.eql(comp);
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
it('should throw an error when the component is not registered', function () {
|
|
862
|
+
const S = new OADocumentBuilder();
|
|
863
|
+
const type = OAComponentType.HEADER;
|
|
864
|
+
const throwable = () => S.getComponent(type, 'comp');
|
|
865
|
+
expect(throwable).to.throw(
|
|
866
|
+
'Component "#/components/headers/comp" does not exist.',
|
|
867
|
+
);
|
|
868
|
+
});
|
|
869
|
+
});
|
|
870
|
+
|
|
871
|
+
describe('when the type is "securityScheme"', function () {
|
|
872
|
+
it('should return the security scheme object for the registered name', function () {
|
|
873
|
+
const S = new OADocumentBuilder();
|
|
874
|
+
const type = OAComponentType.SECURITY_SCHEME;
|
|
875
|
+
const comp = {type: OASecuritySchemeType.HTTP, scheme: 'basic'};
|
|
876
|
+
S.defineComponent(type, 'comp', comp);
|
|
877
|
+
const res = S.getComponent(type, 'comp');
|
|
878
|
+
expect(res).to.be.eql(comp);
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
it('should throw an error when the component is not registered', function () {
|
|
882
|
+
const S = new OADocumentBuilder();
|
|
883
|
+
const type = OAComponentType.SECURITY_SCHEME;
|
|
884
|
+
const throwable = () => S.getComponent(type, 'comp');
|
|
885
|
+
expect(throwable).to.throw(
|
|
886
|
+
'Component "#/components/securitySchemes/comp" does not exist.',
|
|
887
|
+
);
|
|
888
|
+
});
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
describe('when the type is "link"', function () {
|
|
892
|
+
it('should return the link object for the registered name', function () {
|
|
893
|
+
const S = new OADocumentBuilder();
|
|
894
|
+
const type = OAComponentType.LINK;
|
|
895
|
+
const comp = {operationId: 'id'};
|
|
896
|
+
S.defineComponent(type, 'comp', comp);
|
|
897
|
+
const res = S.getComponent(type, 'comp');
|
|
898
|
+
expect(res).to.be.eql(comp);
|
|
899
|
+
});
|
|
900
|
+
|
|
901
|
+
it('should throw an error when the component is not registered', function () {
|
|
902
|
+
const S = new OADocumentBuilder();
|
|
903
|
+
const type = OAComponentType.LINK;
|
|
904
|
+
const throwable = () => S.getComponent(type, 'comp');
|
|
905
|
+
expect(throwable).to.throw(
|
|
906
|
+
'Component "#/components/links/comp" does not exist.',
|
|
907
|
+
);
|
|
908
|
+
});
|
|
909
|
+
});
|
|
910
|
+
|
|
911
|
+
describe('when the type is "callback"', function () {
|
|
912
|
+
it('should return the callback object for the registered name', function () {
|
|
913
|
+
const S = new OADocumentBuilder();
|
|
914
|
+
const type = OAComponentType.CALLBACK;
|
|
915
|
+
const comp = {
|
|
916
|
+
'{$request.query.queryUrl}': {
|
|
917
|
+
get: {
|
|
918
|
+
parameters: [
|
|
919
|
+
{
|
|
920
|
+
name: 'queryUrl',
|
|
921
|
+
in: OAParameterLocation.QUERY,
|
|
922
|
+
schema: {type: OADataType.STRING},
|
|
923
|
+
},
|
|
924
|
+
],
|
|
925
|
+
},
|
|
926
|
+
},
|
|
927
|
+
};
|
|
928
|
+
S.defineComponent(type, 'comp', comp);
|
|
929
|
+
const res = S.getComponent(type, 'comp');
|
|
930
|
+
expect(res).to.be.eql(comp);
|
|
931
|
+
});
|
|
932
|
+
|
|
933
|
+
it('should throw an error when the component is not registered', function () {
|
|
934
|
+
const S = new OADocumentBuilder();
|
|
935
|
+
const type = OAComponentType.CALLBACK;
|
|
936
|
+
const throwable = () => S.getComponent(type, 'comp');
|
|
937
|
+
expect(throwable).to.throw(
|
|
938
|
+
'Component "#/components/callbacks/comp" does not exist.',
|
|
939
|
+
);
|
|
940
|
+
});
|
|
941
|
+
});
|
|
942
|
+
|
|
943
|
+
describe('when the type is "pathItem"', function () {
|
|
944
|
+
it('should return the path item object for the registered name', function () {
|
|
945
|
+
const S = new OADocumentBuilder();
|
|
946
|
+
const type = OAComponentType.PATH_ITEM;
|
|
947
|
+
const comp = {get: {}};
|
|
948
|
+
S.defineComponent(type, 'comp', comp);
|
|
949
|
+
const res = S.getComponent(type, 'comp');
|
|
950
|
+
expect(res).to.be.eql(comp);
|
|
951
|
+
});
|
|
952
|
+
|
|
953
|
+
it('should throw an error when the component is not registered', function () {
|
|
954
|
+
const S = new OADocumentBuilder();
|
|
955
|
+
const type = OAComponentType.PATH_ITEM;
|
|
956
|
+
const throwable = () => S.getComponent(type, 'comp');
|
|
957
|
+
expect(throwable).to.throw(
|
|
958
|
+
'Component "#/components/pathItems/comp" does not exist.',
|
|
959
|
+
);
|
|
960
|
+
});
|
|
961
|
+
});
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
describe('defineSchemaComponent', function () {
|
|
965
|
+
it('should invoke the method "defineComponent" with correct arguments', function () {
|
|
966
|
+
const S = new OADocumentBuilder();
|
|
967
|
+
const type = OAComponentType.SCHEMA;
|
|
968
|
+
const name = 'comp';
|
|
969
|
+
const component = {};
|
|
970
|
+
let invoked = 0;
|
|
971
|
+
S.defineComponent = (...args) => {
|
|
972
|
+
expect(args[0]).to.be.eq(type);
|
|
973
|
+
expect(args[1]).to.be.eq(name);
|
|
974
|
+
expect(args[2]).to.be.eq(component);
|
|
975
|
+
invoked++;
|
|
976
|
+
return 'OK';
|
|
977
|
+
};
|
|
978
|
+
const res = S.defineSchemaComponent(name, component);
|
|
979
|
+
expect(res).to.be.eq('OK');
|
|
980
|
+
expect(invoked).to.be.eq(1);
|
|
981
|
+
});
|
|
982
|
+
});
|
|
983
|
+
|
|
984
|
+
describe('defineResponseComponent', function () {
|
|
985
|
+
it('should invoke the method "defineComponent" with correct arguments', function () {
|
|
986
|
+
const S = new OADocumentBuilder();
|
|
987
|
+
const type = OAComponentType.RESPONSE;
|
|
988
|
+
const name = 'comp';
|
|
989
|
+
const component = {};
|
|
990
|
+
let invoked = 0;
|
|
991
|
+
S.defineComponent = (...args) => {
|
|
992
|
+
expect(args[0]).to.be.eq(type);
|
|
993
|
+
expect(args[1]).to.be.eq(name);
|
|
994
|
+
expect(args[2]).to.be.eq(component);
|
|
995
|
+
invoked++;
|
|
996
|
+
return 'OK';
|
|
997
|
+
};
|
|
998
|
+
const res = S.defineResponseComponent(name, component);
|
|
999
|
+
expect(res).to.be.eq('OK');
|
|
1000
|
+
expect(invoked).to.be.eq(1);
|
|
1001
|
+
});
|
|
1002
|
+
});
|
|
1003
|
+
|
|
1004
|
+
describe('defineParameterComponent', function () {
|
|
1005
|
+
it('should invoke the method "defineComponent" with correct arguments', function () {
|
|
1006
|
+
const S = new OADocumentBuilder();
|
|
1007
|
+
const type = OAComponentType.PARAMETER;
|
|
1008
|
+
const name = 'comp';
|
|
1009
|
+
const component = {};
|
|
1010
|
+
let invoked = 0;
|
|
1011
|
+
S.defineComponent = (...args) => {
|
|
1012
|
+
expect(args[0]).to.be.eq(type);
|
|
1013
|
+
expect(args[1]).to.be.eq(name);
|
|
1014
|
+
expect(args[2]).to.be.eq(component);
|
|
1015
|
+
invoked++;
|
|
1016
|
+
return 'OK';
|
|
1017
|
+
};
|
|
1018
|
+
const res = S.defineParameterComponent(name, component);
|
|
1019
|
+
expect(res).to.be.eq('OK');
|
|
1020
|
+
expect(invoked).to.be.eq(1);
|
|
1021
|
+
});
|
|
1022
|
+
});
|
|
1023
|
+
|
|
1024
|
+
describe('defineExampleComponent', function () {
|
|
1025
|
+
it('should invoke the method "defineComponent" with correct arguments', function () {
|
|
1026
|
+
const S = new OADocumentBuilder();
|
|
1027
|
+
const type = OAComponentType.EXAMPLE;
|
|
1028
|
+
const name = 'comp';
|
|
1029
|
+
const component = {};
|
|
1030
|
+
let invoked = 0;
|
|
1031
|
+
S.defineComponent = (...args) => {
|
|
1032
|
+
expect(args[0]).to.be.eq(type);
|
|
1033
|
+
expect(args[1]).to.be.eq(name);
|
|
1034
|
+
expect(args[2]).to.be.eq(component);
|
|
1035
|
+
invoked++;
|
|
1036
|
+
return 'OK';
|
|
1037
|
+
};
|
|
1038
|
+
const res = S.defineExampleComponent(name, component);
|
|
1039
|
+
expect(res).to.be.eq('OK');
|
|
1040
|
+
expect(invoked).to.be.eq(1);
|
|
1041
|
+
});
|
|
1042
|
+
});
|
|
1043
|
+
|
|
1044
|
+
describe('defineRequestBodyComponent', function () {
|
|
1045
|
+
it('should invoke the method "defineComponent" with correct arguments', function () {
|
|
1046
|
+
const S = new OADocumentBuilder();
|
|
1047
|
+
const type = OAComponentType.REQUEST_BODY;
|
|
1048
|
+
const name = 'comp';
|
|
1049
|
+
const component = {};
|
|
1050
|
+
let invoked = 0;
|
|
1051
|
+
S.defineComponent = (...args) => {
|
|
1052
|
+
expect(args[0]).to.be.eq(type);
|
|
1053
|
+
expect(args[1]).to.be.eq(name);
|
|
1054
|
+
expect(args[2]).to.be.eq(component);
|
|
1055
|
+
invoked++;
|
|
1056
|
+
return 'OK';
|
|
1057
|
+
};
|
|
1058
|
+
const res = S.defineRequestBodyComponent(name, component);
|
|
1059
|
+
expect(res).to.be.eq('OK');
|
|
1060
|
+
expect(invoked).to.be.eq(1);
|
|
1061
|
+
});
|
|
1062
|
+
});
|
|
1063
|
+
|
|
1064
|
+
describe('defineHeaderComponent', function () {
|
|
1065
|
+
it('should invoke the method "defineComponent" with correct arguments', function () {
|
|
1066
|
+
const S = new OADocumentBuilder();
|
|
1067
|
+
const type = OAComponentType.HEADER;
|
|
1068
|
+
const name = 'comp';
|
|
1069
|
+
const component = {};
|
|
1070
|
+
let invoked = 0;
|
|
1071
|
+
S.defineComponent = (...args) => {
|
|
1072
|
+
expect(args[0]).to.be.eq(type);
|
|
1073
|
+
expect(args[1]).to.be.eq(name);
|
|
1074
|
+
expect(args[2]).to.be.eq(component);
|
|
1075
|
+
invoked++;
|
|
1076
|
+
return 'OK';
|
|
1077
|
+
};
|
|
1078
|
+
const res = S.defineHeaderComponent(name, component);
|
|
1079
|
+
expect(res).to.be.eq('OK');
|
|
1080
|
+
expect(invoked).to.be.eq(1);
|
|
1081
|
+
});
|
|
1082
|
+
});
|
|
1083
|
+
|
|
1084
|
+
describe('defineSecuritySchemeComponent', function () {
|
|
1085
|
+
it('should invoke the method "defineComponent" with correct arguments', function () {
|
|
1086
|
+
const S = new OADocumentBuilder();
|
|
1087
|
+
const type = OAComponentType.SECURITY_SCHEME;
|
|
1088
|
+
const name = 'comp';
|
|
1089
|
+
const component = {};
|
|
1090
|
+
let invoked = 0;
|
|
1091
|
+
S.defineComponent = (...args) => {
|
|
1092
|
+
expect(args[0]).to.be.eq(type);
|
|
1093
|
+
expect(args[1]).to.be.eq(name);
|
|
1094
|
+
expect(args[2]).to.be.eq(component);
|
|
1095
|
+
invoked++;
|
|
1096
|
+
return 'OK';
|
|
1097
|
+
};
|
|
1098
|
+
const res = S.defineSecuritySchemeComponent(name, component);
|
|
1099
|
+
expect(res).to.be.eq('OK');
|
|
1100
|
+
expect(invoked).to.be.eq(1);
|
|
1101
|
+
});
|
|
1102
|
+
});
|
|
1103
|
+
|
|
1104
|
+
describe('defineLinkComponent', function () {
|
|
1105
|
+
it('should invoke the method "defineComponent" with correct arguments', function () {
|
|
1106
|
+
const S = new OADocumentBuilder();
|
|
1107
|
+
const type = OAComponentType.LINK;
|
|
1108
|
+
const name = 'comp';
|
|
1109
|
+
const component = {};
|
|
1110
|
+
let invoked = 0;
|
|
1111
|
+
S.defineComponent = (...args) => {
|
|
1112
|
+
expect(args[0]).to.be.eq(type);
|
|
1113
|
+
expect(args[1]).to.be.eq(name);
|
|
1114
|
+
expect(args[2]).to.be.eq(component);
|
|
1115
|
+
invoked++;
|
|
1116
|
+
return 'OK';
|
|
1117
|
+
};
|
|
1118
|
+
const res = S.defineLinkComponent(name, component);
|
|
1119
|
+
expect(res).to.be.eq('OK');
|
|
1120
|
+
expect(invoked).to.be.eq(1);
|
|
1121
|
+
});
|
|
1122
|
+
});
|
|
1123
|
+
|
|
1124
|
+
describe('defineCallbackComponent', function () {
|
|
1125
|
+
it('should invoke the method "defineComponent" with correct arguments', function () {
|
|
1126
|
+
const S = new OADocumentBuilder();
|
|
1127
|
+
const type = OAComponentType.CALLBACK;
|
|
1128
|
+
const name = 'comp';
|
|
1129
|
+
const component = {};
|
|
1130
|
+
let invoked = 0;
|
|
1131
|
+
S.defineComponent = (...args) => {
|
|
1132
|
+
expect(args[0]).to.be.eq(type);
|
|
1133
|
+
expect(args[1]).to.be.eq(name);
|
|
1134
|
+
expect(args[2]).to.be.eq(component);
|
|
1135
|
+
invoked++;
|
|
1136
|
+
return 'OK';
|
|
1137
|
+
};
|
|
1138
|
+
const res = S.defineCallbackComponent(name, component);
|
|
1139
|
+
expect(res).to.be.eq('OK');
|
|
1140
|
+
expect(invoked).to.be.eq(1);
|
|
1141
|
+
});
|
|
1142
|
+
});
|
|
1143
|
+
|
|
1144
|
+
describe('definePathItemComponent', function () {
|
|
1145
|
+
it('should invoke the method "defineComponent" with correct arguments', function () {
|
|
1146
|
+
const S = new OADocumentBuilder();
|
|
1147
|
+
const type = OAComponentType.PATH_ITEM;
|
|
1148
|
+
const name = 'comp';
|
|
1149
|
+
const component = {};
|
|
1150
|
+
let invoked = 0;
|
|
1151
|
+
S.defineComponent = (...args) => {
|
|
1152
|
+
expect(args[0]).to.be.eq(type);
|
|
1153
|
+
expect(args[1]).to.be.eq(name);
|
|
1154
|
+
expect(args[2]).to.be.eq(component);
|
|
1155
|
+
invoked++;
|
|
1156
|
+
return 'OK';
|
|
1157
|
+
};
|
|
1158
|
+
const res = S.definePathItemComponent(name, component);
|
|
1159
|
+
expect(res).to.be.eq('OK');
|
|
1160
|
+
expect(invoked).to.be.eq(1);
|
|
1161
|
+
});
|
|
1162
|
+
});
|
|
1163
|
+
|
|
1164
|
+
describe('defineOperation', function () {
|
|
1165
|
+
it('should require the parameter "operationDef" to be an Object', function () {
|
|
1166
|
+
const throwable = v => () => {
|
|
1167
|
+
const builder = new OADocumentBuilder();
|
|
1168
|
+
builder.defineOperation(v);
|
|
1169
|
+
};
|
|
1170
|
+
const error = s =>
|
|
1171
|
+
format('Operation Definition must be an Object, but %s was given.', s);
|
|
1172
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
1173
|
+
expect(throwable('')).to.throw(error('""'));
|
|
1174
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
1175
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
1176
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
1177
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
1178
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
1179
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
1180
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
1181
|
+
throwable({
|
|
1182
|
+
method: OAOperationMethod.GET,
|
|
1183
|
+
path: '/',
|
|
1184
|
+
})();
|
|
1185
|
+
});
|
|
1186
|
+
|
|
1187
|
+
it('should require the keyword "path" to be a String', function () {
|
|
1188
|
+
const throwable = v => () => {
|
|
1189
|
+
const S = new OADocumentBuilder();
|
|
1190
|
+
S.defineOperation({
|
|
1191
|
+
method: OAOperationMethod.GET,
|
|
1192
|
+
path: v,
|
|
1193
|
+
});
|
|
1194
|
+
};
|
|
1195
|
+
const error = s =>
|
|
1196
|
+
format('Keyword "path" must be a String, but %s was given.', s);
|
|
1197
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
1198
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
1199
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
1200
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
1201
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
1202
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
1203
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
1204
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
1205
|
+
throwable('/path')();
|
|
1206
|
+
});
|
|
1207
|
+
|
|
1208
|
+
it('should require the keyword "path" to start with a forward slash', function () {
|
|
1209
|
+
const throwable = v => () => {
|
|
1210
|
+
const S = new OADocumentBuilder();
|
|
1211
|
+
S.defineOperation({
|
|
1212
|
+
method: OAOperationMethod.GET,
|
|
1213
|
+
path: v,
|
|
1214
|
+
});
|
|
1215
|
+
};
|
|
1216
|
+
const error = s =>
|
|
1217
|
+
format('Keyword "path" must start with "/", but %s was given.', s);
|
|
1218
|
+
expect(throwable('path')).to.throw(error('"path"'));
|
|
1219
|
+
expect(throwable('')).to.throw(error('""'));
|
|
1220
|
+
throwable('/path')();
|
|
1221
|
+
throwable('/')();
|
|
1222
|
+
});
|
|
1223
|
+
|
|
1224
|
+
it('should require the keyword "method" to be a correct value', function () {
|
|
1225
|
+
const throwable = v => () => {
|
|
1226
|
+
const S = new OADocumentBuilder();
|
|
1227
|
+
S.defineOperation({
|
|
1228
|
+
method: v,
|
|
1229
|
+
path: '/',
|
|
1230
|
+
});
|
|
1231
|
+
};
|
|
1232
|
+
const error = s =>
|
|
1233
|
+
format(
|
|
1234
|
+
'Keyword "method" must be one of the allowed values, ' +
|
|
1235
|
+
'but %s was given.',
|
|
1236
|
+
s,
|
|
1237
|
+
);
|
|
1238
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
1239
|
+
expect(throwable('')).to.throw(error('""'));
|
|
1240
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
1241
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
1242
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
1243
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
1244
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
1245
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
1246
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
1247
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
1248
|
+
Object.values(OAOperationMethod).forEach(method => {
|
|
1249
|
+
throwable(method)();
|
|
1250
|
+
});
|
|
1251
|
+
});
|
|
1252
|
+
|
|
1253
|
+
it('should require the keyword "operation" to be an Object', function () {
|
|
1254
|
+
const throwable = v => () => {
|
|
1255
|
+
const builder = new OADocumentBuilder();
|
|
1256
|
+
builder.defineOperation({
|
|
1257
|
+
method: OAOperationMethod.GET,
|
|
1258
|
+
path: '/',
|
|
1259
|
+
operation: v,
|
|
1260
|
+
});
|
|
1261
|
+
};
|
|
1262
|
+
const error = s =>
|
|
1263
|
+
format('Keyword "operation" must be an Object, but %s was given.', s);
|
|
1264
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
1265
|
+
expect(throwable('')).to.throw(error('""'));
|
|
1266
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
1267
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
1268
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
1269
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
1270
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
1271
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
1272
|
+
throwable({})();
|
|
1273
|
+
throwable(undefined)();
|
|
1274
|
+
});
|
|
1275
|
+
|
|
1276
|
+
it('should validate the operation object', function () {
|
|
1277
|
+
const S = new OADocumentBuilder();
|
|
1278
|
+
const throwable = () => {
|
|
1279
|
+
S.defineOperation({
|
|
1280
|
+
method: OAOperationMethod.GET,
|
|
1281
|
+
path: '/',
|
|
1282
|
+
operation: {parameters: 123},
|
|
1283
|
+
});
|
|
1284
|
+
};
|
|
1285
|
+
expect(throwable).to.throw(
|
|
1286
|
+
OADocumentObjectValidationError,
|
|
1287
|
+
'Value at "#/paths/~1/get/parameters" must be array.',
|
|
1288
|
+
);
|
|
1289
|
+
});
|
|
1290
|
+
|
|
1291
|
+
it('should define the operation object', function () {
|
|
1292
|
+
const S = new OADocumentBuilder();
|
|
1293
|
+
const operation = {
|
|
1294
|
+
parameters: [
|
|
1295
|
+
{
|
|
1296
|
+
name: 'id',
|
|
1297
|
+
in: OAParameterLocation.PATH,
|
|
1298
|
+
required: true,
|
|
1299
|
+
schema: {type: OADataType.OBJECT},
|
|
1300
|
+
},
|
|
1301
|
+
],
|
|
1302
|
+
};
|
|
1303
|
+
S.defineOperation({
|
|
1304
|
+
method: OAOperationMethod.GET,
|
|
1305
|
+
path: '/users/{id}',
|
|
1306
|
+
operation,
|
|
1307
|
+
});
|
|
1308
|
+
const doc = S.getDocumentObjectRef();
|
|
1309
|
+
expect(doc.paths['/users/{id}'].get).to.be.eql(operation);
|
|
1310
|
+
});
|
|
1311
|
+
|
|
1312
|
+
it('should return the current instance', function () {
|
|
1313
|
+
const S = new OADocumentBuilder();
|
|
1314
|
+
const res = S.defineOperation({
|
|
1315
|
+
method: OAOperationMethod.GET,
|
|
1316
|
+
path: '/users/{id}',
|
|
1317
|
+
});
|
|
1318
|
+
expect(res).to.be.eq(S);
|
|
1319
|
+
});
|
|
1320
|
+
});
|
|
1321
|
+
|
|
1322
|
+
describe('createOperationGroup', function () {
|
|
1323
|
+
it('should return an instance of OAOperationGroup', function () {
|
|
1324
|
+
const S = new OADocumentBuilder();
|
|
1325
|
+
const res = S.createOperationGroup();
|
|
1326
|
+
expect(res).to.be.instanceOf(OAOperationGroup);
|
|
1327
|
+
});
|
|
1328
|
+
|
|
1329
|
+
it('should pass the current instance to the operation group', function () {
|
|
1330
|
+
const S = new OADocumentBuilder();
|
|
1331
|
+
const res = S.createOperationGroup();
|
|
1332
|
+
expect(res.getBuilder()).to.be.eq(S);
|
|
1333
|
+
});
|
|
1334
|
+
|
|
1335
|
+
it('should create the operation group with specified options', function () {
|
|
1336
|
+
const S = new OADocumentBuilder();
|
|
1337
|
+
const res = S.createOperationGroup({
|
|
1338
|
+
pathPrefix: '/prefix',
|
|
1339
|
+
tags: ['foo', 'bar'],
|
|
1340
|
+
});
|
|
1341
|
+
expect(res.getPathPrefix()).to.be.eq('/prefix');
|
|
1342
|
+
expect(res.getTags()).to.be.eql(['foo', 'bar']);
|
|
1343
|
+
});
|
|
1344
|
+
});
|
|
1345
|
+
|
|
1346
|
+
describe('build', function () {
|
|
1347
|
+
it('should return a copy of the document object', function () {
|
|
1348
|
+
const S = new OADocumentBuilder({
|
|
1349
|
+
info: {
|
|
1350
|
+
title: 'My amazing API',
|
|
1351
|
+
description: 'API Documentation',
|
|
1352
|
+
version: '1.0.0',
|
|
1353
|
+
},
|
|
1354
|
+
});
|
|
1355
|
+
const res1 = S.build();
|
|
1356
|
+
const res2 = S.build();
|
|
1357
|
+
expect(res1).to.be.eql({
|
|
1358
|
+
openapi: OPENAPI_VERSION,
|
|
1359
|
+
info: {
|
|
1360
|
+
title: 'My amazing API',
|
|
1361
|
+
description: 'API Documentation',
|
|
1362
|
+
version: '1.0.0',
|
|
1363
|
+
},
|
|
1364
|
+
paths: {},
|
|
1365
|
+
});
|
|
1366
|
+
expect(res1).to.be.eql(res2);
|
|
1367
|
+
expect(res1).to.be.not.eq(res2);
|
|
1368
|
+
});
|
|
1369
|
+
});
|
|
1370
|
+
|
|
1371
|
+
describe('buildJson', function () {
|
|
1372
|
+
it('should return the document object in JSON', function () {
|
|
1373
|
+
const info = {
|
|
1374
|
+
title: 'My amazing API',
|
|
1375
|
+
description: 'API Documentation',
|
|
1376
|
+
version: '1.0.0',
|
|
1377
|
+
};
|
|
1378
|
+
const S = new OADocumentBuilder({info});
|
|
1379
|
+
const res = S.buildJson();
|
|
1380
|
+
expect(res).to.be.eq(
|
|
1381
|
+
JSON.stringify({
|
|
1382
|
+
openapi: OPENAPI_VERSION,
|
|
1383
|
+
info,
|
|
1384
|
+
paths: {},
|
|
1385
|
+
}),
|
|
1386
|
+
);
|
|
1387
|
+
});
|
|
1388
|
+
|
|
1389
|
+
it('should allow specify the parameter "space" for JSON.stringify', function () {
|
|
1390
|
+
const info = {
|
|
1391
|
+
title: 'My amazing API',
|
|
1392
|
+
description: 'API Documentation',
|
|
1393
|
+
version: '1.0.0',
|
|
1394
|
+
};
|
|
1395
|
+
const S = new OADocumentBuilder({info});
|
|
1396
|
+
const space = 2;
|
|
1397
|
+
const res = S.buildJson(space);
|
|
1398
|
+
expect(res).to.be.eq(
|
|
1399
|
+
JSON.stringify(
|
|
1400
|
+
{
|
|
1401
|
+
openapi: OPENAPI_VERSION,
|
|
1402
|
+
info,
|
|
1403
|
+
paths: {},
|
|
1404
|
+
},
|
|
1405
|
+
undefined,
|
|
1406
|
+
space,
|
|
1407
|
+
),
|
|
1408
|
+
);
|
|
1409
|
+
});
|
|
1410
|
+
});
|
|
1411
|
+
});
|