serverless-openapi-documenter 0.0.53 → 0.0.60

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.
@@ -0,0 +1,700 @@
1
+ 'use strict'
2
+
3
+ const fs = require('fs').promises
4
+ const path = require('path')
5
+
6
+ const expect = require('chai').expect
7
+ const nock = require('nock')
8
+
9
+ const modelsDocumentOG = require('../models/models/models.json')
10
+ const modelsAltDocumentOG = require('../models/models/models-alt.json')
11
+ const modelsListDocumentOG = require('../models/models/modelsList.json')
12
+ const modelsListAltDocumentOG = require('../models/models/modelsList-alt.json')
13
+
14
+ const serverlessMock = require('../helpers/serverless')
15
+ const SchemaHandler = require('../../src/schemaHandler')
16
+
17
+
18
+ describe(`SchemaHandler`, function () {
19
+ let mockServerless
20
+ let openAPI
21
+ let modelsDocument, modelsAltDocument, modelsListDocument, modelsListAltDocument
22
+ const v4 = new RegExp(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i)
23
+ const openAPISchema = {
24
+ version: '3.0.3',
25
+ components: {
26
+ schemas: {}
27
+ }
28
+ }
29
+
30
+ beforeEach(function() {
31
+ mockServerless = JSON.parse(JSON.stringify(serverlessMock))
32
+ modelsDocument = JSON.parse(JSON.stringify(modelsDocumentOG))
33
+ modelsAltDocument = JSON.parse(JSON.stringify(modelsAltDocumentOG))
34
+ modelsListDocument = JSON.parse(JSON.stringify(modelsListDocumentOG))
35
+ modelsListAltDocument = JSON.parse(JSON.stringify(modelsListAltDocumentOG))
36
+ openAPI = JSON.parse(JSON.stringify(openAPISchema))
37
+ });
38
+
39
+ describe(`constuctor`, function () {
40
+ it('should return an instance of SchemaHandler', function() {
41
+ const expected = new SchemaHandler(mockServerless, openAPI)
42
+ expect(expected).to.be.an.instanceOf(SchemaHandler)
43
+ });
44
+
45
+ describe(`standardising the models`, function () {
46
+ it(`should standardise models syntax in to the correct format`, function() {
47
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
48
+ const expected = new SchemaHandler(mockServerless, openAPI)
49
+
50
+ expect(expected.models).to.be.an('array')
51
+ expect(expected.models.length).to.be.equal(1)
52
+
53
+ expect(expected.models[0].name).to.equal('ErrorResponse')
54
+ expect(expected.models[0]).to.have.property('contentType')
55
+ expect(expected.models[0]).to.have.property('schema')
56
+ expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
57
+ });
58
+
59
+ it(`should standardise alternative models syntax in to the correct format`, function() {
60
+ Object.assign(mockServerless.service.custom.documentation, modelsAltDocument)
61
+ const expected = new SchemaHandler(mockServerless, openAPI)
62
+
63
+ expect(expected.models).to.be.an('array')
64
+ expect(expected.models.length).to.be.equal(1)
65
+
66
+ expect(expected.models[0].name).to.equal('ErrorResponse')
67
+ expect(expected.models[0]).to.have.property('contentType')
68
+ expect(expected.models[0]).to.have.property('schema')
69
+ expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
70
+ });
71
+
72
+ it(`should standardise modelsList syntax in to the correct format`, function() {
73
+ Object.assign(mockServerless.service.custom.documentation, modelsListDocument)
74
+ const expected = new SchemaHandler(mockServerless, openAPI)
75
+
76
+ expect(expected.models).to.be.an('array')
77
+ expect(expected.models.length).to.be.equal(1)
78
+
79
+ expect(expected.models[0].name).to.equal('ErrorResponse')
80
+ expect(expected.models[0]).to.have.property('contentType')
81
+ expect(expected.models[0]).to.have.property('schema')
82
+ expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
83
+ });
84
+
85
+ it(`should standardise alternative modelsList syntax in to the correct format`, function() {
86
+ Object.assign(mockServerless.service.custom.documentation, modelsListAltDocument)
87
+ const expected = new SchemaHandler(mockServerless, openAPI)
88
+
89
+ expect(expected.models).to.be.an('array')
90
+ expect(expected.models.length).to.be.equal(1)
91
+
92
+ expect(expected.models[0].name).to.equal('ErrorResponse')
93
+ expect(expected.models[0]).to.have.property('contentType')
94
+ expect(expected.models[0]).to.have.property('schema')
95
+ expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
96
+ });
97
+
98
+ it(`should standardise mixed models syntax in to the correct format`, function() {
99
+ const newModelsDocument = JSON.parse(JSON.stringify(modelsDocument))
100
+ Object.assign(mockServerless.service.custom.documentation, newModelsDocument)
101
+ mockServerless.service.custom.documentation.models.push(
102
+ {
103
+ name: 'SuccessResponse',
104
+ description: 'A success response',
105
+ contentType: 'application/json',
106
+ schema: {
107
+ type: 'string'
108
+ }
109
+ }
110
+ )
111
+ const expected = new SchemaHandler(mockServerless, openAPI)
112
+
113
+ expect(expected.models).to.be.an('array')
114
+ expect(expected.models.length).to.be.equal(2)
115
+
116
+ expect(expected.models[0].name).to.equal('ErrorResponse')
117
+ expect(expected.models[0]).to.have.property('contentType')
118
+ expect(expected.models[0]).to.have.property('schema')
119
+ expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
120
+
121
+ expect(expected.models[1].name).to.equal('SuccessResponse')
122
+ expect(expected.models[1]).to.have.property('contentType')
123
+ expect(expected.models[1]).to.have.property('schema')
124
+ expect(expected.models[1].schema).to.be.eql({type: 'string'})
125
+ });
126
+
127
+ it(`should standardise mixed modelsList syntax in to the correct format`, function() {
128
+ const newModelsDocument = JSON.parse(JSON.stringify(modelsListDocument))
129
+ Object.assign(mockServerless.service.custom.documentation, newModelsDocument)
130
+ mockServerless.service.custom.documentation.modelsList.push(
131
+ {
132
+ name: 'SuccessResponse',
133
+ description: 'A success response',
134
+ contentType: 'application/json',
135
+ schema: {
136
+ type: 'string'
137
+ }
138
+ }
139
+ )
140
+ const expected = new SchemaHandler(mockServerless, openAPI)
141
+
142
+ expect(expected.models).to.be.an('array')
143
+ expect(expected.models.length).to.be.equal(2)
144
+
145
+ expect(expected.models[0].name).to.equal('ErrorResponse')
146
+ expect(expected.models[0]).to.have.property('contentType')
147
+ expect(expected.models[0]).to.have.property('schema')
148
+ expect(expected.models[0].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
149
+
150
+ expect(expected.models[1].name).to.equal('SuccessResponse')
151
+ expect(expected.models[1]).to.have.property('contentType')
152
+ expect(expected.models[1]).to.have.property('schema')
153
+ expect(expected.models[1].schema).to.be.eql({type: 'string'})
154
+ });
155
+
156
+ it(`should standardise mixed models and modelsList syntax in to the correct format`, function() {
157
+ const newModelsDocument = JSON.parse(JSON.stringify(modelsListDocument))
158
+ Object.assign(mockServerless.service.custom.documentation, newModelsDocument)
159
+ Object.assign(
160
+ mockServerless.service.custom.documentation,
161
+ {
162
+ models: [
163
+ {
164
+ name: 'SuccessResponse',
165
+ description: 'A success response',
166
+ contentType: 'application/json',
167
+ schema: {
168
+ type: 'string'
169
+ }
170
+ }
171
+ ]
172
+ }
173
+ )
174
+
175
+ const expected = new SchemaHandler(mockServerless, openAPI)
176
+
177
+ expect(expected.models).to.be.an('array')
178
+ expect(expected.models.length).to.be.equal(2)
179
+
180
+ expect(expected.models[0].name).to.equal('SuccessResponse')
181
+ expect(expected.models[0]).to.have.property('contentType')
182
+ expect(expected.models[0]).to.have.property('schema')
183
+ expect(expected.models[0].schema).to.be.eql({type: 'string'})
184
+
185
+ expect(expected.models[1].name).to.equal('ErrorResponse')
186
+ expect(expected.models[1]).to.have.property('contentType')
187
+ expect(expected.models[1]).to.have.property('schema')
188
+ expect(expected.models[1].schema).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
189
+ });
190
+ });
191
+
192
+ it(`should correctly resolve the RefParserOptions`, async function() {
193
+ let expected = new SchemaHandler(mockServerless, openAPI)
194
+ expect(expected.refParserOptions).to.be.an('object')
195
+ expect(expected.refParserOptions).to.be.empty
196
+
197
+ await fs.mkdir(path.resolve('options'))
198
+ .catch(err => {
199
+ console.error(err)
200
+ throw err
201
+ })
202
+
203
+ await fs.copyFile(path.resolve('test/helpers/ref-parser.js'), path.resolve('options/ref-parser.js'))
204
+ .catch(err => {
205
+ console.error(err)
206
+ throw err
207
+ })
208
+
209
+ expected = new SchemaHandler(mockServerless, openAPI)
210
+ expect(expected.refParserOptions).to.be.an('object')
211
+ expect(expected.refParserOptions).to.have.property('continueOnError')
212
+
213
+ await fs.rm(path.resolve('options/ref-parser.js'))
214
+ .catch(err => {
215
+ console.error(err)
216
+ throw err
217
+ })
218
+
219
+ await fs.rmdir(path.resolve('options'))
220
+ .catch(err => {
221
+ console.error(err)
222
+ throw err
223
+ })
224
+ });
225
+ });
226
+
227
+ describe(`addModelsToOpenAPI`, function () {
228
+ describe(`embedded simple schemas`, function () {
229
+ it(`should add the model to the openAPI schema`, async function() {
230
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
231
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
232
+
233
+ await schemaHandler.addModelsToOpenAPI()
234
+
235
+ expect(schemaHandler.openAPI).to.have.property('components')
236
+ expect(schemaHandler.openAPI.components).to.have.property('schemas')
237
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
238
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
239
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
240
+ });
241
+
242
+ it(`should add a model with references to the openAPI schema`, async function() {
243
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
244
+ mockServerless.service.custom.documentation.models.push(
245
+ {
246
+ name: 'SuccessResponse',
247
+ contentType: 'application/json',
248
+ schema: {
249
+ type: 'object',
250
+ properties: {
251
+ name: {
252
+ '$ref': '#/definitions/nameObject'
253
+ }
254
+ },
255
+ definitions: {
256
+ nameObject: {
257
+ type: 'object',
258
+ properties: {
259
+ firstName: {
260
+ type: 'string'
261
+ }
262
+ }
263
+ }
264
+ }
265
+ }
266
+ }
267
+ )
268
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
269
+
270
+ await schemaHandler.addModelsToOpenAPI()
271
+
272
+ expect(schemaHandler.openAPI).to.have.property('components')
273
+ expect(schemaHandler.openAPI.components).to.have.property('schemas')
274
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
275
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
276
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
277
+
278
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('SuccessResponse')
279
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.an('object')
280
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.eql({
281
+ type: 'object',
282
+ properties: {
283
+ name: {
284
+ type: 'object',
285
+ properties: {
286
+ firstName: {
287
+ type: 'string'
288
+ }
289
+ }
290
+ }
291
+ }
292
+ })
293
+ });
294
+
295
+ it(`should add a model with poorly dereferenced references to the openAPI schema`, async function() {
296
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
297
+ mockServerless.service.custom.documentation.models.push(
298
+ {
299
+ name: 'SuccessResponse',
300
+ contentType: 'application/json',
301
+ schema: {
302
+ type: 'object',
303
+ '$ref': '#/definitions/nameObject',
304
+ definitions: {
305
+ nameObject: {
306
+ type: 'object',
307
+ properties: {
308
+ firstName: {
309
+ type: 'string'
310
+ }
311
+ }
312
+ }
313
+ }
314
+ }
315
+ }
316
+ )
317
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
318
+
319
+ await schemaHandler.addModelsToOpenAPI()
320
+
321
+ expect(schemaHandler.openAPI).to.have.property('components')
322
+ expect(schemaHandler.openAPI.components).to.have.property('schemas')
323
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
324
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
325
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
326
+
327
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('SuccessResponse')
328
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.an('object')
329
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.eql({
330
+ type: 'object',
331
+ properties: { firstName: { type: 'string' } }
332
+ })
333
+ });
334
+ });
335
+
336
+ describe(`schemas with references`, function () {
337
+ describe(`file references`, function () {
338
+ it(`should add schemas with file references to the openAPI schema`, async function() {
339
+
340
+ });
341
+ });
342
+
343
+ describe(`component references`, function () {
344
+ it(`should add schemas with component references to the openAPI schema`, async function() {
345
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
346
+ mockServerless.service.custom.documentation.models.push(
347
+ {
348
+ name: 'SuccessResponse',
349
+ contentType: 'application/json',
350
+ schema: {
351
+ type: 'array',
352
+ items: {
353
+ $ref: '#/components/schemas/Agency'
354
+ }
355
+ }
356
+ }
357
+ )
358
+ mockServerless.service.custom.documentation.models.push(
359
+ {
360
+ name: 'Agency',
361
+ contentType: 'application/json',
362
+ schema: {
363
+ type: 'string'
364
+ }
365
+ }
366
+ )
367
+
368
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
369
+
370
+ await schemaHandler.addModelsToOpenAPI()
371
+
372
+ expect(schemaHandler.openAPI).to.have.property('components')
373
+ expect(schemaHandler.openAPI.components).to.have.property('schemas')
374
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
375
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
376
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
377
+
378
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('SuccessResponse')
379
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.an('object')
380
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.eql({
381
+ type: 'array',
382
+ items: {
383
+ $ref: '#/components/schemas/Agency'
384
+ }
385
+ })
386
+
387
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('Agency')
388
+ expect(schemaHandler.openAPI.components.schemas.Agency).to.be.an('object')
389
+ expect(schemaHandler.openAPI.components.schemas.Agency).to.be.eql({
390
+ type: 'string',
391
+ })
392
+ });
393
+ });
394
+
395
+ describe(`other references`, function () {
396
+ it(`should add a model that is a webUrl to the openAPI schema`, async function() {
397
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
398
+ mockServerless.service.custom.documentation.models.push(
399
+ {
400
+ name: 'SuccessResponse',
401
+ contentType: 'application/json',
402
+ schema: 'https://google.com/build/LicensedMember.json'
403
+ }
404
+ )
405
+
406
+ nock('https://google.com')
407
+ .get('/build/LicensedMember.json')
408
+ .reply(200, {
409
+ type: 'object',
410
+ properties: {
411
+ memberId: {
412
+ type: 'string'
413
+ },
414
+ createdAt: {
415
+ type: 'integer'
416
+ }
417
+ }
418
+ })
419
+
420
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
421
+
422
+ await schemaHandler.addModelsToOpenAPI()
423
+
424
+ expect(schemaHandler.openAPI).to.have.property('components')
425
+ expect(schemaHandler.openAPI.components).to.have.property('schemas')
426
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
427
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
428
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
429
+
430
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('SuccessResponse')
431
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.an('object')
432
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.eql({
433
+ type: 'object',
434
+ properties: {
435
+ memberId: {
436
+ type: 'string'
437
+ },
438
+ createdAt: {
439
+ type: 'integer'
440
+ }
441
+ }
442
+ })
443
+ });
444
+
445
+ it(`should add a complex model that is a webUrl to the openAPI schema`, async function() {
446
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
447
+ mockServerless.service.custom.documentation.models.push(
448
+ {
449
+ name: 'SuccessResponse',
450
+ contentType: 'application/json',
451
+ schema: 'https://google.com/build/LicensedMember.json'
452
+ }
453
+ )
454
+
455
+ nock('https://google.com')
456
+ .get('/build/LicensedMember.json')
457
+ .reply(200, {
458
+ "type": "object",
459
+ "properties": {
460
+ "street_address": {
461
+ "type": "string"
462
+ },
463
+ "country": {
464
+ "default": "United States of America",
465
+ "enum": ["United States of America", "Canada"]
466
+ }
467
+ },
468
+ "if": {
469
+ "properties": { "country": { "const": "United States of America" } }
470
+ },
471
+ "then": {
472
+ "properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
473
+ },
474
+ "else": {
475
+ "properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
476
+ }
477
+ })
478
+
479
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
480
+
481
+ await schemaHandler.addModelsToOpenAPI()
482
+
483
+ expect(schemaHandler.openAPI).to.have.property('components')
484
+ expect(schemaHandler.openAPI.components).to.have.property('schemas')
485
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('ErrorResponse')
486
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.an('object')
487
+ expect(schemaHandler.openAPI.components.schemas.ErrorResponse).to.be.eql({type: 'object', properties: {error: {type: 'string'}}})
488
+
489
+ expect(schemaHandler.openAPI.components.schemas).to.have.property('SuccessResponse')
490
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.be.an('object')
491
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse.properties).to.be.eql({
492
+ "street_address": {
493
+ "type": "string"
494
+ },
495
+ "country": {
496
+ "default": "United States of America",
497
+ "enum": ["United States of America", "Canada"]
498
+ }
499
+ })
500
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse).to.have.property('oneOf')
501
+ expect(schemaHandler.openAPI.components.schemas.SuccessResponse.oneOf.length).to.be.equal(2)
502
+ expect(Object.keys(schemaHandler.openAPI.components.schemas).length).to.be.equal(3)
503
+ });
504
+
505
+ it(`should throw when a webUrl returns a 404`, async function() {
506
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
507
+ mockServerless.service.custom.documentation.models.push(
508
+ {
509
+ name: 'SuccessResponse',
510
+ contentType: 'application/json',
511
+ schema: 'https://google.com/build/LicensedMember.json'
512
+ }
513
+ )
514
+
515
+ nock('https://google.com')
516
+ .get('/build/LicensedMember.json')
517
+ .reply(404, {body: 'Bad Request'})
518
+
519
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
520
+
521
+ await schemaHandler.addModelsToOpenAPI()
522
+ .catch(err => {
523
+ expect(err).to.not.be.undefined
524
+ })
525
+ });
526
+ });
527
+ });
528
+ });
529
+
530
+ describe(`createSchema`, function () {
531
+ it(`returns a reference to the schema when the schema already exists in components and we don't pass through a schema`, async function() {
532
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
533
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
534
+
535
+ await schemaHandler.addModelsToOpenAPI()
536
+
537
+ const expected = await schemaHandler.createSchema('ErrorResponse')
538
+
539
+ expect(expected).to.be.equal('#/components/schemas/ErrorResponse')
540
+ });
541
+
542
+ it(`throws an error when the name of the schema does not exist in components and we don't pass through a schema`, async function() {
543
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
544
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
545
+
546
+ await schemaHandler.addModelsToOpenAPI()
547
+
548
+ const expected = await schemaHandler.createSchema('PUTRequest')
549
+ .catch(err => {
550
+ expect(err).to.not.be.undefined
551
+ expect(err.message).to.be.equal('Expected a file path, URL, or object. Got undefined')
552
+ })
553
+
554
+ expect(expected).to.be.undefined
555
+ });
556
+
557
+ it(`returns a reference to a schema when the schema does not exist in components already`, async function() {
558
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
559
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
560
+
561
+ await schemaHandler.addModelsToOpenAPI()
562
+ const schema = {
563
+ type: 'object',
564
+ properties: {
565
+ createdAt: {
566
+ type: 'number'
567
+ }
568
+ }
569
+ }
570
+ const expected = await schemaHandler.createSchema('PUTRequest', schema)
571
+ .catch(err => {
572
+ expect(err).to.be.undefined
573
+ })
574
+
575
+ expect(expected).to.be.equal('#/components/schemas/PUTRequest')
576
+ expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(schema)
577
+ });
578
+
579
+ it(`returns a reference to a schema when the schema exists in components already and the same schema is being passed through`, async function() {
580
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
581
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
582
+
583
+ await schemaHandler.addModelsToOpenAPI()
584
+ const schema = {
585
+ type: 'object',
586
+ properties: {
587
+ createdAt: {
588
+ type: 'number'
589
+ }
590
+ }
591
+ }
592
+ let expected = await schemaHandler.createSchema('PUTRequest', schema)
593
+ .catch(err => {
594
+ expect(err).to.be.undefined
595
+ })
596
+
597
+ expect(expected).to.be.equal('#/components/schemas/PUTRequest')
598
+ expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(schema)
599
+
600
+ expected = await schemaHandler.createSchema('PUTRequest', schema)
601
+ .catch(err => {
602
+ expect(err).to.be.undefined
603
+ })
604
+
605
+ expect(expected).to.be.equal('#/components/schemas/PUTRequest')
606
+ expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(schema)
607
+ });
608
+
609
+ it(`returns a reference to a new schema when the schema exists in components already and a different schema is being passed through`, async function() {
610
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
611
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
612
+
613
+ await schemaHandler.addModelsToOpenAPI()
614
+ const schema = {
615
+ type: 'object',
616
+ properties: {
617
+ createdAt: {
618
+ type: 'number'
619
+ }
620
+ }
621
+ }
622
+ let expected = await schemaHandler.createSchema('PUTRequest', schema)
623
+ .catch(err => {
624
+ expect(err).to.be.undefined
625
+ })
626
+
627
+ expect(expected).to.be.equal('#/components/schemas/PUTRequest')
628
+ expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(schema)
629
+
630
+ const differentSchema = {
631
+ type: 'object',
632
+ properties: {
633
+ updatedAt: {
634
+ type: 'number'
635
+ }
636
+ }
637
+ }
638
+
639
+ expected = await schemaHandler.createSchema('PUTRequest', differentSchema)
640
+ .catch(err => {
641
+ expect(err).to.be.undefined
642
+ })
643
+
644
+ const splitPath = expected.split('/')
645
+ expect(v4.test(splitPath[3].split('PUTRequest-')[1])).to.be.true
646
+ expect(expected).to.be.equal(`#/components/schemas/${splitPath[3]}`)
647
+ expect(schemaHandler.openAPI.components.schemas[splitPath[3]]).to.be.eql(differentSchema)
648
+ });
649
+
650
+ it(`returns a reference to a new schema when the schema passed through is a URL`, async function() {
651
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
652
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
653
+
654
+ await schemaHandler.addModelsToOpenAPI()
655
+ const schema = 'https://google.com/build/LicensedMember.json'
656
+ const schemaObj = {
657
+ type: 'object',
658
+ properties: {
659
+ name: {
660
+ type: 'string'
661
+ },
662
+ address: {
663
+ type: 'string'
664
+ }
665
+ }
666
+ }
667
+
668
+ nock('https://google.com')
669
+ .get('/build/LicensedMember.json')
670
+ .reply(200, schemaObj)
671
+
672
+ let expected = await schemaHandler.createSchema('PUTRequest', schema)
673
+ .catch(err => {
674
+ expect(err).to.be.undefined
675
+ })
676
+
677
+ expect(expected).to.be.equal('#/components/schemas/PUTRequest')
678
+ expect(schemaHandler.openAPI.components.schemas.PUTRequest).to.be.eql(schemaObj)
679
+ });
680
+
681
+ it(`should throw an error when a schema as a URL can not be resolved correctly`, async function() {
682
+ Object.assign(mockServerless.service.custom.documentation, modelsDocument)
683
+ const schemaHandler = new SchemaHandler(mockServerless, openAPI)
684
+
685
+ await schemaHandler.addModelsToOpenAPI()
686
+ const schema = 'https://google.com/build/LicensedMember.json'
687
+
688
+ nock('https://google.com')
689
+ .get('/build/LicensedMember.json')
690
+ .reply(404)
691
+
692
+ let expected = await schemaHandler.createSchema('PUTRequest', schema)
693
+ .catch(err => {
694
+ expect(err).to.not.undefined
695
+ })
696
+
697
+ expect(expected).to.be.undefined
698
+ });
699
+ });
700
+ });