11import {
22 Oas3_1Schema ,
3- Oas3Definition ,
43 Oas3Operation ,
54 Oas3Parameter ,
65 Oas3RequestBody ,
76 Oas3Schema ,
87 Referenced ,
98} from "@redocly/openapi-core/lib/typings/openapi"
10- import { get , isObject } from "lodash-es"
11- import { Config , Context } from "./config"
9+ import { get } from "lodash-es"
10+ import { Context } from "./config"
1211
1312export type OAS3 = Oas3Schema | Oas3_1Schema
1413
15- const HttpMethods = [ "get" , "post" , "put" , "patch" , "delete" , "head" , "options" , "trace" ] as const
16-
17- // Oas3PathItem defines each method operation as a property.
18- // We want to be able to iterate over methods so we introduce this type.
19- export interface PathItem {
20- ops : Record < string , Oas3Operation >
21- summary ?: string
22- description ?: string
23- parameters ?: Array < Referenced < Oas3Parameter > >
24- }
25-
26- export function filterSchema ( doc : Oas3Definition , config : Partial < Config > ) {
27- const paths : Record < string , PathItem > = { }
28- let usedSchemaRefs = new Set < string > ( )
29- for ( const [ path , pathConfig ] of Object . entries ( doc . paths ?? { } ) ) {
30- const logTag = `${ "[ALL]" . toUpperCase ( ) . padEnd ( 6 , " " ) } ${ path } `
31- if ( ! isObject ( pathConfig ) ) continue
32-
33- if ( "$ref" in pathConfig ) {
34- console . warn ( `${ logTag } $ref should be resolved before (skipping)` )
35- continue
36- }
37-
38- paths [ path ] = {
39- ops : { } ,
40- summary : pathConfig . summary ,
41- description : pathConfig . description ,
42- parameters : pathConfig . parameters ,
43- }
44-
45- for ( const method of HttpMethods ) {
46- const op = pathConfig [ method ]
47- if ( ! op ) continue
48-
49- if ( config . includeTags && ! op . tags ?. some ( ( tag ) => config . includeTags ! . includes ( tag ) ) ) {
50- continue
51- }
52-
53- paths [ path ] . ops [ method ] = op
54- extractSchemaReferences ( op , usedSchemaRefs )
55- }
56- }
57- const schemas = doc . components ?. schemas ?? { }
58-
59- // When we filter out operations we also want to filter out schemas that are no longer used.
60- let schemaRefsToCheck = usedSchemaRefs
61- while ( schemaRefsToCheck . size > 0 ) {
62- const newSchemaRefs = new Set < string > ( )
63- for ( const ref of schemaRefsToCheck ) {
64- extractSchemaReferences ( schemas [ ref ] , newSchemaRefs )
65- }
66- schemaRefsToCheck = newSchemaRefs . difference ( usedSchemaRefs )
67- usedSchemaRefs = usedSchemaRefs . union ( newSchemaRefs )
68- }
69- for ( const unusedSchema of new Set ( Object . keys ( schemas ) ) . difference ( usedSchemaRefs ) ) {
70- delete schemas [ unusedSchema ]
71- }
72- return { paths, schemas }
73- }
74-
7514// todo: wrong <T> typing
7615export const unref = < T extends Oas3RequestBody | Oas3Parameter | OAS3 > (
7716 ctx : Context ,
@@ -84,7 +23,7 @@ export const unref = <T extends Oas3RequestBody | Oas3Parameter | OAS3>(
8423 const obj = parts . reduce (
8524 // openapi encodes "/" in key as "~1"
8625 ( acc , x ) => get ( acc , x , get ( acc , decodeURIComponent ( x ) . replaceAll ( "~1" , "/" ) ) ) ,
87- { components : { schemas : ctx . schemas } } ,
26+ ctx . doc ,
8827 )
8928
9029 if ( obj ) return obj as unknown as T
@@ -147,23 +86,3 @@ export const getRepSchema = (ctx: Context, config: Oas3Operation): OAS3 | undefi
14786
14887 return undefined
14988}
150-
151- function extractSchemaReferences ( obj : any , schemaRefs : Set < string > ) : void {
152- if ( obj === null || obj === undefined ) {
153- return
154- }
155-
156- if ( typeof obj === "object" && ! Array . isArray ( obj ) && obj . $ref ) {
157- const ref = obj . $ref
158- if ( typeof ref === "string" && ref . startsWith ( "#/components/schemas/" ) ) {
159- const schemaName = ref . replace ( "#/components/schemas/" , "" )
160- schemaRefs . add ( schemaName )
161- }
162- }
163-
164- if ( Array . isArray ( obj ) ) {
165- obj . forEach ( ( item ) => extractSchemaReferences ( item , schemaRefs ) )
166- } else if ( typeof obj === "object" ) {
167- Object . values ( obj ) . forEach ( ( prop ) => extractSchemaReferences ( prop , schemaRefs ) )
168- }
169- }
0 commit comments