|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | import { JsonPath, syncCrawl } from '@netcracker/qubership-apihub-json-crawl' |
18 | | -import { OpenAPIV3 } from 'openapi-types' |
| 18 | +import { OpenAPIV3 } from 'openapi-types' |
19 | 19 | import { REST_API_TYPE, REST_KIND_KEY } from './rest.consts' |
20 | 20 | import { operationRules } from './rest.rules' |
21 | 21 | import type * as TYPE from './rest.types' |
@@ -257,22 +257,57 @@ const createSingleOperationSpec = ( |
257 | 257 | ): TYPE.RestOperationData => { |
258 | 258 | const pathData = document.paths[path] as OpenAPIV3.PathItemObject |
259 | 259 |
|
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 = { |
261 | 278 | openapi: openapi ?? '3.0.0', |
262 | 279 | ...takeIfDefined({ servers }), |
263 | 280 | ...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, |
264 | 301 | paths: { |
265 | 302 | [path]: { |
266 | 303 | ...extractCommonPathItemProperties(pathData), |
267 | 304 | [method]: { ...pathData[method] }, |
268 | 305 | }, |
269 | 306 | }, |
270 | | - components: { |
271 | | - ...takeIfDefined({ securitySchemes }), |
272 | | - }, |
273 | 307 | } |
274 | 308 | } |
275 | 309 |
|
| 310 | +const getValueByPath = (value: any, path: JsonPath): any => path.reduce((data, key) => data[key], value) |
276 | 311 | export const extractCommonPathItemProperties = ( |
277 | 312 | pathData: OpenAPIV3.PathItemObject, |
278 | 313 | ): Pick<OpenAPIV3.PathItemObject, 'summary' | 'description' | 'servers' | 'parameters'> => ({ |
|
0 commit comments