@@ -57,6 +57,14 @@ func TestUpgrade_Success(t *testing.T) {
57
57
options : nil ,
58
58
description : "nullable schema should upgrade to oneOf without panic" ,
59
59
},
60
+ {
61
+ name : "upgrade_3_1_0_with_custom_methods" ,
62
+ inputFile : "testdata/upgrade/3_1_0_with_custom_methods.yaml" ,
63
+ expectedFile : "testdata/upgrade/expected_3_1_0_with_custom_methods_upgraded.yaml" ,
64
+ options : []openapi.Option [openapi.UpgradeOptions ]{openapi .WithUpgradeTargetVersion ("3.2.0" )},
65
+ description : "3.1.0 with custom HTTP methods should migrate to additionalOperations" ,
66
+ targetVersion : "3.2.0" ,
67
+ },
60
68
}
61
69
62
70
for _ , tt := range tests {
@@ -311,3 +319,75 @@ components:
311
319
assert .Nil (t , simpleExample .Example , "example should be nil" )
312
320
assert .NotEmpty (t , simpleExample .Examples , "examples should not be empty" )
313
321
}
322
+
323
+ func TestUpgradeAdditionalOperations (t * testing.T ) {
324
+ t .Parallel ()
325
+
326
+ ctx := t .Context ()
327
+
328
+ // Create a document with non-standard HTTP methods
329
+ doc := & openapi.OpenAPI {
330
+ OpenAPI : "3.1.0" ,
331
+ Info : openapi.Info {
332
+ Title : "Test API" ,
333
+ Version : "1.0.0" ,
334
+ },
335
+ Paths : openapi .NewPaths (),
336
+ }
337
+
338
+ // Add a path with both standard and non-standard methods
339
+ pathItem := openapi .NewPathItem ()
340
+
341
+ // Standard method
342
+ pathItem .Set (openapi .HTTPMethodGet , & openapi.Operation {
343
+ Summary : & []string {"Get operation" }[0 ],
344
+ Responses : openapi .NewResponses (),
345
+ })
346
+
347
+ // Non-standard methods
348
+ pathItem .Set (openapi .HTTPMethod ("copy" ), & openapi.Operation {
349
+ Summary : & []string {"Copy operation" }[0 ],
350
+ Responses : openapi .NewResponses (),
351
+ })
352
+
353
+ pathItem .Set (openapi .HTTPMethod ("purge" ), & openapi.Operation {
354
+ Summary : & []string {"Purge operation" }[0 ],
355
+ Responses : openapi .NewResponses (),
356
+ })
357
+
358
+ doc .Paths .Set ("/test" , & openapi.ReferencedPathItem {Object : pathItem })
359
+
360
+ // Verify initial state
361
+ assert .Equal (t , 3 , pathItem .Len (), "should have 3 operations initially" )
362
+ assert .Nil (t , pathItem .AdditionalOperations , "additionalOperations should be nil initially" )
363
+ assert .NotNil (t , pathItem .GetOperation (openapi .HTTPMethod ("copy" )), "copy operation should exist in main map" )
364
+ assert .NotNil (t , pathItem .GetOperation (openapi .HTTPMethod ("purge" )), "purge operation should exist in main map" )
365
+
366
+ // Perform upgrade to 3.2.0
367
+ upgraded , err := openapi .Upgrade (ctx , doc , openapi .WithUpgradeTargetVersion ("3.2.0" ))
368
+ require .NoError (t , err , "upgrade should not fail" )
369
+ assert .True (t , upgraded , "upgrade should have been performed" )
370
+ assert .Equal (t , "3.2.0" , doc .OpenAPI , "version should be 3.2.0" )
371
+
372
+ // Verify migration results
373
+ assert .Equal (t , 1 , pathItem .Len (), "should have only 1 operation in main map after migration" )
374
+ assert .NotNil (t , pathItem .AdditionalOperations , "additionalOperations should be initialized" )
375
+ assert .Equal (t , 2 , pathItem .AdditionalOperations .Len (), "should have 2 operations in additionalOperations" )
376
+
377
+ // Verify standard method remains in main map
378
+ assert .NotNil (t , pathItem .GetOperation (openapi .HTTPMethodGet ), "get operation should remain in main map" )
379
+
380
+ // Verify non-standard methods are moved to additionalOperations
381
+ assert .Nil (t , pathItem .GetOperation (openapi .HTTPMethod ("copy" )), "copy operation should be removed from main map" )
382
+ assert .Nil (t , pathItem .GetOperation (openapi .HTTPMethod ("purge" )), "purge operation should be removed from main map" )
383
+
384
+ copyOp , exists := pathItem .AdditionalOperations .Get ("copy" )
385
+ assert .True (t , exists , "copy operation should exist in additionalOperations" )
386
+ assert .NotNil (t , copyOp , "copy operation should not be nil" )
387
+ assert .Equal (t , "Copy operation" , * copyOp .Summary , "copy operation summary should be preserved" )
388
+
389
+ purgeOp , exists := pathItem .AdditionalOperations .Get ("purge" )
390
+ assert .True (t , exists , "purge operation should exist in additionalOperations" )
391
+ assert .NotNil (t , purgeOp , "purge operation should not be nil" )
392
+ assert .Equal (t , "Purge operation" , * purgeOp .Summary , "purge operation summary should be preserved" )
393
+ }
0 commit comments