Move filterSelectedOpenAPISchemas to @gitbook/openapi-parser (#3054)

This commit is contained in:
Nolann B.
2025-03-27 15:53:41 +01:00
committed by GitHub
parent 5b2bf828e3
commit 7bb37c7f0a
6 changed files with 43 additions and 36 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@gitbook/openapi-parser': patch
'@gitbook/react-openapi': patch
---
Move filterSelectedOpenAPISchemas to @gitbook/openapi-parser
+1
View File
@@ -6,4 +6,5 @@ export type * from '@scalar/openapi-types';
export * from './error';
export * from './helpers/shouldIgnoreEntity';
export * from './traverse';
export * from './schemas';
export type * from './types';
+27
View File
@@ -0,0 +1,27 @@
import type { OpenAPIV3, OpenAPIV3_1 } from '@scalar/openapi-types';
import { shouldIgnoreEntity } from './helpers/shouldIgnoreEntity';
import type { OpenAPISchema } from './types';
/**
* Extract selected schemas from the OpenAPI document.
*/
export function filterSelectedOpenAPISchemas(
schema: OpenAPIV3.Document | OpenAPIV3_1.Document,
selectedSchemas: string[]
): OpenAPISchema[] {
const componentsSchemas = schema.components?.schemas ?? {};
// Preserve the order of the selected schemas
return selectedSchemas
.map((name) => {
const schema = componentsSchemas[name];
if (schema && !shouldIgnoreEntity(schema)) {
return {
name,
schema,
};
}
return null;
})
.filter((schema): schema is OpenAPISchema => !!schema);
}
+5
View File
@@ -79,3 +79,8 @@ export type FilesystemEntry<T extends AnyObject> = {
filename: string;
specification: T;
};
export type OpenAPISchema = {
name: string;
schema: OpenAPIV3.SchemaObject;
};
@@ -1,12 +1,7 @@
import {
type Filesystem,
type OpenAPIV3,
type OpenAPIV3_1,
type OpenAPIV3xDocument,
shouldIgnoreEntity,
} from '@gitbook/openapi-parser';
import type { Filesystem, OpenAPIV3xDocument } from '@gitbook/openapi-parser';
import { filterSelectedOpenAPISchemas } from '@gitbook/openapi-parser';
import { dereferenceFilesystem } from '../dereference';
import type { OpenAPISchema, OpenAPISchemasData } from '../types';
import type { OpenAPISchemasData } from '../types';
//!!TODO: We should return only the schemas that are used in the block. Still a WIP awaiting future work.
@@ -32,26 +27,3 @@ export async function resolveOpenAPISchemas(
return { schemas };
}
/**
* Extract selected schemas from the OpenAPI document.
*/
export function filterSelectedOpenAPISchemas(
schema: OpenAPIV3.Document | OpenAPIV3_1.Document,
selectedSchemas: string[]
): OpenAPISchema[] {
const componentsSchemas = schema.components?.schemas ?? {};
// Preserve the order of the selected schemas
return selectedSchemas
.map((name) => {
const schema = componentsSchemas[name];
if (schema && !shouldIgnoreEntity(schema)) {
return {
name,
schema,
};
}
return null;
})
.filter((schema): schema is OpenAPISchema => !!schema);
}
+1 -5
View File
@@ -1,6 +1,7 @@
import type {
OpenAPICustomOperationProperties,
OpenAPICustomSpecProperties,
OpenAPISchema,
OpenAPIV3,
} from '@gitbook/openapi-parser';
@@ -56,11 +57,6 @@ export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
securities: [string, OpenAPIV3.SecuritySchemeObject][];
}
export type OpenAPISchema = {
name: string;
schema: OpenAPIV3.SchemaObject;
};
export interface OpenAPISchemasData {
/** Components schemas to be used for schemas */
schemas: OpenAPISchema[];