Skip to content

Commit ffdc348

Browse files
committed
feat: Refactoring
1 parent 136d5ce commit ffdc348

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

src/apitypes/rest/rest.operation.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
isOperationDeprecated,
3737
normalizePath,
3838
rawToApiKind,
39-
resolveRefAndMap,
39+
getValueByRefAndUpdate,
4040
setValueByPath,
4141
takeIf,
4242
takeIfDefined,
@@ -311,14 +311,10 @@ const createSingleOperationSpec = (
311311
}
312312

313313
if (pathData.$ref) {
314-
const cleanedDocument = resolveRefAndMap(
315-
document,
316-
pathData.$ref,
317-
(pathItemObject: OpenAPIV3.PathItemObject) => ({
318-
...extractCommonPathItemProperties(pathItemObject),
319-
[method]: { ...pathItemObject[method] },
320-
}),
321-
)
314+
const cleanedDocument = getValueByRefAndUpdate(pathData.$ref, document, (pathItemObject: OpenAPIV3.PathItemObject) => ({
315+
...extractCommonPathItemProperties(pathItemObject),
316+
[method]: { ...pathItemObject[method] },
317+
}))
322318

323319
return {
324320
...baseSpec,

src/strategies/document-group.strategy.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ import {
2929
EXPORT_FORMAT_TO_FILE_FORMAT,
3030
fromBase64,
3131
getParentValueByRef,
32-
resolveRefAndMap,
32+
getValueByRefAndUpdate,
3333
removeFirstSlash,
3434
slugify,
35-
takeIfDefined,
3635
toVersionDocument,
3736
} from '../utils'
3837
import { OpenAPIV3 } from 'openapi-types'
@@ -182,30 +181,27 @@ function transformDocumentData(versionDocument: VersionDocument): OpenAPIV3.Docu
182181
}
183182

184183
function handleRefPathItem(
185-
resultDoc: OpenAPIV3.Document,
186-
sourceDoc: OpenAPIV3.Document,
184+
resultDocument: OpenAPIV3.Document,
185+
sourceDocument: OpenAPIV3.Document,
187186
path: string,
188187
pathData: OpenAPIV3.PathItemObject | OpenAPIV3.ReferenceObject,
189188
method: OpenAPIV3.HttpMethods,
190189
): void {
191-
const targetFromResultDoc = getParentValueByRef(resultDoc, (pathData as any).$ref ?? '')
192-
193-
const target = resolveRefAndMap(
194-
sourceDoc,
195-
(pathData as any).$ref ?? '',
196-
(pathItemObject: OpenAPIV3.PathItemObject) => ({
197-
...targetFromResultDoc,
198-
...extractCommonPathItemProperties(pathItemObject),
199-
[method]: { ...pathItemObject[method] },
200-
}),
201-
)
202-
203-
resultDoc.paths[path] = pathData
204-
205-
if (target.components) {
206-
resultDoc.components = {
207-
...resultDoc.components,
208-
...target.components,
190+
const ref = pathData.$ref ?? ''
191+
const operationsFormResult = getParentValueByRef(ref, resultDocument)
192+
193+
const clearedDocument = getValueByRefAndUpdate(ref, sourceDocument, (pathItemObject: OpenAPIV3.PathItemObject) => ({
194+
...operationsFormResult,
195+
...extractCommonPathItemProperties(pathItemObject),
196+
[method]: { ...pathItemObject[method] },
197+
}))
198+
199+
resultDocument.paths[path] = pathData
200+
201+
if (clearedDocument.components) {
202+
resultDocument.components = {
203+
...resultDocument.components,
204+
...clearedDocument.components,
209205
}
210206
}
211207
}

src/utils/builder.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,18 @@ const getValueByJsonPath = (root: any, path: JsonPath): any => {
193193
return current
194194
}
195195

196-
export const getParentValueByRef = (obj: any, ref: string): any => {
197-
const visited = new Set<string>()
196+
export const getParentValueByRef = (ref: string, document: OpenAPIV3.Document): any => {
197+
const cyclingGuard = new Set<string>()
198198
let currentRef: string | undefined = ref
199199

200200
while (currentRef) {
201-
if (visited.has(currentRef)) {
202-
// circular reference guard
201+
if (cyclingGuard.has(currentRef)) {
203202
return undefined
204203
}
205-
visited.add(currentRef)
204+
cyclingGuard.add(currentRef)
206205

207206
const { jsonPath } = parseRef(currentRef)
208-
const value = getValueByJsonPath(obj, jsonPath)
207+
const value = getValueByJsonPath(document, jsonPath)
209208

210209
if (isRecordObject(value) && typeof value.$ref === 'string') {
211210
currentRef = value.$ref
@@ -217,9 +216,9 @@ export const getParentValueByRef = (obj: any, ref: string): any => {
217216
return undefined
218217
}
219218

220-
export const resolveRefAndMap = (
221-
document: OpenAPIV3.Document,
219+
export const getValueByRefAndUpdate = (
222220
ref: string,
221+
document: OpenAPIV3.Document,
223222
valueMapper: (target: Record<string, unknown>) => Record<string, unknown>,
224223
result: Record<string, unknown> = {},
225224
): Record<string, unknown> => {

0 commit comments

Comments
 (0)