Skip to content

Commit 5b1b4e5

Browse files
committed
feat: Review
1 parent c4db313 commit 5b1b4e5

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

src/apitypes/rest/rest.operation.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ import {
3232
import {
3333
buildSearchScope,
3434
capitalize,
35+
copySymbolIfDefined,
3536
getKeyValue,
3637
getSplittedVersionKey,
38+
getSymbolValueIfDefined,
3739
isDeprecatedOperationItem,
3840
isObject,
3941
isOperationDeprecated,
@@ -247,12 +249,12 @@ export const calculateSpecRefs = (
247249
})
248250

249251
if (operations?.length) {
250-
resolveComponentsPathItemOperationSpec(resultSpec, normalizedSpec, operations)
252+
reduceComponentPathItemsToOperations(resultSpec, normalizedSpec, operations)
251253
}
252254
}
253255

254-
export function resolveComponentsPathItemOperationSpec(
255-
sourceDocument: RestOperationData,
256+
export function reduceComponentPathItemsToOperations(
257+
resultSpec: RestOperationData,
256258
normalizedDocument: RestOperationData,
257259
operations: OperationId[],
258260
): void {
@@ -263,16 +265,16 @@ export function resolveComponentsPathItemOperationSpec(
263265
if (!isNonNullObject(sourcePathItem)) {
264266
continue
265267
}
266-
const refs: string[] = hasInlineRefsFlag(sourcePathItem) ? sourcePathItem[INLINE_REFS_FLAG] : []
267-
if (refs.length === 0) {
268+
const refs = getSymbolValueIfDefined(sourcePathItem, INLINE_REFS_FLAG) as string[] | undefined
269+
if (!refs || refs?.length === 0) {
268270
continue
269271
}
270272
const { jsonPath } = parseRef(refs[0])
271273
if (!jsonPath) {
272274
continue
273275
}
274276

275-
const valueByPath = getValueByPath(sourceDocument, jsonPath) as OpenAPIV3.PathItemObject
277+
const valueByPath = getValueByPath(resultSpec, jsonPath) as OpenAPIV3.PathItemObject
276278

277279
const operationIds: OpenAPIV3.HttpMethods[] = (Object.keys(valueByPath) as OpenAPIV3.HttpMethods[])
278280
.filter((httpMethod) => isValidHttpMethod(httpMethod))
@@ -284,7 +286,7 @@ export function resolveComponentsPathItemOperationSpec(
284286
sourcePathItem?.servers ||
285287
[],
286288
)
287-
const operationId = getOperationId(basePath, httpMethod, path)
289+
const operationId = calculateOperationId(basePath, httpMethod, path)
288290
return operations.includes(operationId)
289291
})
290292

@@ -299,7 +301,7 @@ export function resolveComponentsPathItemOperationSpec(
299301
return pathItemObject
300302
}, {}),
301303
}
302-
setValueByPath(sourceDocument, jsonPath, pathItemObject)
304+
setValueByPath(resultSpec, jsonPath, pathItemObject)
303305
}
304306
}
305307
}
@@ -322,10 +324,6 @@ const isOperationPaths = (paths: JsonPath[]): boolean => {
322324
)
323325
}
324326

