@@ -28,6 +28,8 @@ import { REST_API_TYPE } from '../apitypes'
2828import  { 
2929  EXPORT_FORMAT_TO_FILE_FORMAT , 
3030  fromBase64 , 
31+   getParentValueByRef , 
32+   resolveRefAndMap , 
3133  removeFirstSlash , 
3234  slugify , 
3335  takeIfDefined , 
@@ -37,9 +39,8 @@ import { OpenAPIV3 } from 'openapi-types'
3739import  {  getOperationBasePath  }  from  '../apitypes/rest/rest.utils' 
3840import  {  VersionRestDocument  }  from  '../apitypes/rest/rest.types' 
3941import  {  FILE_FORMAT_JSON ,  INLINE_REFS_FLAG ,  NORMALIZE_OPTIONS  }  from  '../consts' 
40- import  {  normalize ,   parseRef  }  from  '@netcracker/qubership-apihub-api-unifier' 
42+ import  {  normalize  }  from  '@netcracker/qubership-apihub-api-unifier' 
4143import  {  calculateSpecRefs ,  extractCommonPathItemProperties  }  from  '../apitypes/rest/rest.operation' 
42- import  {  getValueByPath  }  from  '../utils/path' 
4344
4445function  getTransformedDocument ( document : ResolvedGroupDocument ,  format : FileFormat ,  packages : ResolvedReferenceMap ) : VersionRestDocument  { 
4546  const  versionDocument  =  toVersionDocument ( document ,  format ) 
@@ -97,7 +98,7 @@ export class DocumentGroupStrategy implements BuilderStrategy {
9798  } 
9899} 
99100
100- function  parseBase64String ( value : string ) : object  { 
101+ function  parseBase64String ( value : string ) : unknown  { 
101102  return  JSON . parse ( fromBase64 ( value ) ) 
102103} 
103104
@@ -112,7 +113,6 @@ function extractDocumentData(versionDocument: VersionDocument): OpenAPIV3.Docume
112113function  transformDocumentData ( versionDocument : VersionDocument ) : OpenAPIV3 . Document  { 
113114  const  sourceDocument  =  extractDocumentData ( versionDocument ) 
114115  const  normalizedDocument  =  normalizeOpenApi ( sourceDocument ) 
115- 
116116  const  {  paths : sourcePaths ,  components : sourceComponents ,  ...restOfSource  }  =  sourceDocument 
117117  const  {  paths : normalizedPaths  }  =  normalizedDocument 
118118
@@ -121,7 +121,8 @@ function transformDocumentData(versionDocument: VersionDocument): OpenAPIV3.Docu
121121    paths : { } , 
122122  } 
123123
124-   for  ( const  path  of  Object . keys ( normalizedPaths ) )  { 
124+   const  normalizedPathKeys  =  Object . keys ( normalizedPaths ) 
125+   for  ( const  path  of  normalizedPathKeys )  { 
125126    const  sourcePathItem  =  sourcePaths [ path ] 
126127    const  normalizedPathItem  =  normalizedPaths [ path ] 
127128
@@ -130,39 +131,53 @@ function transformDocumentData(versionDocument: VersionDocument): OpenAPIV3.Docu
130131    } 
131132
132133    const  commonPathProps  =  extractCommonPathItemProperties ( sourcePathItem ) 
133-     const  pathItemRef  =  sourcePathItem ?. $ref 
134134
135-     for  ( const  method  of  Object . keys ( normalizedPathItem ) )  { 
135+     const  methodKeys  =  Object . keys ( normalizedPathItem ) 
136+     for  ( const  method  of  methodKeys )  { 
136137      const  inferredMethod  =  method  as  OpenAPIV3 . HttpMethods 
138+ 
139+       // check if field is a valid openapi http method defined in OpenAPIV3.HttpMethods 
137140      if  ( ! isValidHttpMethod ( inferredMethod ) )  { 
138141        continue 
139142      } 
140143
141144      const  methodData  =  normalizedPathItem [ inferredMethod ] 
142-       const  basePath  =  getOperationBasePath ( methodData ?. servers  ||  sourcePathItem ?. servers  ||  sourcePathItem ?. servers   ||   [ ] ) 
145+       const  basePath  =  getOperationBasePath ( methodData ?. servers  ||  sourcePathItem ?. servers  ||  [ ] ) 
143146      const  operationPath  =  basePath  +  path 
144- 
145147      const  operationId  =  slugify ( `${ removeFirstSlash ( operationPath ) } ${ method }  ) 
146148
147149      if  ( ! versionDocument . operationIds . includes ( operationId ) )  { 
148150        continue 
149151      } 
150152
151-       const  updatedPathItem  =  buildPath ( 
152-         sourceDocument , 
153-         path , 
154-         inferredMethod , 
155-         commonPathProps , 
156-         pathItemRef , 
157-       ) 
158- 
159-       resultDocument . paths [ path ]  =  { 
160-         ...( resultDocument . paths [ path ]  ||  { } ) , 
161-         ...updatedPathItem , 
162-       } 
163-       resultDocument . components  =  { 
164-         ...takeIfDefined ( {  securitySchemes : sourceComponents ?. securitySchemes  } ) , 
153+       const  pathItemRef  =  sourcePathItem ?. $ref 
154+       const  pathData  =  sourceDocument . paths [ path ] ! 
155+       if  ( pathItemRef )  { 
156+         const  targetFromResultDocument  =  getParentValueByRef ( resultDocument ,  pathData . $ref  ??  '' ) 
157+         const  target  =  resolveRefAndMap ( sourceDocument ,  pathData . $ref  ??  '' ,  ( value )  =>  ( { 
158+           ...targetFromResultDocument , 
159+           ...extractCommonPathItemProperties ( value ) , 
160+           [ method ] : {  ...value [ method ]  } , 
161+         } ) ) 
162+ 
163+         resultDocument . paths [ path ]  =  pathData 
164+ 
165+         resultDocument . components  =  { 
166+           ...takeIfDefined ( {  securitySchemes : sourceComponents ?. securitySchemes  } ) , 
167+           ...target . components , 
168+         } 
169+       }  else  { 
170+         const  existingPath  =  resultDocument . paths [ path ] 
171+         resultDocument . paths [ path ]  =  { 
172+           ...existingPath , 
173+           ...commonPathProps , 
174+           [ inferredMethod ] : {  ...pathData [ inferredMethod ]  } , 
175+         } 
176+         resultDocument . components  =  { 
177+           ...takeIfDefined ( {  securitySchemes : sourceComponents ?. securitySchemes  } ) , 
178+         } 
165179      } 
180+ 
166181    } 
167182  } 
168183
@@ -187,29 +202,3 @@ function isValidHttpMethod(method: string): method is OpenAPIV3.HttpMethods {
187202function  isNonNullObject ( value : unknown ) : value  is Record < string ,  unknown >  { 
188203  return  typeof  value  ===  'object'  &&  value  !==  null 
189204} 
190- 
191- function  buildPath ( 
192-   sourceDocument : OpenAPIV3 . Document , 
193-   path : string , 
194-   method : OpenAPIV3 . HttpMethods , 
195-   commonPathProps : Partial < OpenAPIV3 . PathItemObject > , 
196-   pathItemRef ?: string , 
197- ) : OpenAPIV3 . PathItemObject  { 
198-   if  ( ! pathItemRef )  { 
199-     const  originalPathItem  =  sourceDocument . paths [ path ] ! 
200-     return  { 
201-       ...commonPathProps , 
202-       [ method ] : {  ...originalPathItem [ method ]  } , 
203-     }  as  OpenAPIV3 . PathItemObject 
204-   } 
205- 
206-   const  {  jsonPath }  =  parseRef ( pathItemRef ) 
207-   const  targetPathItem  =  getValueByPath ( sourceDocument ,  jsonPath )  as  OpenAPIV3 . PathItemObject 
208-   if  ( ! targetPathItem )  return  { } 
209- 
210-   const  originalPathItem  =  sourceDocument . paths [ path ] ! 
211-   return  { 
212-     ...( originalPathItem ) , 
213-     ...commonPathProps , 
214-   } 
215- } 
0 commit comments