Skip to content

Commit 36321f2

Browse files
authored
chore: merge pull request #9 from Netcracker/feature/path-param-name-change-annotation
* feat: classify path parameter name change as annotation * feat: classify path change as annotation if only path parameter names changed
2 parents 6500d01 + 25b16b3 commit 36321f2

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

src/openapi/openapi3.classify.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { getKeyValue, isExist, isNotEmptyArray } from '../utils'
1414
import { emptySecurity, getDefaultStyle, includeSecurity } from './openapi3.utils'
1515
import type { ClassifyRule, CompareContext } from '../types'
1616
import { DiffType } from '../types'
17+
import { hidePathParamNames } from './openapi3.mapping'
1718

1819
export const paramClassifyRule: ClassifyRule = [
1920
({ after }) => {
@@ -72,7 +73,7 @@ export const parameterAllowReservedClassifyRule: ClassifyRule = [
7273
export const parameterNameClassifyRule: ClassifyRule = [
7374
nonBreaking,
7475
breaking,
75-
({ before }) => (getKeyValue(before.parent, 'in') === 'path' ? nonBreaking : breaking),
76+
({ before }) => (getKeyValue(before.parent, 'in') === 'path' ? annotation : breaking),
7677
]
7778

7879
export const parameterRequiredClassifyRule: ClassifyRule = [
@@ -141,3 +142,17 @@ export const operationSecurityItemClassifyRule: ClassifyRule = [
141142
after,
142143
}) => (includeSecurity(after.parent, before.parent) || emptySecurity(after.value) ? nonBreaking : breaking),
143144
]
145+
146+
export const pathChangeClassifyRule: ClassifyRule = [
147+
nonBreaking,
148+
breaking,
149+
({ before, after }) => {
150+
const beforePath = before.key as string
151+
const afterPath = after.key as string
152+
const unifiedBeforePath = hidePathParamNames(beforePath)
153+
const unifiedAfterPath = hidePathParamNames(afterPath)
154+
155+
// If unified paths are the same, it means only parameter names changed
156+
return unifiedBeforePath === unifiedAfterPath ? annotation : breaking
157+
}
158+
]

src/openapi/openapi3.mapping.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const contentMediaTypeMappingResolver: MappingResolver<string> = (before,
124124
return result
125125
}
126126

127-
function hidePathParamNames(path: string): string {
127+
export function hidePathParamNames(path: string): string {
128128
return path.replace(PATH_PARAMETER_REGEXP, PATH_PARAM_UNIFIED_PLACEHOLDER)
129129
}
130130

src/openapi/openapi3.rules.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
parameterNameClassifyRule,
5252
parameterRequiredClassifyRule,
5353
parameterStyleClassifyRule,
54+
pathChangeClassifyRule,
5455
} from './openapi3.classify'
5556
import {
5657
contentMediaTypeMappingResolver,
@@ -341,7 +342,7 @@ export const openApi3Rules = (options: OpenApi3RulesOptions): CompareRules => {
341342
$: allUnclassified,
342343
mapping: options.mode === COMPARE_MODE_OPERATION ? singleOperationPathMappingResolver : pathMappingResolver,
343344
'/*': {
344-
$: [nonBreaking, breaking, breaking],
345+
$: pathChangeClassifyRule,
345346
mapping: options.mode === COMPARE_MODE_OPERATION ? singleOperationPathMappingResolver : pathMappingResolver,
346347
'/summary': { $: allAnnotation },
347348
'/description': { $: allAnnotation },

test/openapi.diff.test.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
DIFF_META_KEY,
88
DiffAction,
99
nonBreaking,
10+
annotation,
1011
} from '../src'
1112

1213
import { OpenapiBuilder, TEST_DIFF_FLAG, TEST_ORIGINS_FLAG, TEST_SYNTHETIC_TITLE_FLAG } from './helper'
@@ -186,20 +187,19 @@ describe('Openapi3 operation changes', () => {
186187
action: DiffAction.rename,
187188
beforeDeclarationPaths: [['paths', '/path1/{param1}/{anotherParam1}']],
188189
afterDeclarationPaths: [['paths', '/path1/{param2}/{anotherParam2}']],
189-
// todo fix
190-
type: breaking,
190+
type: annotation, // Only parameter names changed, unified paths are the same
191191
}),
192192
expect.objectContaining({
193193
action: DiffAction.replace,
194194
beforeDeclarationPaths: [['paths', '/path1/{param1}/{anotherParam1}', 'parameters', 0, 'name']],
195195
afterDeclarationPaths: [['paths', '/path1/{param2}/{anotherParam2}', 'parameters', 1, 'name']],
196-
type: nonBreaking,
196+
type: annotation,
197197
}),
198198
expect.objectContaining({
199199
action: DiffAction.replace,
200200
beforeDeclarationPaths: [['paths', '/path1/{param1}/{anotherParam1}', 'parameters', 1, 'name']],
201201
afterDeclarationPaths: [['paths', '/path1/{param2}/{anotherParam2}', 'parameters', 0, 'name']],
202-
type: nonBreaking,
202+
type: annotation,
203203
}),
204204
]))
205205
})
@@ -211,20 +211,19 @@ describe('Openapi3 operation changes', () => {
211211
action: DiffAction.rename,
212212
beforeDeclarationPaths: [['paths', '/path1/{param1}/{anotherParam1}']],
213213
afterDeclarationPaths: [['paths', '/path1/{param2}/{anotherParam2}']],
214-
// todo fix
215-
type: breaking,
214+
type: annotation, // Only parameter names changed, unified paths are the same
216215
}),
217216
expect.objectContaining({
218217
action: DiffAction.replace,
219218
beforeDeclarationPaths: [['paths', '/path1/{param1}/{anotherParam1}', 'get', 'parameters', 0, 'name']],
220219
afterDeclarationPaths: [['paths', '/path1/{param2}/{anotherParam2}', 'get', 'parameters', 1, 'name']],
221-
type: nonBreaking,
220+
type: annotation,
222221
}),
223222
expect.objectContaining({
224223
action: DiffAction.replace,
225224
beforeDeclarationPaths: [['paths', '/path1/{param1}/{anotherParam1}', 'get', 'parameters', 1, 'name']],
226225
afterDeclarationPaths: [['paths', '/path1/{param2}/{anotherParam2}', 'get', 'parameters', 0, 'name']],
227-
type: nonBreaking,
226+
type: annotation,
228227
}),
229228
]))
230229
})
@@ -253,14 +252,13 @@ describe('Openapi3 operation changes', () => {
253252
action: DiffAction.rename,
254253
beforeDeclarationPaths: [['paths', '/path1/{param1}']],
255254
afterDeclarationPaths: [['paths', '/path1/{param2}']],
256-
// todo fix
257-
type: breaking,
255+
type: annotation, // Only parameter name changed, unified paths are the same
258256
}),
259257
expect.objectContaining({
260258
action: DiffAction.replace,
261259
beforeDeclarationPaths: [['paths', '/path1/{param1}', 'get', 'parameters', 0, 'name']],
262260
afterDeclarationPaths: [['paths', '/path1/{param2}', 'get', 'parameters', 0, 'name']],
263-
type: nonBreaking,
261+
type: annotation,
264262
}),
265263
expect.objectContaining({
266264
action: DiffAction.add,

0 commit comments

Comments
 (0)