@@ -4,6 +4,17 @@ import { readFileSync } from 'fs'
44import { load } from 'js-yaml'
55import { diffsMatcher } from './helper/matchers'
66
7+ // Helper function for OpenAPI version change assertion
8+ const expectOpenApiVersionChange = ( fromVersion : string = '3.0.4' , toVersion : string = '3.1.0' ) =>
9+ expect . objectContaining ( {
10+ action : 'replace' ,
11+ afterDeclarationPaths : [ [ 'openapi' ] ] ,
12+ afterValue : toVersion ,
13+ beforeDeclarationPaths : [ [ 'openapi' ] ] ,
14+ beforeValue : fromVersion ,
15+ type : 'annotation' ,
16+ } )
17+
718const TEST_NORMALIZE_OPTIONS : CompareOptions = {
819 validate : true ,
920 liftCombiners : true ,
@@ -14,7 +25,7 @@ const TEST_NORMALIZE_OPTIONS: CompareOptions = {
1425 allowNotValidSyntheticChanges : true ,
1526}
1627
17- describe ( 'OpenAPI 3.0 to 3.1 Migration Tests' , ( ) => {
28+ describe ( 'OpenAPI 3.0 to 3.1 Comparison Tests' , ( ) => {
1829 test ( 'empty-schema' , ( ) => {
1930 // Load the before and after files
2031 const beforePath = './test/helper/resources/3_0-to-3_1/empty-schema/before.yaml'
@@ -32,64 +43,84 @@ describe('OpenAPI 3.0 to 3.1 Migration Tests', () => {
3243
3344 // Check the result - expecting changes from empty schema {} to typed schemas
3445 expect ( diffs ) . toEqual ( diffsMatcher ( [
35- expect . objectContaining ( {
36- action : 'replace' ,
37- afterDeclarationPaths : [ [ 'openapi' ] ] ,
38- afterValue : '3.1.0' ,
39- beforeDeclarationPaths : [ [ 'openapi' ] ] ,
40- beforeValue : '3.0.3' ,
41- type : 'annotation' ,
42- } ) ,
43- ] ) )
44- } )
45-
46- test ( 'nullable-to-union' , ( ) => {
47- // Load the before and after files
48- const beforePath = './test/helper/resources/3_0-to-3_1/nullable-to-union/before.yaml'
49- const afterPath = './test/helper/resources/3_0-to-3_1/nullable-to-union/after.yaml'
50-
51- const beforeSource = load ( readFileSync ( beforePath ) . toString ( ) )
52- const afterSource = load ( readFileSync ( afterPath ) . toString ( ) )
53-
54- // Call apiDiff
55- const { diffs } = apiDiff ( beforeSource , afterSource , {
56- ...TEST_NORMALIZE_OPTIONS ,
57- beforeSource,
58- afterSource,
59- } )
60-
61- // Check the result - expecting changes from nullable: true to union types
62- expect ( diffs ) . toEqual ( diffsMatcher ( [
63- // TODO: Add assertions for nullable to union type changes
46+ expectOpenApiVersionChange ( ) ,
6447 ] ) )
65- } )
48+ } )
6649
6750 test ( 'add-overriden-description' , ( ) => {
68- // Load the before and after files
6951 const beforePath = './test/helper/resources/3_0-to-3_1/add-overriden-description/before.yaml'
7052 const afterPath = './test/helper/resources/3_0-to-3_1/add-overriden-description/after.yaml'
7153
7254 const beforeSource = load ( readFileSync ( beforePath ) . toString ( ) )
7355 const afterSource = load ( readFileSync ( afterPath ) . toString ( ) )
7456
75- // Call apiDiff
7657 const { diffs } = apiDiff ( beforeSource , afterSource , {
7758 ...TEST_NORMALIZE_OPTIONS ,
7859 beforeSource,
7960 afterSource,
80- } )
61+ } )
8162
82- // Check the result - expecting OpenAPI version change and added overridden description
8363 expect ( diffs ) . toEqual ( diffsMatcher ( [
64+ expectOpenApiVersionChange ( ) ,
8465 expect . objectContaining ( {
8566 action : 'replace' ,
86- afterDeclarationPaths : [ [ 'openapi' ] ] ,
87- afterValue : '3.1.0 ' ,
88- beforeDeclarationPaths : [ [ 'openapi ' ] ] ,
89- beforeValue : '3.0.0' ,
67+ beforeValue : 'response description from components' ,
68+ afterValue : 'response description override ' ,
69+ afterDeclarationPaths : [ [ 'paths' , '/path1' , 'post' , 'responses' , '200' , 'description '] ] ,
70+ beforeDeclarationPaths : [ [ 'components' , 'responses' , 'response200' , 'description' ] ] ,
9071 type : 'annotation' ,
9172 } ) ,
92- // Additional diffs for the overridden description will be added after running the test
9373 ] ) )
9474 } )
75+
76+ describe ( 'nullable and null type' , ( ) => {
77+ test ( 'nullable-to-union' , ( ) => {
78+ // Load the before and after files
79+ const beforePath = './test/helper/resources/3_0-to-3_1/nullable-to-union/before.yaml'
80+ const afterPath = './test/helper/resources/3_0-to-3_1/nullable-to-union/after.yaml'
81+
82+ const beforeSource = load ( readFileSync ( beforePath ) . toString ( ) )
83+ const afterSource = load ( readFileSync ( afterPath ) . toString ( ) )
84+
85+ // Call apiDiff
86+ const { diffs } = apiDiff ( beforeSource , afterSource , {
87+ ...TEST_NORMALIZE_OPTIONS ,
88+ beforeSource,
89+ afterSource,
90+ } )
91+
92+ expect ( diffs . length ) . toBe ( 1 )
93+
94+ expect ( diffs ) . toEqual ( diffsMatcher ( [
95+ expectOpenApiVersionChange ( ) ,
96+ ] ) )
97+ } )
98+
99+ test ( 'nullable-to-null' , ( ) => {
100+ // Load the before and after files
101+ const beforePath = './test/helper/resources/3_0-to-3_1/nullable-to-null/before.yaml'
102+ const afterPath = './test/helper/resources/3_0-to-3_1/nullable-to-null/after.yaml'
103+
104+ const beforeSource = load ( readFileSync ( beforePath ) . toString ( ) )
105+ const afterSource = load ( readFileSync ( afterPath ) . toString ( ) )
106+
107+ // Call apiDiff
108+ const { diffs } = apiDiff ( beforeSource , afterSource , {
109+ ...TEST_NORMALIZE_OPTIONS ,
110+ beforeSource,
111+ afterSource,
112+ } )
113+
114+ expect ( diffs ) . toEqual ( diffsMatcher ( [
115+ expectOpenApiVersionChange ( ) ,
116+ expect . objectContaining ( {
117+ action : 'remove' ,
118+ //TODO: validate declaration path
119+ beforeDeclarationPaths : [ [ 'paths' , '/path1' , 'get' , 'responses' , '200' , 'content' , 'application/json' , 'schema' ] ] ,
120+ scope : 'response' ,
121+ type : 'non-breaking' ,
122+ } ) ,
123+ ] ) )
124+ } )
125+ } )
95126} )
0 commit comments