Skip to content

Commit a949ce7

Browse files
Alan-ChaErikWittern
authored andcommitted
Fix bug checking for duplicateLinkKey
Signed-off-by: Alan Cha <[email protected]>
1 parent 98f708c commit a949ce7

File tree

9 files changed

+51
-30
lines changed

9 files changed

+51
-30
lines changed

packages/oasgraph/lib/preprocessor.js

Lines changed: 18 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph/lib/preprocessor.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph/lib/schema_builder.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph/lib/schema_builder.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph/lib/utils.js

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph/lib/utils.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph/src/preprocessor.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,20 @@ export function createDataDef(
386386
)
387387
}
388388

389-
const preferredName = getPreferredName(names)
390-
391389
// resolve allOf element in schema if applicable
392390
if ('allOf' in schema) {
393391
schema = mergeAllOf(schema)
394392
}
395393

394+
const preferredName = getPreferredName(names)
395+
396+
const saneLinks = {}
397+
if (typeof links === 'object') {
398+
Object.keys(links).forEach((linkKey) => {
399+
saneLinks[Oas3Tools.beautify(linkKey)] = links[linkKey]
400+
})
401+
}
402+
396403
// Determine the index of possible existing data definition
397404
const index = getSchemaIndex(preferredName, schema, data.defs)
398405
if (index !== -1) {
@@ -402,25 +409,29 @@ export function createDataDef(
402409
// Collapse links if possible
403410
// I.e. if the current operation has links, combine them with the prexisting
404411
// ones
405-
if (typeof links !== 'undefined') {
412+
if (typeof saneLinks !== 'undefined') {
406413
if (typeof existingDataDef.links !== 'undefined') {
407414
// Check if there are any overlapping links
408-
Object.keys(existingDataDef.links).forEach(linkKey => {
409-
if (!deepEqual(existingDataDef[linkKey], links[linkKey])) {
415+
Object.keys(existingDataDef.links).forEach(saneLinkKey => {
416+
if (typeof saneLinks[saneLinkKey] !== 'undefined'
417+
&& !deepEqual(existingDataDef.links[saneLinkKey], saneLinks[saneLinkKey])) {
410418
handleWarning({
411419
typeKey: 'DUPLICATE_LINK_KEY',
412-
culprit: linkKey,
420+
culprit: `Multiple operations with the same response body share the same sanitized ` +
421+
`link key '${saneLinkKey}' but have different link definitions ` +
422+
`'${JSON.stringify(existingDataDef.links[saneLinkKey])}' and ` +
423+
`'${JSON.stringify(saneLinks[saneLinkKey])}'.`,
413424
data,
414425
log: preprocessingLog
415426
})
416427
}
417428
})
418429

419430
// Collapse the links
420-
Object.assign(existingDataDef.links, links)
431+
Object.assign(existingDataDef.links, saneLinks)
421432
} else {
422433
// No preexisting links, so simply assign the links
423-
existingDataDef.links = links
434+
existingDataDef.links = saneLinks
424435
}
425436
}
426437

@@ -455,7 +466,7 @@ export function createDataDef(
455466
schema,
456467
type,
457468
subDefinitions: undefined,
458-
links,
469+
links: saneLinks,
459470
otName: saneName,
460471
iotName: saneInputName
461472
}

packages/oasgraph/src/schema_builder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ function createFields({
552552

553553
// finally, add the object type to the fields (using sanitized field name)
554554
const saneLinkKey = Oas3Tools.beautifyAndStore(linkKey, data.saneMap)
555+
// TODO: check if fields already has this field name
555556
fields[saneLinkKey] = {
556557
type: resObjectType,
557558
resolve: linkResolver,

packages/oasgraph/src/utils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const WarningTypes: {
2323
return {
2424
type: 'MultipleResponses',
2525
message:
26-
`Operation '${culprit}' has more than one success status ` +
26+
`Operation ${culprit} has more than one success status ` +
2727
`codes (200 - 299).`,
2828
mitigation: `Will select response for status code '${solution}'.`
2929
}
@@ -32,7 +32,7 @@ export const WarningTypes: {
3232
return {
3333
type: 'MissingResponseSchema',
3434
message:
35-
`Operation '${culprit}' has no (valid) response schema. ` +
35+
`Operation ${culprit} has no (valid) response schema. ` +
3636
`If this operation has a 204 HTTP code, you can create a placeholder ` +
3737
`schema using the fillEmptyResponses option.`,
3838
mitigation: `Ignore operation`
@@ -113,9 +113,8 @@ export const WarningTypes: {
113113
DUPLICATE_LINK_KEY: (culprit: string, solution: string) => {
114114
return {
115115
type: 'duplicateLinkKey',
116-
message: `Multiple operations with the same response body schema share the same link key '${culprit}'.`,
117-
// TODO: improve mitigation message
118-
mitigation: `The link will replace the previous one.`
116+
message: culprit,
117+
mitigation: `The latter link definition will replace the previous one.`,
119118
}
120119
},
121120
UNRESOLVABLE_REFERENCE: (culprit: string, solution: string) => {

0 commit comments

Comments
 (0)