@@ -29,16 +29,15 @@ import {
2929 apiDiff ,
3030 breaking ,
3131 COMPARE_MODE_OPERATION ,
32- DEFAULT_DIFFS_AGGREGATED_META_KEY ,
3332 Diff ,
3433 DIFF_META_KEY ,
3534 DiffAction ,
35+ DIFFS_AGGREGATED_META_KEY ,
3636 risky ,
3737} from '@netcracker/qubership-apihub-api-diff'
3838import { MESSAGE_SEVERITY , NORMALIZE_OPTIONS , ORIGINS_SYMBOL } from '../../consts'
3939import {
4040 BREAKING_CHANGE_TYPE ,
41- CompareContext ,
4241 CompareOperationsPairContext ,
4342 NormalizedOperationId ,
4443 OperationChanges ,
@@ -66,12 +65,22 @@ import { createOperationChange, getOperationTags, takeSubstringIf } from '../../
6665export const compareDocuments = async ( apiType : OperationsApiType , operationsMap : Record < NormalizedOperationId , {
6766 previous ?: ResolvedOperation
6867 current ?: ResolvedOperation
69- } > , prevFile : File , currFile : File , currDoc : ResolvedVersionDocument , prevDoc : ResolvedVersionDocument , ctx : CompareContext ) : Promise < {
68+ } > , prevDoc : ResolvedVersionDocument | undefined , currDoc : ResolvedVersionDocument | undefined , ctx : CompareOperationsPairContext ) : Promise < {
7069 operationChanges : OperationChanges [ ]
7170 tags : string [ ]
7271} > => {
73- const prevDocData = JSON . parse ( await prevFile . text ( ) )
74- const currDocData = JSON . parse ( await currFile . text ( ) )
72+ const { rawDocumentResolver, previousVersion, currentVersion, previousPackageId, currentPackageId } = ctx
73+ const prevFile = prevDoc && await rawDocumentResolver ( previousVersion , previousPackageId , prevDoc . slug )
74+ const currFile = currDoc && await rawDocumentResolver ( currentVersion , currentPackageId , currDoc . slug )
75+ let prevDocData = prevFile && JSON . parse ( await prevFile . text ( ) )
76+ let currDocData = currFile && JSON . parse ( await currFile . text ( ) )
77+
78+ if ( ! prevDocData && currDocData ) {
79+ prevDocData = createCopyWithEmptyPath ( currDocData )
80+ }
81+ if ( prevDocData && ! currDocData ) {
82+ currDocData = createCopyWithEmptyPath ( prevDocData )
83+ }
7584
7685 const { merged, diffs } = apiDiff (
7786 prevDocData ,
@@ -80,7 +89,7 @@ export const compareDocuments = async (apiType: OperationsApiType, operationsMap
8089 ...NORMALIZE_OPTIONS ,
8190 metaKey : DIFF_META_KEY ,
8291 originsFlag : ORIGINS_SYMBOL ,
83- diffsAggregatedFlag : DEFAULT_DIFFS_AGGREGATED_META_KEY ,
92+ diffsAggregatedFlag : DIFFS_AGGREGATED_META_KEY ,
8493 // mode: COMPARE_MODE_OPERATION,
8594 normalizedResult : true ,
8695 } ,
@@ -128,17 +137,22 @@ export const compareDocuments = async (apiType: OperationsApiType, operationsMap
128137 let operationDiffs : Diff [ ] = [ ]
129138 if ( current && previous ) {
130139 operationDiffs = [
131- ...( methodData as WithAggregatedDiffs < OpenAPIV3 . OperationObject > ) [ DEFAULT_DIFFS_AGGREGATED_META_KEY ] ,
140+ ...( methodData as WithAggregatedDiffs < OpenAPIV3 . OperationObject > ) [ DIFFS_AGGREGATED_META_KEY ] ,
132141 // todo what about security? add test
133142 ...extractServersDiffs ( merged ) ,
134143 ]
135144
136145 const pathParamRenameDiff = ( merged . paths as WithDiffMetaRecord < OpenAPIV3 . PathsObject > ) [ DIFF_META_KEY ] ?. [ path ]
137146 pathParamRenameDiff && operationDiffs . push ( pathParamRenameDiff )
138147 } else if ( current || previous ) {
139- const operationDiff = ( merged . paths as WithDiffMetaRecord < OpenAPIV3 . PathsObject > ) [ DIFF_META_KEY ] ?. [ path ]
148+ const operationDiff = ( merged . paths [ path ] as WithDiffMetaRecord < OpenAPIV3 . PathsObject > ) [ DIFF_META_KEY ] ?. [ inferredMethod ]
140149 if ( ! operationDiff ) {
141- throw new Error ( 'should not happen' )
150+ // ignore removed and added operations, they'll be handled in a separate docs comparison
151+ continue
152+ }
153+ const deprecatedInVersionsCount = previousVersionDeprecations ?. operations . find ( ( operation ) => operation . operationId === operationId ) ?. deprecatedInPreviousVersions ?. length ?? 0
154+ if ( isOperationRemove ( operationDiff ) && deprecatedInVersionsCount > 1 ) {
155+ operationDiff . type = risky
142156 }
143157 operationDiffs . push ( operationDiff )
144158 }
@@ -162,11 +176,11 @@ export const compareRestOperationsData = async (current: VersionRestOperation |
162176 let previousOperation = removeComponents ( previous ?. data )
163177 let currentOperation = removeComponents ( current ?. data )
164178 if ( ! previousOperation && currentOperation ) {
165- previousOperation = getCopyWithEmptyPath ( currentOperation as RestOperationData )
179+ previousOperation = createCopyWithEmptyPath ( currentOperation as RestOperationData )
166180 }
167181
168182 if ( previousOperation && ! currentOperation ) {
169- currentOperation = getCopyWithEmptyPath ( previousOperation as RestOperationData )
183+ currentOperation = createCopyWithEmptyPath ( previousOperation as RestOperationData )
170184 }
171185
172186 const diffResult = apiDiff (
@@ -201,7 +215,6 @@ async function reclassifyBreakingChanges(
201215 if ( ! previosVersionDeprecations ) {
202216 return
203217 }
204- previosVersionDeprecations . operations [ 0 ]
205218
206219 const previousOperation = previosVersionDeprecations . operations [ 0 ]
207220
@@ -272,11 +285,14 @@ async function reclassifyBreakingChanges(
272285 }
273286}
274287
275- function getCopyWithEmptyPath ( template : RestOperationData ) : RestOperationData {
288+ export function createCopyWithEmptyPath ( template : RestOperationData ) : RestOperationData {
276289 // eslint-disable-next-line @typescript-eslint/no-unused-vars
277290 const { paths, ...rest } = template
291+
278292 return {
279- paths : { } ,
293+ paths : {
294+ ...Object . fromEntries ( Object . keys ( template . paths ) . map ( key => [ key , { } ] ) ) ,
295+ } ,
280296 ...rest ,
281297 }
282298}
0 commit comments