From ded8ef79e40886367302576048367ce7855ffa15 Mon Sep 17 00:00:00 2001 From: omermorad Date: Sun, 8 Nov 2026 21:18:16 +0200 Subject: [PATCH] fix(openapi): classify response enums by output compatibility --- packages/differs.openapi/src/differ.test.ts | 84 ++++++++++++++++++++- packages/differs.openapi/src/differ.ts | 19 +++++ 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/packages/differs.openapi/src/differ.test.ts b/packages/differs.openapi/src/differ.test.ts index 36e2431..a882430 100644 --- a/packages/differs.openapi/src/differ.test.ts +++ b/packages/differs.openapi/src/differ.test.ts @@ -4,11 +4,15 @@ import { diffStructural } from './structural.js'; type SchemaDirection = 'request' | 'response'; -function openApiSpec(direction: SchemaDirection, goalsRequired: boolean): Record { +function openApiSpec( + direction: SchemaDirection, + goalsRequired: boolean, + idConstraint: Record = {} +): Record { const schema = { type: 'object', properties: { - id: { type: 'string' }, + id: { type: 'string', ...idConstraint }, goals: { type: 'array', items: { type: 'string' } }, }, required: goalsRequired ? ['id', 'goals'] : ['id'], @@ -103,3 +107,79 @@ describe('OpenAPI required-property compatibility direction', () => { ]); }); }); + +describe('OpenAPI enum compatibility direction', () => { + const cases = [ + { + name: 'enum widening', + old: { enum: ['a'] }, + next: { enum: ['a', 'b'] }, + category: 'enum-value-added', + inputBreaking: false, + }, + { + name: 'enum narrowing', + old: { enum: ['a', 'b'] }, + next: { enum: ['a'] }, + category: 'enum-value-removed', + inputBreaking: true, + }, + { + name: 'enum constraint addition', + old: {}, + next: { enum: ['a'] }, + category: 'enum-added', + inputBreaking: true, + }, + { + name: 'enum constraint removal', + old: { enum: ['a'] }, + next: {}, + category: 'enum-removed', + inputBreaking: false, + }, + { + name: 'const addition', + old: {}, + next: { const: 'a' }, + category: 'enum-added', + inputBreaking: true, + }, + { + name: 'const removal', + old: { const: 'a' }, + next: {}, + category: 'enum-removed', + inputBreaking: false, + }, + { + name: 'const replacement', + old: { const: 'a' }, + next: { const: 'b' }, + category: 'enum-value-removed', + inputBreaking: true, + }, + { + name: 'null const replacement', + old: { type: ['string', 'null'], const: 'a' }, + next: { type: ['string', 'null'], const: null }, + category: 'enum-value-removed', + inputBreaking: true, + }, + ]; + + describe.each(['request', 'response'] as const)('%s', (direction) => { + test.each(cases)('$name', ({ name, old, next, category, inputBreaking }) => { + const breaking = + name.includes('replacement') || (direction === 'request' ? inputBreaking : !inputBreaking); + const result = diffOpenApiObjects( + openApiSpec(direction, false, old), + openApiSpec(direction, false, next) + ); + expect(result.changes).toEqual([ + expect.objectContaining({ category, severity: breaking ? 'breaking' : 'non-breaking' }), + ]); + expect(result.suggestedBump).toBe(breaking ? 'major' : 'minor'); + }); + }); +}); diff --git a/packages/differs.openapi/src/differ.ts b/packages/differs.openapi/src/differ.ts index 264571f..328bde3 100644 --- a/packages/differs.openapi/src/differ.ts +++ b/packages/differs.openapi/src/differ.ts @@ -41,6 +41,25 @@ export function diffOpenApiObjects( function classifyOpenApiChange(change: RawChange, newSpec: unknown): ChangeSeverity { if (change.schemaDirection === 'output') { + // Responses must remain valid against the schema known to existing clients. + // A const replacement is disjoint, rather than a narrowing: the walker + // represents it as enum-value-removed with both values populated. + if ( + change.type === 'enum-value-removed' && + change.path.endsWith('/const') && + change.newValue !== undefined + ) { + return 'breaking'; + } + + if (change.type === 'enum-value-added' || change.type === 'enum-removed') { + return 'breaking'; + } + + if (change.type === 'enum-value-removed' || change.type === 'enum-added') { + return 'non-breaking'; + } + if (change.type === 'required-removed') { return 'breaking'; }