Skip to content

Commit 665fec8

Browse files
committed
feat: Added cleaning PathItemObject in components
1 parent 6203ae9 commit 665fec8

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/apitypes/rest/rest.operation.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -263,32 +263,44 @@ export function createSinglePathItemOperationSpec(
263263
if (!isNonNullObject(sourcePathItem)) {
264264
continue
265265
}
266-
const refs = hasInlineRefsFlag(sourcePathItem) ? sourcePathItem[INLINE_REFS_FLAG] : []
266+
const refs: string[] = hasInlineRefsFlag(sourcePathItem) ? sourcePathItem[INLINE_REFS_FLAG] : []
267267
if (refs.length === 0) {
268268
continue
269269
}
270270
const richReference = parseRef(refs[0])
271271
if (!richReference) {
272272
continue
273273
}
274-
const valueByPath = getValueByPath(sourceDocument, richReference.jsonPath)
275-
for (const method of Object.keys(valueByPath)) {
276-
const httpMethod = method as OpenAPIV3.HttpMethods
277-
if (!isValidHttpMethod(httpMethod)) continue
278-
279-
const methodData = sourcePathItem[httpMethod]
280-
const basePath = getOperationBasePath(
281-
methodData?.servers ||
282-
sourcePathItem?.servers ||
283-
[],
284-
)
285-
286-
const operationPath = basePath + path
287-
const operationId = slugify(`${removeFirstSlash(operationPath)}-${method}`)
288-
289-
if (!operations.includes(operationId)) {
290-
delete valueByPath[method]
274+
const valueByPath = getValueByPath(sourceDocument, richReference.jsonPath) as OpenAPIV3.PathItemObject
275+
276+
const operationIds: OpenAPIV3.HttpMethods[] = (Object.keys(valueByPath) as OpenAPIV3.HttpMethods[])
277+
.filter((httpMethod) => isValidHttpMethod(httpMethod))
278+
.filter(httpMethod => {
279+
const methodData = sourcePathItem[httpMethod as OpenAPIV3.HttpMethods]
280+
if (!methodData) return false
281+
const basePath = getOperationBasePath(
282+
methodData?.servers ||
283+
sourcePathItem?.servers ||
284+
[],
285+
)
286+
287+
const operationPath = basePath + path
288+
const operationId = slugify(`${removeFirstSlash(operationPath)}-${httpMethod}`)
289+
return operations.includes(operationId)
290+
})
291+
292+
if (operationIds?.length) {
293+
const pathItem = {
294+
...extractCommonPathItemProperties(valueByPath),
295+
...operationIds.reduce<OpenAPIV3.PathItemObject>((pathItemObject: OpenAPIV3.PathItemObject, operationId: OpenAPIV3.HttpMethods) => {
296+
const operationData = valueByPath[operationId]
297+
if (operationData) {
298+
pathItemObject[operationId] = { ...operationData }
299+
}
300+
return pathItemObject
301+
}, {}),
291302
}
303+
setValueByPath(sourceDocument, richReference.jsonPath, pathItem)
292304
}
293305
}
294306
}

0 commit comments

Comments
 (0)