Skip to content

Commit 70b8015

Browse files
committed
chore: revert "chore: merge branch 'feature/performance-optimization' into feature/hash"
This reverts commit 306bc3a, reversing changes made to 4cc36f9.
1 parent 306bc3a commit 70b8015

File tree

13 files changed

+33
-279
lines changed

13 files changed

+33
-279
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"devDependencies": {
3535
"@netcracker/qubership-apihub-compatibility-suites": "dev",
36-
"@netcracker/qubership-apihub-graphapi": "feature-performance-optimization",
36+
"@netcracker/qubership-apihub-graphapi": "1.0.8",
3737
"@netcracker/qubership-apihub-npm-gitflow": "3.1.0",
3838
"@types/jest": "29.5.11",
3939
"@types/node": "20.11.6",

src/api.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ import {
1616
SpecType,
1717
OpenApiSpecVersion,
1818
} from '@netcracker/qubership-apihub-api-unifier'
19-
import {
20-
DIFFS_AGGREGATED_META_KEY,
21-
DEFAULT_NORMALIZED_RESULT,
22-
DEFAULT_OPTION_DEFAULTS_META_KEY,
23-
DEFAULT_OPTION_ORIGINS_META_KEY,
24-
DIFF_META_KEY,
25-
} from './core'
19+
import { DEFAULT_NORMALIZED_RESULT, DEFAULT_OPTION_DEFAULTS_META_KEY, DEFAULT_OPTION_ORIGINS_META_KEY, DIFF_META_KEY } from './core'
2620

2721
function isOpenApiSpecVersion(specType: SpecType): specType is OpenApiSpecVersion {
2822
return specType === SPEC_TYPE_OPEN_API_30 || specType === SPEC_TYPE_OPEN_API_31
@@ -74,7 +68,6 @@ export function apiDiff(before: unknown, after: unknown, options: CompareOptions
7468
metaKey: DIFF_META_KEY,
7569
defaultsFlag: DEFAULT_OPTION_DEFAULTS_META_KEY,
7670
originsFlag: DEFAULT_OPTION_ORIGINS_META_KEY,
77-
diffsAggregatedFlag: DIFFS_AGGREGATED_META_KEY,
7871
compareScope: COMPARE_SCOPE_ROOT,
7972
mergedJsoCache: createEvaluationCacheService(),
8073
diffUniquenessCache: createEvaluationCacheService(),

src/core/compare.ts

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
anyArrayKeys,
3-
getNodeRules,
4-
JsonPath,
5-
syncClone,
6-
syncCrawl,
7-
SyncCrawlHook,
8-
} from '@netcracker/qubership-apihub-json-crawl'
1+
import { anyArrayKeys, getNodeRules, JsonPath, syncCrawl, SyncCrawlHook } from '@netcracker/qubership-apihub-json-crawl'
92

103
import {
114
ChainItem,
@@ -23,7 +16,6 @@ import { deepEqual } from 'fast-equals'
2316
import {
2417
AdapterContext,
2518
AdapterResolver,
26-
AGGREGATE_DIFFS_HERE_RULE,
2719
CompareContext,
2820
CompareResult,
2921
CompareRule,
@@ -246,7 +238,7 @@ const useMergeFactory = (onDiff: DiffCallback, options: InternalCompareOptions):
246238

247239
const beforeKey = unsafeKey ?? (isArray(beforeJso) ? +Object.keys(keyMap).pop()! : Object.keys(keyMap).pop())
248240
const afterKey = keyMap[beforeKey]
249-
const mergeKey = isArray(mergedJso) && isNumber(beforeKey) ? beforeKey : afterKey//THIS IS VERY FRAGILE. Cause this logic duplicate this line mergedJsoValue[keyInMerge] = afterValue[keyInAfter] #gitleaks:allow
241+
const mergeKey = isArray(mergedJso) && isNumber(beforeKey) ? beforeKey : afterKey //gitleaks:allow //THIS IS VERY FRAGILE. Cause this logic duplicate this line mergedJsoValue[keyInMerge] = afterValue[keyInAfter]
250242

251243
// skip if node was removed
252244
if (!(beforeKey in keyMap)) {
@@ -484,7 +476,7 @@ export const compare = (before: unknown, after: unknown, options: InternalCompar
484476
return {
485477
diffs: rawDiffs,
486478
ownerDiffEntry: undefined,
487-
merged: aggregateDiffs(merged, options),
479+
merged,
488480
}
489481
}
490482
const diffFlags = Symbol('diffs')
@@ -502,67 +494,8 @@ export const compare = (before: unknown, after: unknown, options: InternalCompar
502494
return {
503495
diffs: denormalizedDiffs,
504496
ownerDiffEntry: undefined,
505-
merged: merged,
506-
}
507-
}
508-
509-
export interface AggregateDiffsCrawlState {
510-
operationDiffs?: Set<Diff>
511-
}
512-
513-
export function aggregateDiffs(merged: unknown, options: InternalCompareOptions): unknown {
514-
let activeDataCycleGuard: Set<unknown> = new Set()
515-
516-
const collectCurrentNodeDiffs = (value: Record<string | symbol, unknown>, operationDiffs: Set<Diff>) => {
517-
if (options.metaKey in value) {
518-
const diffs = value[options.metaKey] as Record<PropertyKey, unknown> | undefined
519-
for (const key in diffs) {
520-
operationDiffs.add(diffs[key] as Diff)
521-
}
522-
}
523-
}
524-
525-
syncClone<AggregateDiffsCrawlState>(
526497
merged,
527-
[
528-
({ key, value, state, rules }) => {
529-
if (!isObject(value)) {
530-
return { value }
531-
}
532-
if (typeof key === 'symbol') {
533-
return { done: true }
534-
}
535-
if (activeDataCycleGuard.has(value)) {
536-
return { done: true }
537-
}
538-
activeDataCycleGuard.add(value)
539-
540-
if (state.operationDiffs) {
541-
collectCurrentNodeDiffs(value, state.operationDiffs)
542-
}
543-
544-
if (rules && AGGREGATE_DIFFS_HERE_RULE in rules) {
545-
activeDataCycleGuard = new Set()
546-
const operationDiffs = new Set<Diff>()
547-
collectCurrentNodeDiffs(value, operationDiffs)
548-
return {
549-
value,
550-
state: { ...state, operationDiffs },
551-
exitHook: () => {
552-
value[options.diffsAggregatedFlag] = operationDiffs
553-
},
554-
}
555-
}
556-
return { value }
557-
},
558-
],
559-
{
560-
state: {},
561-
rules: options.rules,
562-
},
563-
)
564-
565-
return merged
498+
}
566499
}
567500

568501
export const nestedCompare = (before: unknown, after: unknown, options: InternalCompareOptions): CompareResult => {

src/core/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ClassifyRule } from '../types'
22

33
export const DIFF_META_KEY = Symbol('$diff')
4-
export const DIFFS_AGGREGATED_META_KEY = Symbol('$diffs-aggregated')
54
export const DEFAULT_NORMALIZED_RESULT = false
65
export const DEFAULT_OPTION_DEFAULTS_META_KEY = Symbol('$defaults')
76
export const DEFAULT_OPTION_ORIGINS_META_KEY = Symbol('$origins')

src/graphapi/graphapi.rules.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
deepEqualsUniqueItemsArrayMappingResolver
1919
} from '../core'
2020
import { resolveSchemaDescriptionTemplates } from '../jsonSchema'
21-
import { AGGREGATE_DIFFS_HERE_RULE, ClassifyRule, CompareRules, DescriptionTemplates, MappingResolver } from '../types'
21+
import type { ClassifyRule, CompareRules, DescriptionTemplates, MappingResolver } from '../types'
2222
import { graphApiSchemaAdapter as graphApiTypeAdapter, removeNotCorrectlySupportedInterfacesAdapter } from './graphapi.adapter'
2323
import { COMPARE_SCOPE_COMPONENTS, COMPARE_SCOPE_DIRECTIVE_USAGES, COMPARE_SCOPE_ARGS, COMPARE_SCOPE_OUTPUT } from './graphapi.const'
2424
import { complexTypeCompareResolver } from './graphapi.resolver'
@@ -335,21 +335,18 @@ export const graphApiRules = (): CompareRules => {
335335
'/queries': {
336336
'/*': {
337337
...methodRules,
338-
[AGGREGATE_DIFFS_HERE_RULE]: true,
339338
$: addNonBreaking
340339
},
341340
},
342341
'/mutations': {
343342
'/*': {
344343
...methodRules,
345-
[AGGREGATE_DIFFS_HERE_RULE]: true,
346344
$: addNonBreaking
347345
},
348346
},
349347
'/subscriptions': {
350348
'/*': {
351349
...methodRules,
352-
[AGGREGATE_DIFFS_HERE_RULE]: true,
353350
$: addNonBreaking
354351
}
355352
},

src/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
export { COMPARE_MODE_DEFAULT, COMPARE_MODE_OPERATION } from './types'
22

33
export {
4-
ClassifierType,
5-
DiffAction,
6-
DIFF_META_KEY,
7-
DIFFS_AGGREGATED_META_KEY,
8-
breaking,
9-
nonBreaking,
10-
unclassified,
11-
annotation,
12-
deprecated,
13-
risky,
4+
ClassifierType, DiffAction, DIFF_META_KEY, breaking, nonBreaking, unclassified, annotation, deprecated, risky,
145
} from './core'
156

167
export { apiDiff } from './api'

src/openapi/openapi3.classify.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
breakingIfAfterTrue,
66
nonBreaking,
77
PARENT_JUMP,
8-
reverseClassifyRule,
98
strictResolveValueFromContext,
9+
reverseClassifyRule,
1010
transformClassifyRule,
1111
unclassified,
1212
} from '../core'
@@ -143,12 +143,9 @@ export const pathChangeClassifyRule: ClassifyRule = [
143143
({ before, after }) => {
144144
const beforePath = before.key as string
145145
const afterPath = after.key as string
146-
// todo uncomment
147-
// const unifiedBeforePath = createPathUnifier(before)(beforePath)
148-
// const unifiedAfterPath = createPathUnifier(after)(afterPath)
149146
const unifiedBeforePath = hidePathParamNames(beforePath)
150147
const unifiedAfterPath = hidePathParamNames(afterPath)
151-
148+
152149
// If unified paths are the same, it means only parameter names changed
153150
return unifiedBeforePath === unifiedAfterPath ? annotation : breaking
154151
}

src/openapi/openapi3.mapping.ts

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
import { MapKeysResult, MappingResolver, NodeContext } from '../types'
2-
import {
3-
difference,
4-
getStringValue,
5-
intersection,
6-
objectKeys,
7-
onlyExistedArrayIndexes,
8-
removeSlashes,
9-
} from '../utils'
1+
import type { MapKeysResult, MappingResolver } from '../types'
2+
import { getStringValue, objectKeys, onlyExistedArrayIndexes } from '../utils'
103
import { mapPathParams } from './openapi3.utils'
11-
import { OpenAPIV3 } from 'openapi-types'
124

135
export const singleOperationPathMappingResolver: MappingResolver<string> = (before, after) => {
146

@@ -31,40 +23,32 @@ export const singleOperationPathMappingResolver: MappingResolver<string> = (befo
3123
return result
3224
}
3325

34-
export const pathMappingResolver: MappingResolver<string> = (before, after, ctx) => {
26+
export const pathMappingResolver: MappingResolver<string> = (before, after) => {
3527

3628
const result: MapKeysResult<string> = { added: [], removed: [], mapped: {} }
3729

38-
const unifyBeforePath = createPathUnifier(ctx.before)
39-
const unifyAfterPath = createPathUnifier(ctx.after)
30+
const originalBeforeKeys = objectKeys(before)
31+
const originalAfterKeys = objectKeys(after)
32+
const unifiedAfterKeys = originalAfterKeys.map(hidePathParamNames)
4033

41-
const unifiedBeforeKeyToKey = Object.fromEntries(objectKeys(before).map(key => [unifyBeforePath(key), key]))
42-
const unifiedAfterKeyToKey = Object.fromEntries(objectKeys(after).map(key => [unifyAfterPath(key), key]))
34+
const notMappedAfterIndices = new Set(originalAfterKeys.keys())
4335

44-
const unifiedBeforeKeys = Object.keys(unifiedBeforeKeyToKey)
45-
const unifiedAfterKeys = Object.keys(unifiedAfterKeyToKey)
36+
originalBeforeKeys.forEach(beforeKey => {
37+
const unifiedBeforePath = hidePathParamNames(beforeKey)
38+
const index = unifiedAfterKeys.indexOf(unifiedBeforePath)
4639

47-
result.added = difference(unifiedAfterKeys, unifiedBeforeKeys).map(key => unifiedAfterKeyToKey[key])
48-
result.removed = difference(unifiedBeforeKeys, unifiedAfterKeys).map(key => unifiedBeforeKeyToKey[key])
49-
result.mapped = Object.fromEntries(
50-
intersection(unifiedBeforeKeys, unifiedAfterKeys).map(key => [unifiedBeforeKeyToKey[key], unifiedAfterKeyToKey[key]]),
51-
)
52-
53-
return result
54-
}
55-
56-
export const methodMappingResolver: MappingResolver<string> = (before, after) => {
57-
58-
const result: MapKeysResult<string> = { added: [], removed: [], mapped: {} }
59-
60-
const beforeKeys = objectKeys(before)
61-
const afterKeys = objectKeys(after)
62-
63-
result.added = difference(afterKeys, beforeKeys)
64-
result.removed = difference(beforeKeys, afterKeys)
40+
if (index < 0) {
41+
// removed item
42+
result.removed.push(beforeKey)
43+
} else {
44+
// mapped items
45+
result.mapped[beforeKey] = originalAfterKeys[index]
46+
notMappedAfterIndices.delete(index)
47+
}
48+
})
6549

66-
const mapped = intersection(beforeKeys, afterKeys)
67-
mapped.forEach(key => result.mapped[key] = key)
50+
// added items
51+
notMappedAfterIndices.forEach((notMappedIndex) => result.added.push(originalAfterKeys[notMappedIndex]))
6852

6953
return result
7054
}
@@ -191,31 +175,6 @@ function isWildcardCompatible(beforeType: string, afterType: string): boolean {
191175
return true
192176
}
193177

194-
// todo copy-paste from api-processor
195-
export const extractOperationBasePath = (servers?: OpenAPIV3.ServerObject[]): string => {
196-
if (!Array.isArray(servers) || !servers.length) { return '' }
197-
198-
try {
199-
const [firstServer] = servers
200-
let serverUrl = firstServer.url
201-
const { variables = {} } = firstServer
202-
203-
for (const param of Object.keys(variables)) {
204-
serverUrl = serverUrl.replace(new RegExp(`{${param}}`, 'g'), variables[param].default)
205-
}
206-
207-
const { pathname } = new URL(serverUrl, 'https://localhost')
208-
return pathname.slice(-1) === '/' ? pathname.slice(0, -1) : pathname
209-
} catch (error) {
210-
return ''
211-
}
212-
}
213-
214-
export function createPathUnifier(nodeContext: NodeContext): (path: string) => string {
215-
const serverPrefix = extractOperationBasePath((nodeContext.root as OpenAPIV3.Document).servers) // /api/v2
216-
return (path) => removeSlashes(`${serverPrefix}${hidePathParamNames(path)}`)
217-
}
218-
219178
export function hidePathParamNames(path: string): string {
220179
return path.replace(PATH_PARAMETER_REGEXP, PATH_PARAM_UNIFIED_PLACEHOLDER)
221180
}

src/openapi/openapi3.rules.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
deepEqualsUniqueItemsArrayMappingResolver,
3131
} from '../core'
3232
import {
33-
AGGREGATE_DIFFS_HERE_RULE,
3433
COMPARE_MODE_OPERATION,
3534
CompareRules,
3635
DescriptionTemplates,
@@ -55,7 +54,6 @@ import {
5554
} from './openapi3.classify'
5655
import {
5756
contentMediaTypeMappingResolver,
58-
methodMappingResolver,
5957
paramMappingResolver,
6058
pathMappingResolver,
6159
singleOperationPathMappingResolver,
@@ -347,7 +345,6 @@ export const openApi3Rules = (options: OpenApi3RulesOptions): CompareRules => {
347345

348346
const operationRule: CompareRules = {
349347
$: [nonBreaking, breaking, unclassified],
350-
[AGGREGATE_DIFFS_HERE_RULE]: true,
351348
'/callbacks': {
352349
'/*': {
353350
//no support?
@@ -393,7 +390,7 @@ export const openApi3Rules = (options: OpenApi3RulesOptions): CompareRules => {
393390

394391
const pathItemObjectRules = (options: OpenApi3RulesOptions): CompareRules => ({
395392
$: pathChangeClassifyRule,
396-
mapping: options.mode === COMPARE_MODE_OPERATION ? singleOperationPathMappingResolver : methodMappingResolver,
393+
mapping: options.mode === COMPARE_MODE_OPERATION ? singleOperationPathMappingResolver : pathMappingResolver,
397394
'/description': { $: allAnnotation },
398395
'/parameters': {
399396
$: [nonBreaking, breaking, breaking],
@@ -434,7 +431,6 @@ export const openApi3Rules = (options: OpenApi3RulesOptions): CompareRules => {
434431
'/*': pathItemObjectRules(options),
435432
},
436433
'/securitySchemes': {
437-
[AGGREGATE_DIFFS_HERE_RULE]: true,
438434
$: [breaking, nonBreaking, breaking],
439435
'/*': {
440436
$: [breaking, nonBreaking, breaking],
@@ -456,18 +452,14 @@ export const openApi3Rules = (options: OpenApi3RulesOptions): CompareRules => {
456452
...documentAnnotationRule,
457453
'/**': documentAnnotationRule,
458454
},
459-
'/servers': {
460-
[AGGREGATE_DIFFS_HERE_RULE]: true,
461-
...serversRules,
462-
},
455+
'/servers': serversRules,
463456
'/paths': {
464457
$: allUnclassified,
465458
mapping: options.mode === COMPARE_MODE_OPERATION ? singleOperationPathMappingResolver : pathMappingResolver,
466459
'/*': pathItemObjectRules(options),
467460
},
468461
'/components': componentsRule,
469462
'/security': {
470-
[AGGREGATE_DIFFS_HERE_RULE]: true,
471463
$: globalSecurityClassifyRule,
472464
'/*': { $: globalSecurityItemClassifyRule },
473465
},

0 commit comments

Comments
 (0)