Skip to content

Commit 5cec871

Browse files
committed
feat: path change due to renaming path parameters classified as annotation
1 parent 6500d01 commit 5cec871

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

src/openapi/openapi3.classify.ts

Lines changed: 15 additions & 0 deletions
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 }) => {
@@ -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: 4 additions & 6 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,8 +187,7 @@ 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,
@@ -211,8 +211,7 @@ 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,
@@ -253,8 +252,7 @@ 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,

0 commit comments

Comments
 (0)