Skip to content

Commit 37369ef

Browse files
committed
feat: refactoring
1 parent df04f59 commit 37369ef

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/apitypes/rest/rest.operation.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,21 +258,28 @@ const createSingleOperationSpec = (
258258
): TYPE.RestOperationData => {
259259
const pathData = document.paths[path] as OpenAPIV3.PathItemObject
260260

261-
const resolveRefPathItem = (ref: string): OpenAPIV3.PathItemObject => {
261+
const resolveRefPathItem = (ref: string): OpenAPIV3.PathItemObject | null => {
262+
if (!ref) {
263+
return null
264+
}
262265
const { jsonPath } = parseRef(ref)
263266
const target = getValueByPath(document, jsonPath) as OpenAPIV3.PathItemObject
267+
if (!target || typeof target !== 'object') {
268+
return null
269+
}
264270
return {
265271
...extractCommonPathItemProperties(target),
266272
[method]: { ...target[method] },
267273
}
268274
}
269275

270-
const buildComponentsFromRef = (ref: string): any => {
271-
const { jsonPath } = parseRef(ref)
276+
const buildComponentsFromRef = (ref: string): OpenAPIV3.ComponentsObject => {
272277
const resolved = resolveRefPathItem(ref)
278+
if (!resolved) {return {}}
279+
const { jsonPath } = parseRef(ref)
273280
const container: any = {}
274281
setValueByPath(container, jsonPath, resolved)
275-
return container.components
282+
return container.components || {}
276283
}
277284

278285
const specBase = {
@@ -284,7 +291,7 @@ const createSingleOperationSpec = (
284291
},
285292
}
286293

287-
if ('$ref' in pathData) {
294+
if (pathData && '$ref' in pathData && pathData.$ref) {
288295
return {
289296
...specBase,
290297
paths: {

src/strategies/document-group.strategy.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function transformDocumentData(versionDocument: VersionDocument): OpenAPIV3.Docu
131131
}
132132

133133
const commonPathProps = extractCommonPathItemProperties(sourcePathItem)
134-
const pathItemRef = '$ref' in sourcePathItem ? sourcePathItem.$ref : undefined
134+
const pathItemRef = sourcePathItem?.$ref
135135

136136
for (const method of Object.keys(normalizedPathItem)) {
137137
const inferredMethod = method as OpenAPIV3.HttpMethods
@@ -209,6 +209,8 @@ function buildPathAndComponents(
209209

210210
const { jsonPath } = parseRef(pathItemRef)
211211
const targetPathItem = getValueByPath(sourceDocument, jsonPath) as OpenAPIV3.PathItemObject
212+
if (!targetPathItem) return { updatedPathItem: {} }
213+
212214
const resolvedPathItem = {
213215
...extractCommonPathItemProperties(targetPathItem),
214216
[method]: { ...targetPathItem[method] },

0 commit comments

Comments
 (0)