Skip to content

Commit

Permalink
prevent creation of optional fields with undefined value
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpn committed Apr 30, 2024
1 parent 10d0d76 commit 473f05c
Showing 1 changed file with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ export function mergePaths(bundledDocuments: BundledDocument[]): OpenAPIV3.Paths
const mergedPathItem = mergedPaths[path];

try {
mergedPathItem.summary = mergeOptionalPrimitiveValue(
sourcePathItem.summary,
mergedPathItem.summary
);
mergeOptionalPrimitiveValue('summary', sourcePathItem, mergedPathItem);
} catch {
throw new Error(
`❌ Unable to bundle ${chalk.bold(absolutePath)} since ${chalk.bold(
Expand All @@ -44,10 +41,7 @@ export function mergePaths(bundledDocuments: BundledDocument[]): OpenAPIV3.Paths
}

try {
mergedPathItem.description = mergeOptionalPrimitiveValue(
sourcePathItem.description,
mergedPathItem.description
);
mergeOptionalPrimitiveValue('description', sourcePathItem, mergedPathItem);
} catch {
throw new Error(
`❌ Unable to bundle ${chalk.bold(absolutePath)} since ${chalk.bold(
Expand Down Expand Up @@ -111,23 +105,22 @@ function mergeOperations(
}
}

function mergeOptionalPrimitiveValue<Value extends string | number | boolean | undefined>(
incoming: Value,
existing: Value
): Value {
if (!incoming) {
return existing;
function mergeOptionalPrimitiveValue<FieldName extends string>(
fieldName: FieldName,
source: { [field in FieldName]?: unknown },
merged: { [field in FieldName]?: unknown }
): void {
if (!source[fieldName]) {
return;
}

if (incoming && !existing) {
return incoming;
if (source[fieldName] && !merged[fieldName]) {
merged[fieldName] = source[fieldName];
}

if (incoming !== existing) {
throw new Error('Primitive value merge conflict');
if (source[fieldName] !== merged[fieldName]) {
throw new Error(`${fieldName} merge conflict`);
}

return existing;
}

function mergeParameters(
Expand Down

0 comments on commit 473f05c

Please sign in to comment.