Skip to content

Commit 1caef30

Browse files
committed
feat: Refactoring
1 parent e3cbfa9 commit 1caef30

File tree

24 files changed

+159
-8
lines changed

24 files changed

+159
-8
lines changed

src/apitypes/rest/rest.operation.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
grepValue,
4949
JSON_SCHEMA_PROPERTY_DEPRECATED,
5050
matchPaths,
51+
normalize,
5152
OPEN_API_PROPERTY_COMPONENTS,
5253
OPEN_API_PROPERTY_PATHS,
5354
OPEN_API_PROPERTY_SCHEMAS,
@@ -59,7 +60,6 @@ import {
5960
} from '@netcracker/qubership-apihub-api-unifier'
6061
import { calculateObjectHash } from '../../utils/hashes'
6162
import { calculateTolerantHash } from '../../components/deprecated'
62-
import { getValueByPath } from '../../utils/path'
6363

6464
export const buildRestOperation = (
6565
operationId: string,
@@ -262,8 +262,7 @@ const createSingleOperationSpec = (
262262
if (!ref) {
263263
return null
264264
}
265-
const { jsonPath } = parseRef(ref)
266-
const target = getValueByPath(document, jsonPath) as OpenAPIV3.PathItemObject
265+
const target = normalize(pathData, { source: document }) as OpenAPIV3.PathItemObject
267266
if (!target || typeof target !== 'object') {
268267
return null
269268
}

test/document-group.test.ts

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ const groupToOperationIdsMap2 = {
3232
'some-path2-post',
3333
],
3434
}
35+
36+
const groupToOnePathOperationIdsMap = {
37+
[GROUP_NAME]: [
38+
'path1-get',
39+
'path1-post',
40+
],
41+
}
3542
const EXPECTED_RESULT_FILE = 'result.yaml'
3643

3744
describe('Document Group test', () => {
@@ -44,9 +51,23 @@ describe('Document Group test', () => {
4451
})
4552

4653
test('should have properly merged documents', async () => {
47-
await runMergeOperationsCase('case1')
54+
await runMergeOperationsCase('basic-documents-for-merge')
4855
})
4956

57+
test('should have keep a few operations in one path', async () => {
58+
const pkg = LocalRegistry.openPackage('document-group/few-operations-in-one-path', groupToOnePathOperationIdsMap)
59+
const editor = await Editor.openProject(pkg.packageId, pkg)
60+
await pkg.publish(pkg.packageId, { packageId: pkg.packageId })
61+
62+
const result = await editor.run({
63+
packageId: pkg.packageId,
64+
groupName: GROUP_NAME,
65+
buildType: BUILD_TYPE.REDUCED_SOURCE_SPECIFICATIONS,
66+
})
67+
for (const document of Array.from(result.documents.values())) {
68+
expect(Object.keys(document.data.paths['/path1']).length).toEqual(document.operationIds.length)
69+
}
70+
})
5071

5172
test('should rename documents with matching names', async () => {
5273
const dashboard = LocalRegistry.openPackage('documents-collision', groupToOperationIdsMap2)
@@ -83,7 +104,39 @@ describe('Document Group test', () => {
83104

84105
describe('PathItems tests', () => {
85106
test('should have documents with keep pathItems in components', async () => {
86-
const pkg = LocalRegistry.openPackage('document-group/case1', groupToOperationIdsMap)
107+
const pkg = LocalRegistry.openPackage('document-group/operations-in-pathitems', groupToOperationIdsMap)
108+
const editor = await Editor.openProject(pkg.packageId, pkg)
109+
await pkg.publish(pkg.packageId, { packageId: pkg.packageId })
110+
111+
const result = await editor.run({
112+
packageId: pkg.packageId,
113+
groupName: GROUP_NAME,
114+
buildType: BUILD_TYPE.REDUCED_SOURCE_SPECIFICATIONS,
115+
})
116+
117+
for (const document of Array.from(result.documents.values())) {
118+
expect(Object.keys(document.data.components.pathItems).length).toEqual(document.operationIds.length)
119+
}
120+
})
121+
122+
test('should have keep a few operations in one pathItems', async () => {
123+
const pkg = LocalRegistry.openPackage('document-group/few-operations-in-one-pathitems', groupToOnePathOperationIdsMap)
124+
const editor = await Editor.openProject(pkg.packageId, pkg)
125+
await pkg.publish(pkg.packageId, { packageId: pkg.packageId })
126+
127+
const result = await editor.run({
128+
packageId: pkg.packageId,
129+
groupName: GROUP_NAME,
130+
buildType: BUILD_TYPE.REDUCED_SOURCE_SPECIFICATIONS,
131+
})
132+
133+
for (const document of Array.from(result.documents.values())) {
134+
expect(Object.keys(document.data.components.pathItems['pathItem1']).length).toEqual(document.operationIds.length)
135+
}
136+
})
137+
138+
test('should delete pathItems object which is not referenced', async () => {
139+
const pkg = LocalRegistry.openPackage('document-group/pathitems-object-which-is-not-referenced', groupToOperationIdsMap)
87140
const editor = await Editor.openProject(pkg.packageId, pkg)
88141
await pkg.publish(pkg.packageId, { packageId: pkg.packageId })
89142

@@ -99,7 +152,7 @@ describe('Document Group test', () => {
99152
})
100153

101154
test('should have documents stripped of operations other than from provided group', async () => {
102-
const pkg = LocalRegistry.openPackage('document-group/case2', groupToOperationIdsMap)
155+
const pkg = LocalRegistry.openPackage('document-group/stripped-of-pathitems-operations', groupToOperationIdsMap)
103156
const editor = await Editor.openProject(pkg.packageId, pkg)
104157
await pkg.publish(pkg.packageId, { packageId: pkg.packageId })
105158

@@ -115,11 +168,11 @@ describe('Document Group test', () => {
115168

116169
describe('Merge Operations', () => {
117170
test('should have properly merged documents', async () => {
118-
await runMergeOperationsCase('case2')
171+
await runMergeOperationsCase('basic-documents-pathitems-for-merge')
119172
})
120173

121174
test('should have properly merged documents mixed formats (operation + pathItems operation)', async () => {
122-
await runMergeOperationsCase('case3')
175+
await runMergeOperationsCase('documents-pathitems-with-mixed-formats')
123176
})
124177
})
125178
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
openapi: "3.1.0"
2+
info:
3+
title: test
4+
version: 0.1.0
5+
paths:
6+
/path1:
7+
get:
8+
responses:
9+
'200':
10+
description: response description main
11+
post:
12+
responses:
13+
'200':
14+
description: response description main
15+
16+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
openapi: "3.1.0"
2+
info:
3+
title: test
4+
version: 0.1.0
5+
paths:
6+
/path1:
7+
$ref: '#/components/pathItems/pathItem1'
8+
components:
9+
pathItems:
10+
pathItem1:
11+
get:
12+
responses:
13+
'200':
14+
description: response description main
15+
post:
16+
responses:
17+
'200':
18+
description: response description main
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"packageId": "55",
3+
"apiType": "rest",
4+
"version": "v1",
5+
"files": [
6+
{
7+
"fileId": "1.yaml",
8+
"publish": true,
9+
"labels": [],
10+
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a3cd"
11+
}
12+
]
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
openapi: "3.1.0"
2+
info:
3+
title: test
4+
version: 0.1.0
5+
paths:
6+
/path1:
7+
$ref: '#/components/pathItems/pathItem1'
8+
/path2:
9+
$ref: '#/components/pathItems/pathItem2'
10+
components:
11+
pathItems:
12+
pathItem1:
13+
get:
14+
responses:
15+
'200':
16+
description: response description main
17+
pathItem2:
18+
post:
19+
responses:
20+
'200':
21+
description: response description main
22+
pathItem3:
23+
post:
24+
responses:
25+
'200':
26+
description: response description main
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"packageId": "55",
3+
"apiType": "rest",
4+
"version": "v1",
5+
"files": [
6+
{
7+
"fileId": "1.yaml",
8+
"publish": true,
9+
"labels": [],
10+
"commitId": "6c778b1f44200bd19944a6a8eac10a4e5a21a3cd"
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)