-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathindex.js
1666 lines (1539 loc) · 67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @ts-check
'use strict';
const fs = require('fs');
const url = require('url');
const pathlib = require('path');
const maybe = require('call-me-maybe');
const fetch = require('node-fetch-h2');
const yaml = require('yaml');
const jptr = require('reftools/lib/jptr.js');
const resolveInternal = jptr.jptr;
const isRef = require('reftools/lib/isref.js').isRef;
const clone = require('reftools/lib/clone.js').clone;
const cclone = require('reftools/lib/clone.js').circularClone;
const recurse = require('reftools/lib/recurse.js').recurse;
const resolver = require('oas-resolver');
const sw = require('oas-schema-walker');
const common = require('oas-kit-common');
const statusCodes = require('./lib/statusCodes.js').statusCodes;
const ourVersion = require('./package.json').version;
// TODO handle specification-extensions with plugins?
const targetVersion = '3.0.0';
let componentNames; // initialised in main
class S2OError extends Error {
constructor(message) {
super(message);
this.name = 'S2OError';
}
}
function throwError(message, options) {
let err = new S2OError(message);
err.options = options;
if (options.promise) {
options.promise.reject(err);
}
else {
throw err;
}
}
function throwOrWarn(message, container, options) {
if (options.warnOnly) {
container[options.warnProperty||'x-s2o-warning'] = message;
}
else {
throwError(message, options);
}
}
function fixUpSubSchema(schema,parent,options) {
if (schema.nullable) options.patches++;
if (schema.discriminator && typeof schema.discriminator === 'string') {
schema.discriminator = { propertyName: schema.discriminator };
}
if (schema.items && Array.isArray(schema.items)) {
if (schema.items.length === 0) {
schema.items = {};
}
else if (schema.items.length === 1) {
schema.items = schema.items[0];
}
else schema.items = { anyOf: schema.items };
}
if (schema.type && Array.isArray(schema.type)) {
if (options.patch) {
options.patches++;
if (schema.type.length === 0) {
delete schema.type;
}
else {
if (!schema.oneOf) schema.oneOf = [];
for (let type of schema.type) {
let newSchema = {};
if (type === 'null') {
schema.nullable = true;
}
else {
newSchema.type = type;
for (let prop of common.arrayProperties) {
if (typeof schema.prop !== 'undefined') {
newSchema[prop] = schema[prop];
delete schema[prop];
}
}
}
if (newSchema.type) {
schema.oneOf.push(newSchema);
}
}
delete schema.type;
if (schema.oneOf.length === 0) {
delete schema.oneOf; // means was just null => nullable
}
else if (schema.oneOf.length < 2) {
schema.type = schema.oneOf[0].type;
if (Object.keys(schema.oneOf[0]).length > 1) {
throwOrWarn('Lost properties from oneOf',schema,options);
}
delete schema.oneOf;
}
}
// do not else this
if (schema.type && Array.isArray(schema.type) && schema.type.length === 1) {
schema.type = schema.type[0];
}
}
else {
throwError('(Patchable) schema type must not be an array', options);
}
}
if (schema.type && schema.type === 'null') {
delete schema.type;
schema.nullable = true;
}
if ((schema.type === 'array') && (!schema.items)) {
schema.items = {};
}
if (schema.type === 'file') {
schema.type = 'string';
schema.format = 'binary';
}
if (typeof schema.required === 'boolean') {
if (schema.required && schema.name) {
if (typeof parent.required === 'undefined') {
parent.required = [];
}
if (Array.isArray(parent.required)) parent.required.push(schema.name);
}
delete schema.required;
}
// TODO if we have a nested properties (object inside an object) and the
// *parent* type is not set, force it to object
// TODO if default is set but type is not set, force type to typeof default
if (schema.xml && typeof schema.xml.namespace === 'string') {
if (!schema.xml.namespace) delete schema.xml.namespace;
}
if (typeof schema.allowEmptyValue !== 'undefined') {
options.patches++;
delete schema.allowEmptyValue;
}
}
function fixUpSubSchemaExtensions(schema,parent) {
if (schema["x-required"] && Array.isArray(schema["x-required"])) {
if (!schema.required) schema.required = [];
schema.required = schema.required.concat(schema["x-required"]);
delete schema["x-required"];
}
if (schema["x-anyOf"]) {
schema.anyOf = schema["x-anyOf"];
delete schema["x-anyOf"];
}
if (schema["x-oneOf"]) {
schema.oneOf = schema["x-oneOf"];
delete schema["x-oneOf"];
}
if (schema["x-not"]) {
schema.not = schema["x-not"];
delete schema["x-not"];
}
if (typeof schema["x-nullable"] === 'boolean') {
schema.nullable = schema["x-nullable"];
delete schema["x-nullable"];
}
if ((typeof schema["x-discriminator"] === 'object') && (typeof schema["x-discriminator"].propertyName === 'string')) {
schema.discriminator = schema["x-discriminator"];
delete schema["x-discriminator"];
for (let entry in schema.discriminator.mapping) {
let schemaOrRef = schema.discriminator.mapping[entry];
if (schemaOrRef.startsWith('#/definitions/')) {
schema.discriminator.mapping[entry] = schemaOrRef.replace('#/definitions/','#/components/schemas/');
}
}
}
}
function fixUpSchema(schema,options) {
sw.walkSchema(schema,{},{},function(schema,parent,state){
fixUpSubSchemaExtensions(schema,parent);
fixUpSubSchema(schema,parent,options);
});
}
function getMiroComponentName(ref) {
if (ref.indexOf('#')>=0) {
ref = ref.split('#')[1].split('/').pop();
}
else {
ref = ref.split('/').pop().split('.')[0];
}
return encodeURIComponent(common.sanitise(ref));
}
function fixupRefs(obj, key, state) {
let options = state.payload.options;
if (isRef(obj,key)) {
if (obj[key].startsWith('#/components/')) {
// no-op
}
else if (obj[key] === '#/consumes') {
// people are *so* creative
delete obj[key];
state.parent[state.pkey] = clone(options.openapi.consumes);
}
else if (obj[key] === '#/produces') {
// and by creative, I mean devious
delete obj[key];
state.parent[state.pkey] = clone(options.openapi.produces);
}
else if (obj[key].startsWith('#/definitions/')) {
//only the first part of a schema component name must be sanitised
let keys = obj[key].replace('#/definitions/', '').split('/');
const ref = jptr.jpunescape(keys[0]);
let newKey = componentNames.schemas[decodeURIComponent(ref)]; // lookup, resolves a $ref
if (newKey) {
keys[0] = newKey;
}
else {
throwOrWarn('Could not resolve reference '+obj[key],obj,options);
}
obj[key] = '#/components/schemas/' + keys.join('/');
}
else if (obj[key].startsWith('#/parameters/')) {
// for extensions like Apigee's x-templates
obj[key] = '#/components/parameters/' + common.sanitise(obj[key].replace('#/parameters/', ''));
}
else if (obj[key].startsWith('#/responses/')) {
// for extensions like Apigee's x-templates
obj[key] = '#/components/responses/' + common.sanitise(obj[key].replace('#/responses/', ''));
}
else if (obj[key].startsWith('#')) {
// fixes up direct $refs or those created by resolvers
let target = clone(jptr.jptr(options.openapi,obj[key]));
if (target === false) throwOrWarn('direct $ref not found '+obj[key],obj,options)
else if (options.refmap[obj[key]]) {
obj[key] = options.refmap[obj[key]];
}
else {
// we use a heuristic to determine what kind of thing is being referenced
let oldRef = obj[key];
oldRef = oldRef.replace('/properties/headers/','');
oldRef = oldRef.replace('/properties/responses/','');
oldRef = oldRef.replace('/properties/parameters/','');
oldRef = oldRef.replace('/properties/schemas/','');
let type = 'schemas';
let schemaIndex = oldRef.lastIndexOf('/schema');
type = (oldRef.indexOf('/headers/')>schemaIndex) ? 'headers' :
((oldRef.indexOf('/responses/')>schemaIndex) ? 'responses' :
((oldRef.indexOf('/example')>schemaIndex) ? 'examples' :
((oldRef.indexOf('/x-')>schemaIndex) ? 'extensions' :
((oldRef.indexOf('/parameters/')>schemaIndex) ? 'parameters' : 'schemas'))));
// non-body/form parameters have not moved in the overall structure (like responses)
// but extracting the requestBodies can cause the *number* of parameters to change
if (type === 'schemas') {
fixUpSchema(target,options);
}
if ((type !== 'responses') && (type !== 'extensions')) {
let prefix = type.substr(0,type.length-1);
if ((prefix === 'parameter') && target.name && (target.name === common.sanitise(target.name))) {
prefix = encodeURIComponent(target.name);
}
let suffix = 1;
if (obj['x-miro']) {
prefix = getMiroComponentName(obj['x-miro']);
suffix = '';
}
while (jptr.jptr(options.openapi,'#/components/'+type+'/'+prefix+suffix)) {
suffix = (suffix === '' ? 2 : ++suffix);
}
let newRef = '#/components/'+type+'/'+prefix+suffix;
let refSuffix = '';
if (type === 'examples') {
target = { value: target };
refSuffix = '/value';
}
jptr.jptr(options.openapi,newRef,target);
options.refmap[obj[key]] = newRef+refSuffix;
obj[key] = newRef+refSuffix;
}
}
}
delete obj['x-miro'];
// do this last - rework cases where $ref object has sibling properties
if (Object.keys(obj).length > 1) {
const tmpRef = obj[key];
const inSchema = state.path.indexOf('/schema') >= 0; // not perfect, but in the absence of a reasonably-sized and complete OAS 2.0 parser...
if (options.refSiblings === 'preserve') {
// no-op
}
else if (inSchema && (options.refSiblings === 'allOf')) {
delete obj.$ref;
state.parent[state.pkey] = { allOf: [ { $ref: tmpRef }, obj ]};
}
else { // remove, or not 'preserve' and not in a schema
state.parent[state.pkey] = { $ref: tmpRef };
}
}
}
if ((key === 'x-ms-odata') && (typeof obj[key] === 'string') && (obj[key].startsWith('#/'))) {
let keys = obj[key].replace('#/definitions/', '').replace('#/components/schemas/','').split('/');
let newKey = componentNames.schemas[decodeURIComponent(keys[0])]; // lookup, resolves a $ref
if (newKey) {
keys[0] = newKey;
}
else {
throwOrWarn('Could not resolve reference '+obj[key],obj,options);
}
obj[key] = '#/components/schemas/' + keys.join('/');
}
}
/*
* This has to happen as a separate pass because multiple $refs may point
* through elements of the same path
*/
function dedupeRefs(openapi, options) {
for (let ref in options.refmap) {
jptr.jptr(openapi,ref,{ $ref: options.refmap[ref] });
}
}
function processSecurity(securityObject) {
for (let s in securityObject) {
for (let k in securityObject[s]) {
let sname = common.sanitise(k);
if (k !== sname) {
securityObject[s][sname] = securityObject[s][k];
delete securityObject[s][k];
}
}
}
}
function processSecurityScheme(scheme, options) {
if (scheme.type === 'basic') {
scheme.type = 'http';
scheme.scheme = 'basic';
}
if (scheme.type === 'oauth2') {
let flow = {};
let flowName = scheme.flow;
if (scheme.flow === 'application') flowName = 'clientCredentials';
if (scheme.flow === 'accessCode') flowName = 'authorizationCode';
if (typeof scheme.authorizationUrl !== 'undefined') flow.authorizationUrl = scheme.authorizationUrl.split('?')[0].trim() || '/';
if (typeof scheme.tokenUrl === 'string') flow.tokenUrl = scheme.tokenUrl.split('?')[0].trim() || '/';
flow.scopes = scheme.scopes || {};
scheme.flows = {};
scheme.flows[flowName] = flow;
delete scheme.flow;
delete scheme.authorizationUrl;
delete scheme.tokenUrl;
delete scheme.scopes;
if (typeof scheme.name !== 'undefined') {
if (options.patch) {
options.patches++;
delete scheme.name;
}
else {
throwError('(Patchable) oauth2 securitySchemes should not have name property', options);
}
}
}
}
function keepParameters(value) {
return (value && !value["x-s2o-delete"]);
}
function processHeader(header, options) {
if (header.$ref) {
header.$ref = header.$ref.replace('#/responses/', '#/components/responses/');
}
else {
if (header.type && !header.schema) {
header.schema = {};
}
if (header.type) header.schema.type = header.type;
if (header.items && header.items.type !== 'array') {
if (header.items.collectionFormat !== header.collectionFormat) {
throwOrWarn('Nested collectionFormats are not supported', header, options);
}
delete header.items.collectionFormat;
}
if (header.type === 'array') {
if (header.collectionFormat === 'ssv') {
throwOrWarn('collectionFormat:ssv is no longer supported for headers', header, options); // not lossless
}
else if (header.collectionFormat === 'pipes') {
throwOrWarn('collectionFormat:pipes is no longer supported for headers', header, options); // not lossless
}
else if (header.collectionFormat === 'multi') {
header.explode = true;
}
else if (header.collectionFormat === 'tsv') {
throwOrWarn('collectionFormat:tsv is no longer supported', header, options); // not lossless
header["x-collectionFormat"] = 'tsv';
}
else { // 'csv'
header.style = 'simple';
}
delete header.collectionFormat;
}
else if (header.collectionFormat) {
if (options.patch) {
options.patches++;
delete header.collectionFormat;
}
else {
throwError('(Patchable) collectionFormat is only applicable to header.type array', options);
}
}
delete header.type;
for (let prop of common.parameterTypeProperties) {
if (typeof header[prop] !== 'undefined') {
header.schema[prop] = header[prop];
delete header[prop];
}
}
for (let prop of common.arrayProperties) {
if (typeof header[prop] !== 'undefined') {
header.schema[prop] = header[prop];
delete header[prop];
}
}
}
}
function fixParamRef(param, options) {
if (param.$ref.indexOf('#/parameters/') >= 0) {
let refComponents = param.$ref.split('#/parameters/');
param.$ref = refComponents[0] + '#/components/parameters/' + common.sanitise(refComponents[1]);
}
if (param.$ref.indexOf('#/definitions/') >= 0) {
throwOrWarn('Definition used as parameter', param, options);
}
}
function attachRequestBody(op,options) {
let newOp = {};
for (let key of Object.keys(op)) {
newOp[key] = op[key];
if (key === 'parameters') {
newOp.requestBody = {};
if (options.rbname) newOp[options.rbname] = '';
}
}
newOp.requestBody = {}; // just in case there are no parameters
return newOp;
}
/**
* @returns op, as it may have changed
*/
function processParameter(param, op, path, method, index, openapi, options) {
let result = {};
let singularRequestBody = true;
let originalType;
if (op && op.consumes && (typeof op.consumes === 'string')) {
if (options.patch) {
options.patches++;
op.consumes = [op.consumes];
}
else {
return throwError('(Patchable) operation.consumes must be an array', options);
}
}
if (!Array.isArray(openapi.consumes)) delete openapi.consumes;
let consumes = ((op ? op.consumes : null) || (openapi.consumes || [])).filter(common.uniqueOnly);
if (param && param.$ref && (typeof param.$ref === 'string')) {
// if we still have a ref here, it must be an internal one
fixParamRef(param, options);
let ptr = decodeURIComponent(param.$ref.replace('#/components/parameters/', ''));
let rbody = false;
let target = openapi.components.parameters[ptr]; // resolves a $ref, must have been sanitised already
if (((!target) || (target["x-s2o-delete"])) && param.$ref.startsWith('#/')) {
// if it's gone, chances are it's a requestBody component now unless spec was broken
param["x-s2o-delete"] = true;
rbody = true;
}
// shared formData parameters from swagger or path level could be used in any combination.
// we dereference all op.requestBody's then hash them and pull out common ones later
if (rbody) {
let ref = param.$ref;
let newParam = resolveInternal(openapi, param.$ref);
if (!newParam && ref.startsWith('#/')) {
throwOrWarn('Could not resolve reference ' + ref, param, options);
}
else {
if (newParam) param = newParam; // preserve reference
}
}
}
if (param && (param.name || param.in)) { // if it's a real parameter OR we've dereferenced it
if (typeof param['x-deprecated'] === 'boolean') {
param.deprecated = param['x-deprecated'];
delete param['x-deprecated'];
}
if (typeof param['x-example'] !== 'undefined') {
param.example = param['x-example'];
delete param['x-example'];
}
if ((param.in !== 'body') && (!param.type)) {
if (options.patch) {
options.patches++;
param.type = 'string';
}
else {
throwError('(Patchable) parameter.type is mandatory for non-body parameters', options);
}
}
if (param.type && typeof param.type === 'object' && param.type.$ref) {
// $ref anywhere sensibility
param.type = resolveInternal(openapi, param.type.$ref);
}
if (param.type === 'file') {
param['x-s2o-originalType'] = param.type;
originalType = param.type;
}
if (param.description && typeof param.description === 'object' && param.description.$ref) {
// $ref anywhere sensibility
param.description = resolveInternal(openapi, param.description.$ref);
}
if (param.description === null) delete param.description;
let oldCollectionFormat = param.collectionFormat;
if ((param.type === 'array') && !oldCollectionFormat) {
oldCollectionFormat = 'csv';
}
if (oldCollectionFormat) {
if (param.type !== 'array') {
if (options.patch) {
options.patches++;
delete param.collectionFormat;
}
else {
throwError('(Patchable) collectionFormat is only applicable to param.type array', options);
}
}
if ((oldCollectionFormat === 'csv') && ((param.in === 'query') || (param.in === 'cookie'))) {
param.style = 'form';
param.explode = false;
}
if ((oldCollectionFormat === 'csv') && ((param.in === 'path') || (param.in === 'header'))) {
param.style = 'simple';
}
if (oldCollectionFormat === 'ssv') {
if (param.in === 'query') {
param.style = 'spaceDelimited';
}
else {
throwOrWarn('collectionFormat:ssv is no longer supported except for in:query parameters', param, options); // not lossless
}
}
if (oldCollectionFormat === 'pipes') {
if (param.in === 'query') {
param.style = 'pipeDelimited';
}
else {
throwOrWarn('collectionFormat:pipes is no longer supported except for in:query parameters', param, options); // not lossless
}
}
if (oldCollectionFormat === 'multi') {
param.explode = true;
}
if (oldCollectionFormat === 'tsv') {
throwOrWarn('collectionFormat:tsv is no longer supported', param, options); // not lossless
param["x-collectionFormat"] = 'tsv';
}
delete param.collectionFormat;
}
if (param.type && (param.type !== 'body') && (param.in !== 'formData')) {
if (param.items && param.schema) {
throwOrWarn('parameter has array,items and schema', param, options);
}
else {
if (param.schema) options.patches++; // already present
if ((!param.schema) || (typeof param.schema !== 'object')) param.schema = {};
param.schema.type = param.type;
if (param.items) {
param.schema.items = param.items;
delete param.items;
recurse(param.schema.items, null, function (obj, key, state) {
if ((key === 'collectionFormat') && (typeof obj[key] === 'string')) {
if (oldCollectionFormat && obj[key] !== oldCollectionFormat) {
throwOrWarn('Nested collectionFormats are not supported', param, options);
}
delete obj[key]; // not lossless
}
// items in 2.0 was a subset of the JSON-Schema items
// object, it gets fixed up below
});
}
for (let prop of common.parameterTypeProperties) {
if (typeof param[prop] !== 'undefined') param.schema[prop] = param[prop];
delete param[prop];
}
}
}
if (param.schema) {
fixUpSchema(param.schema,options);
}
if (param["x-ms-skip-url-encoding"]) {
if (param.in === 'query') { // might be in:path, not allowed in OAS3
param.allowReserved = true;
delete param["x-ms-skip-url-encoding"];
}
}
}
if (param && param.in === 'formData') {
// convert to requestBody component
singularRequestBody = false;
result.content = {};
let contentType = 'application/x-www-form-urlencoded';
if ((consumes.length) && (consumes.indexOf('multipart/form-data') >= 0)) {
contentType = 'multipart/form-data';
}
result.content[contentType] = {};
if (param.schema) {
result.content[contentType].schema = param.schema;
if (param.schema.$ref) {
result['x-s2o-name'] = decodeURIComponent(param.schema.$ref.replace('#/components/schemas/', ''));
}
}
else {
result.content[contentType].schema = {};
result.content[contentType].schema.type = 'object';
result.content[contentType].schema.properties = {};
result.content[contentType].schema.properties[param.name] = {};
let schema = result.content[contentType].schema;
let target = result.content[contentType].schema.properties[param.name];
if (param.description) target.description = param.description;
if (param.example) target.example = param.example;
if (param.type) target.type = param.type;
for (let prop of common.parameterTypeProperties) {
if (typeof param[prop] !== 'undefined') target[prop] = param[prop];
}
if (param.required === true) {
if (!schema.required) schema.required = [];
schema.required.push(param.name);
result.required = true;
}
if (typeof param.default !== 'undefined') target.default = param.default;
if (target.properties) target.properties = param.properties;
if (param.allOf) target.allOf = param.allOf; // new are anyOf, oneOf, not
if ((param.type === 'array') && (param.items)) {
target.items = param.items;
if (target.items.collectionFormat) delete target.items.collectionFormat;
}
if ((originalType === 'file') || (param['x-s2o-originalType'] === 'file')) {
target.type = 'string';
target.format = 'binary';
}
// Copy any extensions on the form param to the target schema property.
copyExtensions(param, target);
}
}
else if (param && (param.type === 'file')) {
// convert to requestBody
if (param.required) result.required = param.required;
result.content = {};
result.content["application/octet-stream"] = {};
result.content["application/octet-stream"].schema = {};
result.content["application/octet-stream"].schema.type = 'string';
result.content["application/octet-stream"].schema.format = 'binary';
copyExtensions(param, result);
}
if (param && param.in === 'body') {
result.content = {};
if (param.name) result['x-s2o-name'] = (op && op.operationId ? common.sanitiseAll(op.operationId) : '') + ('_' + param.name).toCamelCase();
if (param.description) result.description = param.description;
if (param.required) result.required = param.required;
// Set the "request body name" extension on the operation if requested.
if (op && options.rbname && param.name) {
op[options.rbname] = param.name;
}
if (param.schema && param.schema.$ref) {
result['x-s2o-name'] = decodeURIComponent(param.schema.$ref.replace('#/components/schemas/', ''));
}
else if (param.schema && (param.schema.type === 'array') && param.schema.items && param.schema.items.$ref) {
result['x-s2o-name'] = decodeURIComponent(param.schema.items.$ref.replace('#/components/schemas/', '')) + 'Array';
}
if (!consumes.length) {
consumes.push('application/json'); // TODO verify default
}
for (let mimetype of consumes) {
result.content[mimetype] = {};
result.content[mimetype].schema = clone(param.schema || {});
fixUpSchema(result.content[mimetype].schema,options);
}
// Copy any extensions from the original parameter to the new requestBody
copyExtensions(param, result);
}
if (Object.keys(result).length > 0) {
param["x-s2o-delete"] = true;
// work out where to attach the requestBody
if (op) {
if (op.requestBody && singularRequestBody) {
op.requestBody["x-s2o-overloaded"] = true;
let opId = op.operationId || index;
throwOrWarn('Operation ' + opId + ' has multiple requestBodies', op, options);
}
else {
if (!op.requestBody) {
op = path[method] = attachRequestBody(op,options); // make sure we have one
}
if ((op.requestBody.content && op.requestBody.content["multipart/form-data"])
&& (op.requestBody.content["multipart/form-data"].schema) && (op.requestBody.content["multipart/form-data"].schema.properties) && (result.content["multipart/form-data"]) && (result.content["multipart/form-data"].schema) && (result.content["multipart/form-data"].schema.properties)) {
op.requestBody.content["multipart/form-data"].schema.properties =
Object.assign(op.requestBody.content["multipart/form-data"].schema.properties, result.content["multipart/form-data"].schema.properties);
op.requestBody.content["multipart/form-data"].schema.required = (op.requestBody.content["multipart/form-data"].schema.required || []).concat(result.content["multipart/form-data"].schema.required||[]);
if (!op.requestBody.content["multipart/form-data"].schema.required.length) {
delete op.requestBody.content["multipart/form-data"].schema.required;
}
}
else if ((op.requestBody.content && op.requestBody.content["application/x-www-form-urlencoded"] && op.requestBody.content["application/x-www-form-urlencoded"].schema && op.requestBody.content["application/x-www-form-urlencoded"].schema.properties)
&& result.content["application/x-www-form-urlencoded"] && result.content["application/x-www-form-urlencoded"].schema && result.content["application/x-www-form-urlencoded"].schema.properties) {
op.requestBody.content["application/x-www-form-urlencoded"].schema.properties =
Object.assign(op.requestBody.content["application/x-www-form-urlencoded"].schema.properties, result.content["application/x-www-form-urlencoded"].schema.properties);
op.requestBody.content["application/x-www-form-urlencoded"].schema.required = (op.requestBody.content["application/x-www-form-urlencoded"].schema.required || []).concat(result.content["application/x-www-form-urlencoded"].schema.required||[]);
if (!op.requestBody.content["application/x-www-form-urlencoded"].schema.required.length) {
delete op.requestBody.content["application/x-www-form-urlencoded"].schema.required;
}
}
else {
op.requestBody = Object.assign(op.requestBody, result);
if (!op.requestBody['x-s2o-name']) {
if (op.requestBody.schema && op.requestBody.schema.$ref) {
op.requestBody['x-s2o-name'] = decodeURIComponent(op.requestBody.schema.$ref.replace('#/components/schemas/', '')).split('/').join('');
}
else if (op.operationId) {
op.requestBody['x-s2o-name'] = common.sanitiseAll(op.operationId);
}
}
}
}
}
}
// tidy up
if (param && !param['x-s2o-delete']) {
delete param.type;
for (let prop of common.parameterTypeProperties) {
delete param[prop];
}
if ((param.in === 'path') && ((typeof param.required === 'undefined') || (param.required !== true))) {
if (options.patch) {
options.patches++;
param.required = true;
}
else {
throwError('(Patchable) path parameters must be required:true ['+param.name+' in '+index+']', options);
}
}
}
return op;
}
function copyExtensions(src, tgt) {
for (let prop in src) {
if (prop.startsWith('x-') && !prop.startsWith('x-s2o')) {
tgt[prop] = src[prop];
}
}
}
function processResponse(response, name, op, openapi, options) {
if (!response) return false;
if (response.$ref && (typeof response.$ref === 'string')) {
if (response.$ref.indexOf('#/definitions/') >= 0) {
//response.$ref = '#/components/schemas/'+common.sanitise(response.$ref.replace('#/definitions/',''));
throwOrWarn('definition used as response: ' + response.$ref, response, options);
}
else {
if (response.$ref.startsWith('#/responses/')) {
response.$ref = '#/components/responses/' + common.sanitise(decodeURIComponent(response.$ref.replace('#/responses/', '')));
}
}
}
else {
if ((typeof response.description === 'undefined') || (response.description === null)
|| ((response.description === '') && options.patch)) {
if (options.patch) {
if ((typeof response === 'object') && (!Array.isArray(response))) {
options.patches++;
response.description = (statusCodes[response] || '');
}
}
else {
throwError('(Patchable) response.description is mandatory', options);
}
}
if (typeof response.schema !== 'undefined') {
fixUpSchema(response.schema,options);
if (response.schema.$ref && (typeof response.schema.$ref === 'string') && response.schema.$ref.startsWith('#/responses/')) {
response.schema.$ref = '#/components/responses/' + common.sanitise(decodeURIComponent(response.schema.$ref.replace('#/responses/', '')));
}
if (op && op.produces && (typeof op.produces === 'string')) {
if (options.patch) {
options.patches++;
op.produces = [op.produces];
}
else {
return throwError('(Patchable) operation.produces must be an array', options);
}
}
if (openapi.produces && !Array.isArray(openapi.produces)) delete openapi.produces;
let produces = ((op ? op.produces : null) || (openapi.produces || [])).filter(common.uniqueOnly);
if (!produces.length) produces.push('*/*'); // TODO verify default
response.content = {};
for (let mimetype of produces) {
response.content[mimetype] = {};
response.content[mimetype].schema = clone(response.schema);
if (response.examples && response.examples[mimetype]) {
let example = {};
example.value = response.examples[mimetype];
response.content[mimetype].examples = {};
response.content[mimetype].examples.response = example;
delete response.examples[mimetype];
}
if (response.content[mimetype].schema.type === 'file') {
response.content[mimetype].schema = { type: 'string', format: 'binary' };
}
}
delete response.schema;
}
// examples for content-types not listed in produces
for (let mimetype in response.examples) {
if (!response.content) response.content = {};
if (!response.content[mimetype]) response.content[mimetype] = {};
response.content[mimetype].examples = {};
response.content[mimetype].examples.response = {};
response.content[mimetype].examples.response.value = response.examples[mimetype];
}
delete response.examples;
if (response.headers) {
for (let h in response.headers) {
if (h.toLowerCase() === 'status code') {
if (options.patch) {
options.patches++;
delete response.headers[h];
}
else {
throwError('(Patchable) "Status Code" is not a valid header', options);
}
}
else {
processHeader(response.headers[h], options);
}
}
}
}
}
function processPaths(container, containerName, options, requestBodyCache, openapi) {
for (let p in container) {
let path = container[p];
// path.$ref is external only
if (path && (path['x-trace']) && (typeof path['x-trace'] === 'object')) {
path.trace = path['x-trace'];
delete path['x-trace'];
}
if (path && (path['x-summary']) && (typeof path['x-summary'] === 'string')) {
path.summary = path['x-summary'];
delete path['x-summary'];
}
if (path && (path['x-description']) && (typeof path['x-description'] === 'string')) {
path.description = path['x-description'];
delete path['x-description'];
}
if (path && (path['x-servers']) && (Array.isArray(path['x-servers']))) {
path.servers = path['x-servers'];
delete path['x-servers'];
}
for (let method in path) {
if ((common.httpMethods.indexOf(method) >= 0) || (method === 'x-amazon-apigateway-any-method')) {
let op = path[method];
if (op && op.parameters && Array.isArray(op.parameters)) {
if (path.parameters) {
for (let param of path.parameters) {
if (typeof param.$ref === 'string') {
fixParamRef(param, options);
param = resolveInternal(openapi, param.$ref);
}
let match = op.parameters.find(function (e, i, a) {
return ((e.name === param.name) && (e.in === param.in));
});
if (!match && ((param.in === 'formData') || (param.in === 'body') || (param.type === 'file'))) {
op = processParameter(param, op, path, method, p, openapi, options);
if (options.rbname && op[options.rbname] === '') {
delete op[options.rbname];
}
}
}
}
for (let param of op.parameters) {
op = processParameter(param, op, path, method, method + ':' + p, openapi, options);
}
if (options.rbname && op[options.rbname] === '') {
delete op[options.rbname];
}
if (!options.debug) {
if (op.parameters) op.parameters = op.parameters.filter(keepParameters);
}
}
if (op && op.security) processSecurity(op.security);
//don't need to remove requestBody for non-supported ops as they "SHALL be ignored"
// responses
if (typeof op === 'object') {
if (!op.responses) {
let defaultResp = {};
defaultResp.description = 'Default response';
op.responses = { default: defaultResp };
}
for (let r in op.responses) {
let response = op.responses[r];
processResponse(response, r, op, openapi, options);
}
}
if (op && (op['x-servers']) && (Array.isArray(op['x-servers']))) {
op.servers = op['x-servers'];
delete op['x-servers'];
} else if (op && op.schemes && op.schemes.length) {
for (let scheme of op.schemes) {
if ((!openapi.schemes) || (openapi.schemes.indexOf(scheme) < 0)) {
if (!op.servers) {
op.servers = [];
}
if (Array.isArray(openapi.servers)) {
for (let server of openapi.servers) {
let newServer = clone(server);
let serverUrl = url.parse(newServer.url);
serverUrl.protocol = scheme;
newServer.url = serverUrl.format();
op.servers.push(newServer);
}
}
}
}
}
if (options.debug) {