Skip to content

Commit

Permalink
Merge pull request #340 from vahnag/vahagn/parser-nested-allOf
Browse files Browse the repository at this point in the history
Fix parsing of nested allOf objects
  • Loading branch information
elanh authored Jan 6, 2024
2 parents 07af57a + 5aae27d commit e41c1ae
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,27 @@ function convertParam(param: OpenAPIV3_1.ParameterObject): Param | undefined {
};
}

function mergeAllOfSchema(
schema: OpenAPIV3_1.SchemaObject,
): OpenAPIV3_1.SchemaObject | null {
if (!schema.allOf) return null;

const allOf = schema.allOf as OpenAPIV3_1.SchemaObject[];

return {
type: 'object',
title: schema.title || allOf.find((param) => param.title)?.title,
properties: allOf.reduce(
(acc, param) => ({
...acc,
...(mergeAllOfSchema(param)?.properties || param.properties),
}),
{},
) as Record<string, OpenAPIV3_1.SchemaObject>,
required: allOf.flatMap((param) => param.required || [], []) || undefined,
};
}

function convertSchema(schema: OpenAPIV3_1.SchemaObject): Param | undefined {
if (schema == null || Object.keys(schema).length === 0) return;

Expand All @@ -307,17 +328,11 @@ function convertSchema(schema: OpenAPIV3_1.SchemaObject): Param | undefined {
}

if (schema.allOf) {
const allOf = schema.allOf as OpenAPIV3_1.SchemaObject[];

return convertSchema({
type: 'object',
title: schema.title || allOf.find((param) => param.title)?.title,
properties: allOf.reduce(
(acc, param) => ({ ...acc, ...param.properties }),
{},
) as Record<string, OpenAPIV3_1.SchemaObject>,
required: allOf.flatMap((param) => param.required || [], []) || undefined,
});
const mergedAllOfSchema = mergeAllOfSchema(schema);

if (!mergedAllOfSchema) return;

return convertSchema(mergedAllOfSchema);
}

if (schema.oneOf) {
Expand Down

0 comments on commit e41c1ae

Please sign in to comment.