Skip to content

Commit bfb27a2

Browse files
committed
feat: Review
1 parent c034d00 commit bfb27a2

File tree

4 files changed

+26
-29
lines changed

4 files changed

+26
-29
lines changed

src/apitypes/rest/rest.changes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import {
8282
getOperationTags,
8383
OperationsMap,
8484
} from '../../components'
85-
import { createApiKindChecker, getApiKindFormLabels } from '../../components/compare/bwc.validation'
85+
import { createApiKindChecker, getApiKindFromLabels } from '../../components/compare/bwc.validation'
8686

8787
export const compareDocuments: DocumentsCompare = async (
8888
operationsMap: OperationsMap,
@@ -136,8 +136,8 @@ export const compareDocuments: DocumentsCompare = async (
136136
afterValueNormalizedProperty: AFTER_VALUE_NORMALIZED_PROPERTY,
137137
beforeValueNormalizedProperty: BEFORE_VALUE_NORMALIZED_PROPERTY,
138138
apiCompatibilityScopeFunction: createApiKindChecker(
139-
prevDoc?.apiKind ?? getApiKindFormLabels(prevDocData?.info, prevDoc?.labels, previousVersionLabels),
140-
currDoc?.apiKind ?? getApiKindFormLabels(currDocData?.info, currDoc?.labels, currentVersionLabels),
139+
prevDoc?.apiKind ?? getApiKindFromLabels(prevDocData?.info, prevDoc?.labels, previousVersionLabels),
140+
currDoc?.apiKind ?? getApiKindFromLabels(currDocData?.info, currDoc?.labels, currentVersionLabels),
141141
),
142142
},
143143
) as { merged: OpenAPIV3.Document; diffs: Diff[] }

src/apitypes/rest/rest.document.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const buildRestDocument: DocumentBuilder<OpenAPIV3.Document> = async (par
7676

7777
let bundledFileData = data
7878

79-
const documentKind = getApiKind(bundledFileData?.info, apiKind)
79+
const documentApiKind = getApiKind(bundledFileData?.info, apiKind)
8080

8181
if (parsedFile.type === REST_DOCUMENT_TYPE.SWAGGER) {
8282
try {
@@ -99,7 +99,7 @@ export const buildRestDocument: DocumentBuilder<OpenAPIV3.Document> = async (par
9999
fileId: parsedFileId,
100100
type: type,
101101
format: FILE_FORMAT.JSON,
102-
apiKind: documentKind,
102+
apiKind: documentApiKind,
103103
data: bundledFileData,
104104
slug, // unique slug should be already generated
105105
filename: `${slug}.${FILE_FORMAT.JSON}`,

src/components/compare/bwc.validation.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
ApiCompatibilityScopeFunction,
2424
} from '@netcracker/qubership-apihub-api-diff'
2525
import { Labels } from '../../types'
26-
import { findApiKindLabel, getApiKind } from '../document'
26+
import { calculateApiKindFromLabels, getApiKind } from '../document'
2727
import { OpenAPIV3 } from 'openapi-types'
2828

2929
export const getApiCompatibilityKind = (beforeJson: unknown, afterJson: unknown): ApiCompatibilityKind | undefined => {
@@ -63,10 +63,6 @@ const checkAllMethodsHaveSameApiKind = (obj: unknown, apiKind: string): boolean
6363
if (!isObject(obj)) {
6464
return false
6565
}
66-
67-
if (hasApiKind(obj, apiKind)) {
68-
return true
69-
}
7066
const entries = Object.entries(obj)
7167

7268
return entries.length > 0 &&
@@ -110,39 +106,40 @@ export const createApiKindChecker = (
110106
* Level 3: When individual operations are deleted/added
111107
*/
112108
const isFirstPathSegmentPaths = path?.[0] === 'paths'
113-
if (!isFirstPathSegmentPaths || pathLength < PATH_ITEM_PATH_LENGTH || pathLength > OPERATION_OBJECT_PATH_LENGTH) {
109+
if (!isFirstPathSegmentPaths) {
114110
return undefined
115111
}
116-
117112
const beforeExists = isObject(beforeJson)
118113
const afterExists = isObject(afterJson)
119114

120115
if (!beforeExists && !afterExists) {
121116
return undefined
122117
}
123118

124-
if (beforeExists && afterExists) {
125-
return getApiCompatibilityKind(beforeJson, afterJson)
119+
if (pathLength === PATH_ITEM_PATH_LENGTH) {
120+
// case remove: when a node disappears, api-diff emits REMOVE diffs for each
121+
// operation. We only mark the deletion as NO_BWC if all removed methods were
122+
// explicitly flagged NO_BWC, keeping deletions consistent with declared scope.
123+
if (beforeExists && !afterExists) {
124+
return getMethodsApiCompatibilityKind(beforeJson)
125+
}
126+
127+
// case add: additions are checked the same way. A new path or operation is
128+
// considered NO_BWC only when every contained method declares NO_BWC.
129+
if (afterExists && !beforeExists) {
130+
return getMethodsApiCompatibilityKind(afterJson)
131+
}
126132
}
127133

128-
// case remove: when a node disappears, api-diff emits REMOVE diffs for each
129-
// operation. We only mark the deletion as NO_BWC if all removed methods were
130-
// explicitly flagged NO_BWC, keeping deletions consistent with declared scope.
131-
if (beforeExists && !afterExists) {
132-
return getMethodsApiCompatibilityKind(beforeJson)
133-
}
134-
135-
// case add: additions are checked the same way. A new path or operation is
136-
// considered NO_BWC only when every contained method declares NO_BWC.
137-
if (afterExists && !beforeExists) {
138-
return getMethodsApiCompatibilityKind(afterJson)
134+
if (pathLength === OPERATION_OBJECT_PATH_LENGTH) {
135+
return getApiCompatibilityKind(beforeJson, afterJson)
139136
}
140137

141138
return undefined
142139
}
143140
}
144141

145-
export const getApiKindFormLabels = (
142+
export const getApiKindFromLabels = (
146143
info?: OpenAPIV3.InfoObject,
147144
fileLabels?: Labels,
148145
versionLabels?: Labels,
@@ -152,6 +149,6 @@ export const getApiKindFormLabels = (
152149
return infoApiKind.toLowerCase()
153150
}
154151

155-
const apiKind = findApiKindLabel(fileLabels, versionLabels)
152+
const apiKind = calculateApiKindFromLabels(fileLabels, versionLabels)
156153
return apiKind?.toLowerCase()
157154
}

src/components/document.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ export const buildDocument = async (parsedFile: SourceFile, file: BuildConfigFil
4848
const apiBuilder = ctx.apiBuilders.find(({ types }) => types.includes(parsedFile.type)) || unknownApiBuilder
4949

5050
try {
51-
file.apiKind = findApiKindLabel(file.labels, ctx.versionLabels)
51+
file.apiKind = calculateApiKindFromLabels(file.labels, ctx.versionLabels)
5252

5353
return await apiBuilder.buildDocument(parsedFile, file, ctx)
5454
} catch (error) {
5555
throw new Error(`Cannot process the "${file.fileId}" document. ${error instanceof Error ? error.message : 'Unknown error'}`)
5656
}
5757
}
5858

59-
export const findApiKindLabel = (fileLabels: unknown, versionLabels: unknown): ApiKind => {
59+
export const calculateApiKindFromLabels = (fileLabels: unknown, versionLabels: unknown): ApiKind => {
6060
if (!Array.isArray(fileLabels) && !Array.isArray(versionLabels)) {
6161
return API_KIND.BWC
6262
}

0 commit comments

Comments
 (0)