325-
function hasInlineRefsFlag(obj: unknown): obj is { [INLINE_REFS_FLAG]: string[] } {
326-
return typeof obj === 'object' && obj !== null && INLINE_REFS_FLAG in obj
327-
}
328-
329327
// todo output of this method disrupts document normalization.
330328
// origin symbols are not being transferred to the resulting spec.
331329
// DO NOT pass output of this method to apiDiff
@@ -340,19 +338,18 @@ const createSingleOperationSpec = (
340338
): TYPE.RestOperationData => {
341339
const pathData = document.paths[path] as OpenAPIV3.PathItemObject
342340

343-
const isContainsRef = !!pathData.$ref
344-
const refFlag = hasInlineRefsFlag(pathData) ? pathData[INLINE_REFS_FLAG] : false
341+
const isRefPathData = !!pathData.$ref
345342
return {
346343
openapi: openapi ?? '3.0.0',
347344
...takeIfDefined({ servers }),
348345
...takeIfDefined({ security }), // TODO: remove duplicates in security
349346
paths: {
350-
[path]: isContainsRef
347+
[path]: isRefPathData
351348
? pathData
352349
: {
353350
...extractCommonPathItemProperties(pathData),
354351
[method]: { ...pathData[method] },
355-
...(refFlag ? { [INLINE_REFS_FLAG]: refFlag } : {}),
352+
...copySymbolIfDefined(pathData, INLINE_REFS_FLAG),
356353
},
357354
},
358355
components: {
@@ -374,7 +371,7 @@ function isValidHttpMethod(method: string): method is OpenAPIV3.HttpMethods {
374371
return (Object.values(OpenAPIV3.HttpMethods) as string[]).includes(method)
375372
}
376373

377-
export function getOperationId(
374+
export function calculateOperationId(
378375
basePath: string,
379376
key: string,
380377
path: string,

src/apitypes/rest/rest.operations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { OpenAPIV3 } from 'openapi-types'
1818

19-
import { buildRestOperation, getOperationId } from './rest.operation'
19+
import { buildRestOperation, calculateOperationId } from './rest.operation'
2020
import { OperationIdNormalizer, OperationsBuilder } from '../../types'
2121
import {
2222
createBundlingErrorHandler,
@@ -77,7 +77,7 @@ export const buildRestOperations: OperationsBuilder<OpenAPIV3.Document> = async
7777
const methodData = pathData[key as OpenAPIV3.HttpMethods]
7878
const basePath = getOperationBasePath(methodData?.servers || pathData?.servers || servers || [])
7979

80-
const operationId = getOperationId(basePath, key, path)
80+
const operationId = calculateOperationId(basePath, key, path)
8181

8282
if (ctx.operationResolver(operationId)) {
8383
ctx.notifications.push({

src/strategies/document-group.strategy.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { getOperationBasePath } from '../apitypes/rest/rest.utils'
3838
import { VersionRestDocument } from '../apitypes/rest/rest.types'
3939
import { FILE_FORMAT_JSON, INLINE_REFS_FLAG, NORMALIZE_OPTIONS } from '../consts'
4040
import { normalize } from '@netcracker/qubership-apihub-api-unifier'
41-
import { calculateSpecRefs, extractCommonPathItemProperties } from '../apitypes/rest/rest.operation'
41+
import { calculateSpecRefs, extractCommonPathItemProperties, calculateOperationId } from '../apitypes/rest/rest.operation'
4242

4343
function getTransformedDocument(document: ResolvedGroupDocument, format: FileFormat, packages: ResolvedReferenceMap): VersionRestDocument {
4444
const versionDocument = toVersionDocument(document, format)
@@ -141,17 +141,16 @@ function transformDocumentData(versionDocument: VersionDocument): OpenAPIV3.Docu
141141
[],
142142
)
143143

144-
const operationPath = basePath + path
145-
const operationId = slugify(`${removeFirstSlash(operationPath)}-${method}`)
144+
const operationId = calculateOperationId(basePath, method, path)
146145

147146
if (!versionDocument.operationIds.includes(operationId)) {
148147
continue
149148
}
150149

151150
if (versionDocument.operationIds.includes(operationId)) {
152151
const pathData = sourceDocument.paths[path]!
153-
const isContainsRef = !!pathData.$ref
154-
resultDocument.paths[path] = isContainsRef
152+
const isRefPathData = !!pathData.$ref
153+
resultDocument.paths[path] = isRefPathData
155154
? pathData
156155
: {
157156
...resultDocument.paths[path],

src/utils/builder.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import {
2929
import { API_KIND } from '../consts'
3030
import { Diff, DiffType } from '@netcracker/qubership-apihub-api-diff'
3131
import { JsonPath } from '@netcracker/qubership-apihub-json-crawl'
32-
import { parseRef } from '@netcracker/qubership-apihub-api-unifier'
33-
import { OpenAPIV3 } from 'openapi-types'
3432

3533
export type ObjPath = (string | number)[]
3634

src/utils/objects.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,20 @@ export const isSymbol = (value: unknown): value is symbol => {
6565
export const isObject = (value: unknown): value is Record<string | symbol, unknown> => {
6666
return typeof value === 'object' && value !== null
6767
}
68+
69+
export const getSymbolValueIfDefined = <T extends object>(
70+
obj: T,
71+
symbol: symbol,
72+
): unknown => {
73+
const symbolObj = obj as { [key: symbol]: unknown }
74+
75+
return symbol in symbolObj ? symbolObj[symbol] : undefined
76+
}
77+
78+
export const copySymbolIfDefined = <T extends object>(
79+
obj: T,
80+
symbol: symbol,
81+
): { [key: symbol]: unknown } => {
82+
const value = getSymbolValueIfDefined(obj, symbol)
83+
return value !== undefined ? { [symbol]: value } : {}
84+
}

0 commit comments

Comments
 (0)