Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions packages/differs.openapi/src/differ.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { diffStructural } from './structural.js';

type SchemaDirection = 'request' | 'response';

function openApiSpec(direction: SchemaDirection, goalsRequired: boolean): Record<string, unknown> {
function openApiSpec(
direction: SchemaDirection,
goalsRequired: boolean,
idConstraint: Record<string, unknown> = {}
): Record<string, unknown> {
const schema = {
type: 'object',
properties: {
id: { type: 'string' },
id: { type: 'string', ...idConstraint },
goals: { type: 'array', items: { type: 'string' } },
},
required: goalsRequired ? ['id', 'goals'] : ['id'],
Expand Down Expand Up @@ -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');
});
});
});
19 changes: 19 additions & 0 deletions packages/differs.openapi/src/differ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
Loading