Skip to content

Commit 29cb21f

Browse files
committed
fix: prefix groups changelog calculation when prefix is specified in root servers[0].url
1 parent 26cc504 commit 29cb21f

File tree

10 files changed

+284
-4
lines changed

10 files changed

+284
-4
lines changed

src/apitypes/rest/rest.changes.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ export const compareDocuments = async (
148148
const missingOperations = prevNormalizedOperationId === currNormalizedOperationId ? `the ${prevNormalizedOperationId} operation` : `the ${prevNormalizedOperationId} and ${currNormalizedOperationId} operations`
149149
throw new Error(`Can't find ${missingOperations} from documents pair ${prevDoc?.fileId} and ${currDoc?.fileId}`)
150150
}
151-
const operationChanged = Boolean(current && previous)
152-
const operationAddedOrRemoved = !operationChanged
151+
const operationPotentiallyChanged = Boolean(current && previous)
152+
const operationAddedOrRemoved = !operationPotentiallyChanged
153153

154154
let operationDiffs: Diff[] = []
155-
if (operationChanged && collectOnlyChangedOperations) {
155+
if (operationPotentiallyChanged && collectOnlyChangedOperations) {
156156
operationDiffs = [
157157
...(methodData as WithAggregatedDiffs<OpenAPIV3.OperationObject>)[DIFFS_AGGREGATED_META_KEY],
158158
...extractRootServersDiffs(merged),
@@ -268,7 +268,9 @@ export function createCopyWithEmptyPathItems(template: RestOperationData): RestO
268268

269269
return {
270270
paths: {
271-
...Object.fromEntries(Object.keys(paths).map(key => [key, {}])),
271+
...Object.fromEntries(
272+
Object.keys(paths).map(key => [key, {}]),
273+
),
272274
},
273275
...rest,
274276
}
@@ -278,6 +280,12 @@ export function createCopyWithCurrentGroupOperationsOnly(template: RestOperation
278280
const { paths, ...rest } = template
279281
const groupWithoutEdgeSlashes = trimSlashes(group)
280282

283+
if (trimSlashes(extractOperationBasePath(template.servers)) === groupWithoutEdgeSlashes) {
284+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
285+
const { servers, ...rest } = template
286+
return { ...rest }
287+
}
288+
281289
return {
282290
paths: {
283291
...Object.fromEntries(

test/prefix-groups.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,80 @@ describe('Prefix Groups test', () => {
9595
}))
9696
})
9797

98+
test('should compare prefix groups /api/{group} when prefix specified in server', async () => {
99+
const result = await buildPrefixGroupChangelogPackage({
100+
packageId: 'prefix-groups/mixed-cases-with-bulk-prefix-increment',
101+
config: { files: [{ fileId: 'spec1.yaml' }, { fileId: 'spec2.yaml' }] },
102+
})
103+
104+
expect(result).toEqual(changesSummaryMatcher({
105+
[BREAKING_CHANGE_TYPE]: 1,
106+
[NON_BREAKING_CHANGE_TYPE]: 1,
107+
[ANNOTATION_CHANGE_TYPE]: 1,
108+
}))
109+
expect(result).toEqual(numberOfImpactedOperationsMatcher({
110+
[BREAKING_CHANGE_TYPE]: 1,
111+
[NON_BREAKING_CHANGE_TYPE]: 1,
112+
[ANNOTATION_CHANGE_TYPE]: 1,
113+
}))
114+
})
115+
116+
test('should compare prefix groups /api/{group} when prefix is moved from server to path', async () => {
117+
const result = await buildPrefixGroupChangelogPackage({
118+
packageId: 'prefix-groups/mixed-cases-with-prefix-moved-from-server-to-path',
119+
config: { files: [{ fileId: 'spec1.yaml' }, { fileId: 'spec2.yaml' }] },
120+
})
121+
122+
expect(result).toEqual(changesSummaryMatcher({
123+
[BREAKING_CHANGE_TYPE]: 1,
124+
[NON_BREAKING_CHANGE_TYPE]: 1,
125+
[ANNOTATION_CHANGE_TYPE]: 1,
126+
}))
127+
expect(result).toEqual(numberOfImpactedOperationsMatcher({
128+
[BREAKING_CHANGE_TYPE]: 1,
129+
[NON_BREAKING_CHANGE_TYPE]: 1,
130+
[ANNOTATION_CHANGE_TYPE]: 1,
131+
}))
132+
})
133+
134+
// todo: case that we don't support due to shifting to the new changelog calculation approach which involves comparison of the entire docs instead of the operation vs operation comparison
135+
test.skip('should compare prefix groups /api/{group} when prefix is overridden in method', async () => {
136+
const result = await buildPrefixGroupChangelogPackage({
137+
packageId: 'prefix-groups/mixed-cases-with-method-prefix-override',
138+
config: { files: [{ fileId: 'spec1.yaml' }, { fileId: 'spec2.yaml' }] },
139+
})
140+
141+
expect(result).toEqual(changesSummaryMatcher({
142+
[BREAKING_CHANGE_TYPE]: 1,
143+
[NON_BREAKING_CHANGE_TYPE]: 1,
144+
[ANNOTATION_CHANGE_TYPE]: 1,// todo
145+
}))
146+
expect(result).toEqual(numberOfImpactedOperationsMatcher({
147+
[BREAKING_CHANGE_TYPE]: 1,
148+
[NON_BREAKING_CHANGE_TYPE]: 1,
149+
[ANNOTATION_CHANGE_TYPE]: 1,// todo
150+
}))
151+
})
152+
153+
// todo: case that we don't support due to shifting to the new changelog calculation approach which involves comparison of the entire docs instead of the operation vs operation comparison
154+
test.skip('should compare prefix groups /api/{group} when prefix is overridden in path', async () => {
155+
const result = await buildPrefixGroupChangelogPackage({
156+
packageId: 'prefix-groups/mixed-cases-with-path-prefix-override',
157+
config: { files: [{ fileId: 'spec1.yaml' }, { fileId: 'spec2.yaml' }] },
158+
})
159+
160+
expect(result).toEqual(changesSummaryMatcher({
161+
[BREAKING_CHANGE_TYPE]: 1,
162+
[NON_BREAKING_CHANGE_TYPE]: 1,
163+
[ANNOTATION_CHANGE_TYPE]: 1,// todo
164+
}))
165+
expect(result).toEqual(numberOfImpactedOperationsMatcher({
166+
[BREAKING_CHANGE_TYPE]: 1,
167+
[NON_BREAKING_CHANGE_TYPE]: 1,
168+
[ANNOTATION_CHANGE_TYPE]: 1,// todo
169+
}))
170+
})
171+
98172
test('Add method in a new version', async () => {
99173
const result = await buildPrefixGroupChangelogPackage({ packageId: 'prefix-groups/add-method' })
100174

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: 3.0.3
2+
info:
3+
title: test
4+
version: 0.1.0
5+
servers:
6+
- url: https://example1.com/api/v1
7+
paths:
8+
/removed:
9+
get:
10+
responses:
11+
'200':
12+
description: OK
13+
/changed1:
14+
get:
15+
responses:
16+
'200':
17+
description: a1
18+
/untouched:
19+
get:
20+
responses:
21+
'200':
22+
description: OK
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: 3.0.3
2+
info:
3+
title: test
4+
version: 0.1.0
5+
servers:
6+
- url: https://example1.com/api/v2
7+
paths:
8+
/added:
9+
get:
10+
responses:
11+
'200':
12+
description: OK
13+
/changed1:
14+
get:
15+
responses:
16+
'200':
17+
description: a2
18+
/untouched:
19+
get:
20+
responses:
21+
'200':
22+
description: OK
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: 3.0.3
2+
info:
3+
title: test
4+
version: 0.1.0
5+
servers:
6+
- url: https://example1.com/api/v4
7+
paths:
8+
/added:
9+
get:
10+
responses:
11+
'200':
12+
description: OK
13+
servers:
14+
- url: https://example1.com/api/v1
15+
/changed1:
16+
get:
17+
responses:
18+
'200':
19+
description: a2
20+
servers:
21+
- url: https://example1.com/api/v1
22+
/untouched:
23+
get:
24+
responses:
25+
'200':
26+
description: OK
27+
servers:
28+
- url: https://example1.com/api/v1
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: 3.0.3
2+
info:
3+
title: test
4+
version: 0.1.0
5+
servers:
6+
- url: https://example1.com/api/v3
7+
paths:
8+
/removed:
9+
get:
10+
responses:
11+
'200':
12+
description: OK
13+
servers:
14+
- url: https://example1.com/api/v2
15+
/changed1:
16+
get:
17+
responses:
18+
'200':
19+
description: a1
20+
servers:
21+
- url: https://example1.com/api/v2
22+
/untouched:
23+
get:
24+
responses:
25+
'200':
26+
description: OK
27+
servers:
28+
- url: https://example1.com/api/v2
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: 3.0.3
2+
info:
3+
title: test
4+
version: 0.1.0
5+
servers:
6+
- url: https://example1.com/api/v4
7+
paths:
8+
/added:
9+
get:
10+
responses:
11+
'200':
12+
description: OK
13+
servers:
14+
- url: https://example1.com/api/v1
15+
/changed1:
16+
get:
17+
responses:
18+
'200':
19+
description: a2
20+
servers:
21+
- url: https://example1.com/api/v1
22+
/untouched:
23+
get:
24+
responses:
25+
'200':
26+
description: OK
27+
servers:
28+
- url: https://example1.com/api/v1
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: 3.0.3
2+
info:
3+
title: test
4+
version: 0.1.0
5+
servers:
6+
- url: https://example1.com/api/v3
7+
paths:
8+
/removed:
9+
get:
10+
responses:
11+
'200':
12+
description: OK
13+
servers:
14+
- url: https://example1.com/api/v2
15+
/changed1:
16+
get:
17+
responses:
18+
'200':
19+
description: a1
20+
servers:
21+
- url: https://example1.com/api/v2
22+
/untouched:
23+
get:
24+
responses:
25+
'200':
26+
description: OK
27+
servers:
28+
- url: https://example1.com/api/v2
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: 3.0.3
2+
info:
3+
title: test
4+
version: 0.1.0
5+
servers:
6+
- url: https://example1.com/api/v1
7+
paths:
8+
/removed:
9+
get:
10+
responses:
11+
'200':
12+
description: OK
13+
/changed1:
14+
get:
15+
responses:
16+
'200':
17+
description: a1
18+
/untouched:
19+
get:
20+
responses:
21+
'200':
22+
description: OK
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
openapi: 3.0.3
2+
info:
3+
title: test
4+
version: 0.1.0
5+
paths:
6+
/api/v2/added:
7+
get:
8+
responses:
9+
'200':
10+
description: OK
11+
/api/v2/changed1:
12+
get:
13+
responses:
14+
'200':
15+
description: a2
16+
/api/v2/untouched:
17+
get:
18+
responses:
19+
'200':
20+
description: OK

0 commit comments

Comments
 (0)