Skip to content

Commit

Permalink
refactor: minor commment improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign committed May 15, 2024
1 parent 6dbca05 commit f85963d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/openapiToTsJsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ export async function openapiToTsJsonSchema(

const openApiParser = new $RefParser();
const jsonSchemaParser = new $RefParser();

// Resolve and inline external $ref definitions
const bundledOpenApiSchema = await openApiParser.bundle(openApiSchemaPath);
// Convert oas definitions to JSON schema
const initialJsonSchema = convertOpenApiToJsonSchema(bundledOpenApiSchema);

const inlinedRefs: Map<string, JSONSchema> = new Map();
// Inline and collect internal $ref definitions
const dereferencedJsonSchema = await jsonSchemaParser.dereference(
initialJsonSchema,
{
Expand All @@ -97,8 +101,8 @@ export async function openapiToTsJsonSchema(
}

/**
* mark inlined ref objects with a "SCHEMA_ID_SYMBOL" to retrieve their
* original $ref value once inlined
* mark inlined ref objects with a "SCHEMA_ID_SYMBOL"
* to retrieve their id once inlined
*/
inlinedSchema[SCHEMA_ID_SYMBOL] = id;

Expand Down
8 changes: 4 additions & 4 deletions src/utils/convertOpenApiToJsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ function convertToJsonSchema<Value extends unknown>(
}

/**
* Traverse the openAPI schema tree an brutally try to convert everything
* possible to JSON schema. We are probably overdoing since we process any object we find.
* Traverse the openAPI schema tree an brutally try to convert every oas definition
* to JSON schema. We are probably overdoing since we process any found object.
*
* - Is there a way to tell an OpenAPI schema objects convertible to JSON schema from the others?
* - Could we explicitly convert only the properties where we know conversion is needed?
* - Is there a way to tell an OpenAPI definition objects convertible to JSON schema from the others?
* - Could we explicitly convert only the properties that need it?
*
* @TODO Find a nicer way to convert convert all the expected OpenAPI schemas
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { JSONSchema, JSONSchemaWithPlaceholders } from '../../types';

/**
* Get any JSON schema node and:
* - Return ref placeholder is the entity is an inlined ref schema objects (with SCHEMA_ID_SYMBOL prop)
* - Return id placeholder if the entity is an inlined ref schema objects (with SCHEMA_ID_SYMBOL prop)
* - Return provided node in all other cases
*/
function replaceInlinedSchemaWithPlaceholder<Node extends unknown>(
Expand All @@ -21,7 +21,7 @@ function replaceInlinedSchemaWithPlaceholder<Node extends unknown>(
/**
* Iterate a JSON schema to replace inlined ref schema objects
* (marked with a SCHEMA_ID_SYMBOL property holding the original $ref value)
* with a string placeholder with a reference to the original $ref value ("_OTJS-START_/id/value_OTJS-END_")
* with a string placeholder with a reference to their internal id ("_OTJS-START_/id/value_OTJS-END_")
*/
export function replaceInlinedRefsWithStringPlaceholder(
schema: JSONSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { makeRelativeModulePath, PLACEHOLDER_REGEX } from '..';
import type { SchemaMetaDataMap } from '../../types';

/**
* Replace Refs placeholders with imported schemas
* Replace id placeholders with imported schemas
*/
export function replacePlaceholdersWithImportedSchemas({
schemaAsText,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/makeTsJsonSchema/replacePlaceholdersWithRefs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PLACEHOLDER_REGEX } from '..';

/**
* Replace Refs placeholders with original ref objects
* Replace id placeholders with their relevant $ref object
*/
export function replacePlaceholdersWithRefs({
schemaAsText,
Expand Down

0 comments on commit f85963d

Please sign in to comment.