Skip to content

Commit 050f7ae

Browse files
committed
feat: Added work with '$ref' in rest operation
1 parent 1f9b49e commit 050f7ae

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/apitypes/rest/rest.operation.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { JsonPath, syncCrawl } from '@netcracker/qubership-apihub-json-crawl'
18-
import { OpenAPIV3 } from 'openapi-types'
18+
import { OpenAPIV3 } from 'openapi-types'
1919
import { REST_API_TYPE, REST_KIND_KEY } from './rest.consts'
2020
import { operationRules } from './rest.rules'
2121
import type * as TYPE from './rest.types'
@@ -257,22 +257,57 @@ const createSingleOperationSpec = (
257257
): TYPE.RestOperationData => {
258258
const pathData = document.paths[path] as OpenAPIV3.PathItemObject
259259

260-
return {
260+
const resolveRefPathItem = (ref: string): OpenAPIV3.PathItemObject => {
261+
const { jsonPath } = parseRef(ref)
262+
const target = getValueByPath(document, jsonPath) as OpenAPIV3.PathItemObject
263+
return {
264+
...extractCommonPathItemProperties(target),
265+
[method]: { ...target[method] },
266+
}
267+
}
268+
269+
const buildComponentsFromRef = (ref: string): any => {
270+
const { jsonPath } = parseRef(ref)
271+
const resolved = resolveRefPathItem(ref)
272+
const container: any = {}
273+
setValueByPath(container, jsonPath, resolved)
274+
return container.components
275+
}
276+
277+
const specBase = {
261278
openapi: openapi ?? '3.0.0',
262279
...takeIfDefined({ servers }),
263280
...takeIfDefined({ security }), // TODO: remove duplicates in security
281+
components: {
282+
...takeIfDefined({ securitySchemes }),
283+
},
284+
}
285+
286+
if ('$ref' in pathData) {
287+
return {
288+
...specBase,
289+
paths: {
290+
[path]: { ...pathData },
291+
},
292+
components: {
293+
...specBase.components,
294+
...buildComponentsFromRef(pathData.$ref ?? ''),
295+
},
296+
}
297+
}
298+
299+
return {
300+
...specBase,
264301
paths: {
265302
[path]: {
266303
...extractCommonPathItemProperties(pathData),
267304
[method]: { ...pathData[method] },
268305
},
269306
},
270-
components: {
271-
...takeIfDefined({ securitySchemes }),
272-
},
273307
}
274308
}
275309

310+
const getValueByPath = (value: any, path: JsonPath): any => path.reduce((data, key) => data[key], value)
276311
export const extractCommonPathItemProperties = (
277312
pathData: OpenAPIV3.PathItemObject,
278313
): Pick<OpenAPIV3.PathItemObject, 'summary' | 'description' | 'servers' | 'parameters'> => ({

src/apitypes/rest/rest.operations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const buildRestOperations: OperationsBuilder<OpenAPIV3.Document> = async
6161
debugCtx,
6262
)
6363

64-
const { paths, servers } = document.data
64+
const { paths, servers } = effectiveDocument
6565

6666
const operations: TYPE.VersionRestOperation[] = []
6767
if (!paths) { return [] }

0 commit comments

Comments
 (0)