diff --git a/src/core/compare.ts b/src/core/compare.ts index 49aff4f..9d7ec84 100644 --- a/src/core/compare.ts +++ b/src/core/compare.ts @@ -156,7 +156,7 @@ const cleanUpRecursive = (ctx: NodeContext): NodeContext => { } export const getOrCreateChildDiffAdd = (diffUniquenessCache: EvaluationCacheService, childCtx: CompareContext) => { - return diffUniquenessCache.cacheEvaluationResultByFootprint<[unknown, string, CompareScope, typeof DiffAction.add], DiffEntry>([childCtx.after.value, buildPathsIdentifier(childCtx.after.declarativePaths), childCtx.scope, DiffAction.add], () => { + return diffUniquenessCache.cacheEvaluationResultByFootprint<[unknown, string, CompareScope, typeof DiffAction.add, PropertyKey], DiffEntry>([childCtx.after.value, buildPathsIdentifier(childCtx.after.declarativePaths), childCtx.scope, DiffAction.add, childCtx.mergeKey], () => { return diffFactory.added(childCtx) }, {} as DiffEntry, (result, guard) => { Object.assign(guard, result) diff --git a/test/bugs.test.ts b/test/bugs.test.ts index ae02bd6..82f3ecf 100644 --- a/test/bugs.test.ts +++ b/test/bugs.test.ts @@ -1,4 +1,4 @@ -import { annotation, apiDiff, ClassifierType, CompareOptions, DiffAction, nonBreaking, unclassified } from '../src' +import { annotation, apiDiff, breaking, ClassifierType, CompareOptions, DiffAction, nonBreaking, unclassified } from '../src' import offeringQualificationBefore from './helper/resources/api-v2-offeringqualification-qualification-post/before.json' import offeringQualificationAfter from './helper/resources/api-v2-offeringqualification-qualification-post/after.json' import readDefaultValueOfRequiredBefore from './helper/resources/read-default-value-of-required-field/before.json' @@ -30,6 +30,9 @@ import spearedParamsAfter from './helper/resources/speared-parameters/after.json import wildcardContentSchemaMediaTypeCombinedWithSpecificMediaTypeBefore from './helper/resources/wildcard-content-schema-media-type-combined-with-specific-media-type/before.json' import wildcardContentSchemaMediaTypeCombinedWithSpecificMediaTypeAfter from './helper/resources/wildcard-content-schema-media-type-combined-with-specific-media-type/after.json' +import shouldNotMissRemoveDiffForEnumEntryInOneOfBefore from './helper/resources/should-not-miss-remove-diff-for-enum-entry-in-oneOf/before.json' +import shouldNotMissRemoveDiffForEnumEntryInOneOfAfter from './helper/resources/should-not-miss-remove-diff-for-enum-entry-in-oneOf/after.json' + import { diffsMatcher } from './helper/matchers' import { TEST_DIFF_FLAG, TEST_ORIGINS_FLAG } from './helper' import { JSON_SCHEMA_NODE_SYNTHETIC_TYPE_NOTHING } from '@netcracker/qubership-apihub-api-unifier' @@ -227,4 +230,35 @@ describe('Real Data', () => { }), ])) }) + + it('should not miss remove diff for enum entry in oneOf', () => { + const before: any = shouldNotMissRemoveDiffForEnumEntryInOneOfBefore + const after: any = shouldNotMissRemoveDiffForEnumEntryInOneOfAfter + const { merged } = apiDiff(before, after, OPTIONS) + + expect( + Object.values((merged as any).paths['/path1'].post.requestBody.content['application/json'].schema.oneOf[1].properties.scope.items.enum[TEST_DIFF_FLAG]) + ).toEqual(diffsMatcher([ + expect.objectContaining({ + beforeValue: 'query', + action: DiffAction.remove, + type: breaking, + }), + expect.objectContaining({ + beforeValue: 'subscription', + action: DiffAction.remove, + type: breaking, + }), + expect.objectContaining({ + afterValue: 'argument', + action: DiffAction.add, + type: nonBreaking, + }), + expect.objectContaining({ + afterValue: 'annotation', + action: DiffAction.add, + type: nonBreaking, + }), + ])) + }) }) diff --git a/test/helper/resources/should-not-miss-remove-diff-for-enum-entry-in-oneOf/after.json b/test/helper/resources/should-not-miss-remove-diff-for-enum-entry-in-oneOf/after.json new file mode 100644 index 0000000..82720f4 --- /dev/null +++ b/test/helper/resources/should-not-miss-remove-diff-for-enum-entry-in-oneOf/after.json @@ -0,0 +1,66 @@ +{ + "openapi": "3.0.3", + "info": { + "title": "test", + "version": "0.1.0" + }, + "paths": { + "/path1": { + "post": { + "requestBody": { + "content": { + "application/json": { + "schema": { + "oneOf": [ + { + "type": "object", + "title": "SearchRestParams", + "properties": { + "apiType": { + "type": "string", + "enum": [ + "rest" + ] + }, + "scope": { + "type": "array", + "items": { + "type": "string", + "enum": [ + "request" + ] + } + } + } + }, + { + "type": "object", + "title": "SearchGQLParams", + "properties": { + "scope": { + "type": "array", + "items": { + "type": "string", + "enum": [ + "argument", + "annotation" + ] + } + } + } + } + ] + } + } + } + }, + "responses": { + "200": { + "description": "Success", + "content": {} + } + } + } + } + } +} \ No newline at end of file diff --git a/test/helper/resources/should-not-miss-remove-diff-for-enum-entry-in-oneOf/before.json b/test/helper/resources/should-not-miss-remove-diff-for-enum-entry-in-oneOf/before.json new file mode 100644 index 0000000..9a30dde --- /dev/null +++ b/test/helper/resources/should-not-miss-remove-diff-for-enum-entry-in-oneOf/before.json @@ -0,0 +1,66 @@ +{ + "openapi": "3.0.3", + "info": { + "title": "test", + "version": "0.1.0" + }, + "paths": { + "/path1": { + "post": { + "requestBody": { + "content": { + "application/json": { + "schema": { + "oneOf": [ + { + "type": "object", + "title": "SearchRestParams", + "properties": { + "apiType": { + "type": "string", + "enum": [ + "Rest" + ] + }, + "scope": { + "type": "array", + "items": { + "type": "string", + "enum": [ + "request" + ] + } + } + } + }, + { + "type": "object", + "title": "SearchGQLParams", + "properties": { + "scope": { + "type": "array", + "items": { + "type": "string", + "enum": [ + "query", + "subscription" + ] + } + } + } + } + ] + } + } + } + }, + "responses": { + "200": { + "description": "Success", + "content": {} + } + } + } + } + } +} \ No newline at end of file