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
44 changes: 42 additions & 2 deletions packages/differs.core/src/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ function walkInternal(
// 18. Composition changes (anyOf, oneOf, allOf, if/then/else, not)
changes.push(...compareComposition(old, newS, path, depth));

// Preserve explicit dialects from every enclosing schema without interpreting
// them here. The consumer decides which dialects its compatibility proof supports.
const schemaDialects = [old, newS]
.filter((schema) => Object.hasOwn(schema, '$schema'))
.map((schema) => schema.$schema);
if (schemaDialects.length > 0) {
for (const change of changes) {
if (change.propertyAddition) {
change.propertyAddition.schemaDialects = [
...(change.propertyAddition.schemaDialects ?? []),
...schemaDialects,
];
}
}
}

// Declaring a property changes evaluation annotations, which an ancestor's
// unevaluated* keyword can observe even when its own text is unchanged.
if (
old.unevaluatedProperties !== undefined ||
newS.unevaluatedProperties !== undefined ||
old.unevaluatedItems !== undefined ||
newS.unevaluatedItems !== undefined
) {
return uncertainPropertyAdditions(changes);
}
return changes;
}

Expand Down Expand Up @@ -667,6 +693,7 @@ function compareProperties(
type: 'property-added',
oldValue: undefined,
newValue: newProps[key],
propertyAddition: { name: key, oldParent: oldSchema, newParent: newSchema },
});
}
}
Expand Down Expand Up @@ -1099,7 +1126,11 @@ function compareArrayItems(
joinPath(path, 'contains'),
depth + 1
);
changes.push(...nestedChanges);
changes.push(
...(oldSchema.maxContains !== undefined || newSchema.maxContains !== undefined
? uncertainPropertyAdditions(nestedChanges)
: nestedChanges)
);
} else {
changes.push({
path: joinPath(path, 'contains'),
Expand Down Expand Up @@ -1375,7 +1406,7 @@ function compareOneOf(
joinPath(path, 'oneOf', String(newIdx)),
depth + 1
);
changes.push(...nestedChanges);
changes.push(...uncertainPropertyAdditions(nestedChanges));
}
}

Expand Down Expand Up @@ -1591,3 +1622,12 @@ function matchCompositionOptions(

return { matched, removed, added };
}

/** Local subset proofs are not sufficient inside non-monotonic applicators. */
function uncertainPropertyAdditions(changes: RawChange[]): RawChange[] {
return changes.map((change) =>
change.propertyAddition
? { ...change, propertyAddition: { ...change.propertyAddition, uncertain: true } }
: change
);
}
32 changes: 30 additions & 2 deletions packages/differs.openapi/src/differ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { ChangeSeverity, DiffResult, RawChange } from '@contractual/types';
import { assembleResult, classify, classifyPropertyAdded } from '@contractual/differs.core';
import { resolveOpenApiSpec } from './resolver.js';
import { diffStructural } from './structural.js';
import { classifyPropertyAddition, supportedDialect } from './property-addition.js';

/**
* Diff two OpenAPI specification files
Expand Down Expand Up @@ -36,10 +37,30 @@ export function diffOpenApiObjects(
newSpec: Record<string, unknown>
): DiffResult {
const rawChanges = diffStructural(oldSpec, newSpec);
return assembleResult(rawChanges, newSpec, { classifyFn: classifyOpenApiChange });
const oldUsesNullable = typeof oldSpec.openapi === 'string' && oldSpec.openapi.startsWith('3.0.');
const newUsesNullable = typeof newSpec.openapi === 'string' && newSpec.openapi.startsWith('3.0.');
const supportedDocumentDialects = [oldSpec, newSpec].every(
(spec) => !Object.hasOwn(spec, 'jsonSchemaDialect') || supportedDialect(spec.jsonSchemaDialect)
);
return assembleResult(rawChanges, newSpec, {
classifyFn: (change, schema) =>
classifyOpenApiChange(
change,
schema,
oldUsesNullable,
newUsesNullable,
supportedDocumentDialects
),
});
}

function classifyOpenApiChange(change: RawChange, newSpec: unknown): ChangeSeverity {
function classifyOpenApiChange(
change: RawChange,
newSpec: unknown,
oldUsesNullable: boolean,
newUsesNullable: boolean,
supportedDocumentDialects: boolean
): ChangeSeverity {
if (change.enumConstraintEquivalent) return 'non-breaking';

// oneOf accepts exactly one matching branch. Either narrowing or widening
Expand Down Expand Up @@ -79,6 +100,13 @@ function classifyOpenApiChange(change: RawChange, newSpec: unknown): ChangeSever
}

if (change.type === 'property-added') {
if (change.propertyAddition)
return classifyPropertyAddition(
change,
oldUsesNullable,
newUsesNullable,
supportedDocumentDialects
);
return classifyPropertyAdded(change, newSpec);
}

Expand Down
Loading
Loading