@@ -29,6 +29,20 @@ import { GraphQLOperationType } from './types/graphql'
29
29
30
30
const preprocessingLog = debug ( 'preprocessing' )
31
31
32
+ /**
33
+ * Given an operation object from the OAS, create an Operation, which contains
34
+ * the necessary data to create a GraphQL wrapper for said operation object.
35
+ *
36
+ * @param path The path of the operation object
37
+ * @param method The method of the operation object
38
+ * @param operationString A string representation of the path and the method (and the OAS title if applicable)
39
+ * @param operationType Whether the operation should be turned into a Query/Mutation/Subscription operation
40
+ * @param operation The operation object from the OAS
41
+ * @param pathItem The path item object from the OAS from which the operation object is derived from
42
+ * @param oas The OAS from which the path item and operation object are derived from
43
+ * @param data An assortment of data which at this point is mainly used enable logging
44
+ * @param options The options passed by the user
45
+ */
32
46
function processOperation (
33
47
path : string ,
34
48
method : string ,
@@ -305,7 +319,10 @@ export function preprocessOas(
305
319
}
306
320
307
321
// Process all callbacks
308
- if ( operation . callbacks ) {
322
+ if (
323
+ data . options . createSubscriptionsFromCallbacks &&
324
+ operation . callbacks
325
+ ) {
309
326
Object . entries ( operation . callbacks ) . forEach (
310
327
( [ callbackName , callback ] ) => {
311
328
const resolvedCallback = ! ( '$ref' in callback )
@@ -323,59 +340,75 @@ export function preprocessOas(
323
340
? callbackPathItem
324
341
: Oas3Tools . resolveRef ( callbackPathItem [ '$ref' ] , oas )
325
342
326
- Object . keys ( resolvedCallbackPathItem )
327
- . filter ( objectKey => {
343
+ const callbackOperationObjectMethods = Object . keys (
344
+ resolvedCallbackPathItem
345
+ ) . filter ( objectKey => {
346
+ /**
347
+ * Get only fields that contain operation objects
348
+ *
349
+ * Can also contain other fields such as summary or description
350
+ */
351
+ return Oas3Tools . isOperation ( objectKey )
352
+ } )
353
+
354
+ if ( callbackOperationObjectMethods . length > 0 ) {
355
+ if ( callbackOperationObjectMethods . length > 1 ) {
356
+ handleWarning ( {
357
+ typeKey :
358
+ 'CALLBACKS_MULTIPLE_OPERATION_OBJECT_METHODS' ,
359
+ message : `Callback '${ callbackExpression } ' on operation '${ operationString } ' has multiple operation objects with the methods '${ callbackOperationObjectMethods } '. OpenAPI-to-GraphQL can only utilize one of these operation objects.` ,
360
+ mitigationAddendum : `The operation with the method '${ callbackOperationObjectMethods [ 0 ] } ' will be selected and all others will be ignored.` ,
361
+ data,
362
+ log : preprocessingLog
363
+ } )
364
+ }
365
+
366
+ // Select only one of the operation object methods
367
+ const callbackMethod = callbackOperationObjectMethods [ 0 ]
368
+
369
+ const callbackOperationString = Oas3Tools . formatOperationString (
370
+ method ,
371
+ callbackName
372
+ )
373
+
374
+ const callbackOperation = processOperation (
375
+ callbackExpression ,
376
+ callbackMethod ,
377
+ callbackOperationString ,
378
+ GraphQLOperationType . Subscription ,
379
+ resolvedCallbackPathItem [ callbackMethod ] ,
380
+ callbackPathItem ,
381
+ oas ,
382
+ data ,
383
+ options
384
+ )
385
+
386
+ if ( callbackOperation ) {
328
387
/**
329
- * Get only fields that contain operation objects
330
- *
331
- * Can also contain other fields such as summary or description
388
+ * Handle operationId property name collision
389
+ * May occur if multiple OAS are provided
332
390
*/
333
- return Oas3Tools . isOperation ( objectKey )
334
- } )
335
- . forEach ( callbackMethod => {
336
- const callbackOperationString = Oas3Tools . formatOperationString (
337
- method ,
338
- callbackName
339
- )
340
-
341
- const callbackOperation = processOperation (
342
- callbackExpression ,
343
- callbackMethod ,
344
- callbackOperationString ,
345
- GraphQLOperationType . Subscription ,
346
- resolvedCallbackPathItem [ callbackMethod ] ,
347
- callbackPathItem ,
348
- oas ,
349
- data ,
350
- options
351
- )
352
-
353
- if ( callbackOperation ) {
354
- /**
355
- * Handle operationId property name collision
356
- * May occur if multiple OAS are provided
357
- */
358
- if (
359
- callbackOperation &&
360
- ! (
361
- callbackOperation . operationId in
362
- data . callbackOperations
363
- )
364
- ) {
365
- data . callbackOperations [
366
- callbackOperation . operationId
367
- ] = callbackOperation
368
- } else {
369
- handleWarning ( {
370
- typeKey : 'DUPLICATE_OPERATIONID' ,
371
- message : `Multiple OASs share callback operations with the same operationId '${ callbackOperation . operationId } '` ,
372
- mitigationAddendum : `The callback operation from the OAS '${ operationData . oas . info . title } ' will be ignored` ,
373
- data,
374
- log : preprocessingLog
375
- } )
376
- }
391
+ if (
392
+ callbackOperation &&
393
+ ! (
394
+ callbackOperation . operationId in
395
+ data . callbackOperations
396
+ )
397
+ ) {
398
+ data . callbackOperations [
399
+ callbackOperation . operationId
400
+ ] = callbackOperation
401
+ } else {
402
+ handleWarning ( {
403
+ typeKey : 'DUPLICATE_OPERATIONID' ,
404
+ message : `Multiple OASs share callback operations with the same operationId '${ callbackOperation . operationId } '` ,
405
+ mitigationAddendum : `The callback operation from the OAS '${ operationData . oas . info . title } ' will be ignored` ,
406
+ data,
407
+ log : preprocessingLog
408
+ } )
377
409
}
378
- } )
410
+ }
411
+ }
379
412
}
380
413
)
381
414
}
0 commit comments