Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleh Shumov committed Dec 8, 2024
1 parent 2667d1b commit 1d119f2
Showing 1 changed file with 20 additions and 34 deletions.
54 changes: 20 additions & 34 deletions src/commands/openapi/refs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-param-reassign */
/* eslint-disable camelcase */
import type { OASDocument } from 'oas/types';
// eslint-disable-next-line camelcase
import type { IJsonSchema, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types';
import type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types';

import fs from 'node:fs';
import path from 'node:path';
Expand All @@ -21,13 +21,14 @@ import { validateFilePath } from '../../lib/validatePromptInput.js';

type SchemaCollection = Record<
string,
// eslint-disable-next-line camelcase
OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject | OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject
>;

type Schema =
// eslint-disable-next-line camelcase
OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject | OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject;
| OpenAPIV3_1.ReferenceObject
| OpenAPIV3_1.SchemaObject
| OpenAPIV3.ReferenceObject
| OpenAPIV3.SchemaObject;

export default class OpenAPIRefsCommand extends BaseCommand<typeof OpenAPIRefsCommand> {
static summary = 'Resolves circular and recursive references in OpenAPI by replacing them with object schemas.';
Expand Down Expand Up @@ -207,7 +208,6 @@ export default class OpenAPIRefsCommand extends BaseCommand<typeof OpenAPIRefsCo
throw new Error(`Schema or property not found for path: ${refPath}`);
}

// Handle case when schema contains only a $ref
if ('$ref' in property) {
const refSchemaName = property.$ref?.split('/')[3];
if (refSchemaName) {
Expand All @@ -224,42 +224,28 @@ export default class OpenAPIRefsCommand extends BaseCommand<typeof OpenAPIRefsCo
}

// Handle references within items in an array
let refSchemaName: string | undefined;
let refSchemaName: string;
if (
refParts.length > 6 &&
refParts[6] === 'items' &&
property.type === 'array' &&
property.items &&
typeof property.items === 'object'
) {
const itemsRefSchemaName = (property.items as IJsonSchema).$ref?.split('/')[3];
if (itemsRefSchemaName) {
refSchemaName = `${itemsRefSchemaName}Ref`;
if (itemsRefSchemaName.includes('Ref')) {
debug(`Skipping proxy schema for ${itemsRefSchemaName} in array items.`);
return;
if ('$ref' in property.items) {
const itemsRefSchemaName = property.items.$ref.split('/')[3];

if (itemsRefSchemaName) {
refSchemaName = `${itemsRefSchemaName}Ref`;
if (itemsRefSchemaName.includes('Ref')) {
debug(`Skipping proxy schema for ${itemsRefSchemaName} in array items.`);
return;
}
property.items = { $ref: `#/components/schemas/${refSchemaName}` };
createRefSchema(itemsRefSchemaName, refSchemaName);
}
property.items = { $ref: `#/components/schemas/${refSchemaName}` };
createRefSchema(itemsRefSchemaName, refSchemaName);
}
}
// Handle direct reference
else if ('$ref' in property && typeof property.$ref === 'string') {
refSchemaName = property.$ref?.split('/')[3];
if (refSchemaName) {
const newRefSchemaName = `${refSchemaName}Ref`;
if (refSchemaName.includes('Ref')) {
debug(`Skipping proxy schema for ${refSchemaName}.`);
return;
}
replaceRef(schemaName, propertyName, newRefSchemaName);
createRefSchema(refSchemaName, newRefSchemaName);
} else {
throw new Error(`Invalid $ref in property: ${JSON.stringify(property)}`);
}
} else {
throw new Error(`Property does not contain a $ref: ${JSON.stringify(property)}`);
}
});
}

Expand All @@ -281,7 +267,7 @@ export default class OpenAPIRefsCommand extends BaseCommand<typeof OpenAPIRefsCo
const schemaName = refParts[3];
const propertyName = refParts[5];

let schema = schemas?.[schemaName];
let schema: Schema = schemas?.[schemaName];
if (!schema) {
warn(`Schema not found for: ${schemaName}`);
return;
Expand Down Expand Up @@ -320,7 +306,7 @@ export default class OpenAPIRefsCommand extends BaseCommand<typeof OpenAPIRefsCo

let remainingCircularRefs = await OpenAPIRefsCommand.getCircularRefsFromOas(openApiData);
let iterationCount = 0;
const maxIterations = 8;
const maxIterations = 10;

while (remainingCircularRefs.length > 0 && iterationCount < maxIterations) {
debug(
Expand Down

0 comments on commit 1d119f2

Please sign in to comment.