Skip to content

Commit 7817a91

Browse files
committed
fix: remove prefix from servers case
1 parent cc7cb9d commit 7817a91

File tree

8 files changed

+62
-20
lines changed

8 files changed

+62
-20
lines changed

src/apitypes/rest/rest.changes.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import {
6060
extractPathParamRenameDiff,
6161
extractRootSecurityDiffs,
6262
extractRootServersDiffs,
63-
getOperationBasePath,
63+
extractOperationBasePath,
6464
} from './rest.utils'
6565
import { createOperationChange, getOperationTags, OperationsMap } from '../../components'
6666

@@ -141,14 +141,15 @@ export const compareDocuments = async (
141141

142142
const methodData = pathData[inferredMethod]
143143
// todo if there were actually servers here, we wouldn't have handle it, add a test
144-
const basePath = getOperationBasePath(methodData?.servers || pathData?.servers || merged.servers || [])
145-
const operationPath = basePath + path
146-
const operationId = slugify(`${operationPath}-${inferredMethod}`)
147-
const normalizedOperationId = slugify(`${normalizePath(operationPath)}-${inferredMethod}`, [], IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER)
144+
const previousBasePath = extractOperationBasePath(methodData?.servers || pathData?.servers || prevDocData.servers || [])
145+
const currentBasePath = extractOperationBasePath(methodData?.servers || pathData?.servers || currDocData.servers || [])
146+
const prevNormalizedOperationId = slugify(`${normalizePath(previousBasePath + path)}-${inferredMethod}`, [], IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER)
147+
const currNormalizedOperationId = slugify(`${normalizePath(currentBasePath + path)}-${inferredMethod}`, [], IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER)
148148

149-
const { current, previous } = operationsMap[normalizedOperationId] ?? operationsMap[operationId] ?? {}
149+
const { current, previous } = operationsMap[prevNormalizedOperationId] ?? operationsMap[currNormalizedOperationId] /*?? operationsMap[operationId]*/ ?? {}
150150
if (!current && !previous) {
151-
throw new Error(`Can't find the operation ${operationId} from documents pair ${prevDoc?.fileId} and ${currDoc?.fileId}`)
151+
const missingOperations = prevNormalizedOperationId === currNormalizedOperationId ? `the ${prevNormalizedOperationId} operation` : `the ${prevNormalizedOperationId} and ${currNormalizedOperationId} operations`
152+
throw new Error(`Can't find ${missingOperations} from documents pair ${prevDoc?.fileId} and ${currDoc?.fileId}`)
152153
}
153154
const operationChanged = Boolean(current && previous)
154155
const operationAddedOrRemoved = !operationChanged

src/apitypes/rest/rest.operations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const buildRestOperations: OperationsBuilder<OpenAPIV3.Document> = async
7676

7777
await asyncFunction(() => {
7878
const methodData = pathData[key as OpenAPIV3.HttpMethods]
79-
const basePath = getOperationBasePath(methodData?.servers || pathData?.servers || servers || [])
79+
const basePath = extractOperationBasePath(methodData?.servers || pathData?.servers || servers || [])
8080
const operationPath = basePath + path
8181

8282
const operationId = slugify(`${removeFirstSlash(operationPath)}-${key}`)

src/apitypes/rest/rest.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import YAML from 'js-yaml'
2929
import { Diff, DIFF_META_KEY } from '@netcracker/qubership-apihub-api-diff'
3030
import { isPathParamRenameDiff } from '../../utils'
3131

32-
export const getOperationBasePath = (servers?: OpenAPIV3.ServerObject[]): string => {
32+
export const extractOperationBasePath = (servers?: OpenAPIV3.ServerObject[]): string => {
3333
if (!Array.isArray(servers) || !servers.length) { return '' }
3434

3535
try {

src/strategies/document-group.strategy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
toVersionDocument,
3535
} from '../utils'
3636
import { OpenAPIV3 } from 'openapi-types'
37-
import { getOperationBasePath } from '../apitypes/rest/rest.utils'
37+
import { extractOperationBasePath } from '../apitypes/rest/rest.utils'
3838
import { VersionRestDocument } from '../apitypes/rest/rest.types'
3939
import { FILE_FORMAT_JSON, INLINE_REFS_FLAG, NORMALIZE_OPTIONS } from '../consts'
4040
import { normalize } from '@netcracker/qubership-apihub-api-unifier'
@@ -140,7 +140,7 @@ function transformDocumentData(versionDocument: VersionDocument): OpenAPIV3.Docu
140140
}
141141

142142
const methodData = pathData[inferredMethod]
143-
const basePath = getOperationBasePath(methodData?.servers || pathData?.servers || documentData?.servers || [])
143+
const basePath = extractOperationBasePath(methodData?.servers || pathData?.servers || documentData?.servers || [])
144144
const operationPath = basePath + path
145145

146146
const operationId = slugify(`${removeFirstSlash(operationPath)}-${method}`)

test/changes.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ describe('Changes test', () => {
106106
expect(result).toEqual(changesSummaryMatcher({ [ANNOTATION_CHANGE_TYPE]: 2 }))
107107
expect(result).toEqual(numberOfImpactedOperationsMatcher({ [ANNOTATION_CHANGE_TYPE]: 1 }))
108108
})
109+
110+
test('Remove prefix from server', async () => {
111+
const result = await buildChangelogPackage('changelog/remove-prefix-from-server')
112+
113+
expect(result).toEqual(changesSummaryMatcher({
114+
[BREAKING_CHANGE_TYPE]: 1,
115+
[NON_BREAKING_CHANGE_TYPE]: 1,
116+
}))
117+
expect(result).toEqual(numberOfImpactedOperationsMatcher({
118+
[BREAKING_CHANGE_TYPE]: 1,
119+
[NON_BREAKING_CHANGE_TYPE]: 1,
120+
}))
121+
})
109122
})
110123

111124
describe('Diffs collecting in the root-level properties', () => {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: 3.0.3
2+
info:
3+
title: test
4+
version: 0.1.0
5+
servers:
6+
- url: https://example2.com
7+
description: It is a description of server
8+
paths:
9+
/path1:
10+
get:
11+
responses:
12+
'200':
13+
description: OK
14+
content: { }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
description: It is a description of server
8+
paths:
9+
/path1:
10+
get:
11+
responses:
12+
'200':
13+
description: OK
14+
content: { }

test/utils.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { findSharedPath, removeObjectDuplicates, removeSecurityDuplicates, slugify } from '../src/utils'
18-
import { getOperationBasePath } from '../src/apitypes/rest/rest.utils'
18+
import { extractOperationBasePath } from '../src/apitypes/rest/rest.utils'
1919

2020
describe('Utils', () => {
2121
describe('Unit tests for \'slugify\' function', () => {
@@ -42,20 +42,20 @@ describe('Utils', () => {
4242
},
4343
}]
4444

45-
expect(getOperationBasePath(servers)).toEqual('/api')
45+
expect(extractOperationBasePath(servers)).toEqual('/api')
4646
})
4747

4848
test('Should handle Servers with absolute url correctly', () => {
49-
expect(getOperationBasePath([{ url: 'https://example.com/v1' }])).toEqual('/v1')
50-
expect(getOperationBasePath([{ url: 'https://example.com/v1/' }])).toEqual('/v1')
49+
expect(extractOperationBasePath([{ url: 'https://example.com/v1' }])).toEqual('/v1')
50+
expect(extractOperationBasePath([{ url: 'https://example.com/v1/' }])).toEqual('/v1')
5151
})
5252

5353
test('Should handle Servers with relative url correctly', () => {
54-
expect(getOperationBasePath([{ url: '/v1' }])).toEqual('/v1')
55-
expect(getOperationBasePath([{ url: 'v1' }])).toEqual('/v1')
56-
expect(getOperationBasePath([{ url: 'v1/' }])).toEqual('/v1')
57-
expect(getOperationBasePath([{ url: '/v1/' }])).toEqual('/v1')
58-
expect(getOperationBasePath([{ url: '/' }])).toEqual('')
54+
expect(extractOperationBasePath([{ url: '/v1' }])).toEqual('/v1')
55+
expect(extractOperationBasePath([{ url: 'v1' }])).toEqual('/v1')
56+
expect(extractOperationBasePath([{ url: 'v1/' }])).toEqual('/v1')
57+
expect(extractOperationBasePath([{ url: '/v1/' }])).toEqual('/v1')
58+
expect(extractOperationBasePath([{ url: '/' }])).toEqual('')
5959
})
6060
})
6161

0 commit comments

Comments
 (0)