import { OpenAPIV3 } from 'openapi-types' ; import { $Refs } from '@apidevtools/swagger-parser' ; import { pascalCase , kebabCase } from '@sap-cloud-sdk/util' ; import { isReferenceObject } from '../schema-util' ; import { SchemaNaming } from '../openapi-types' ; import { SchemaRefMapping } from './parsing-info' ; import { ensureUniqueNames } from './unique-naming' ; import { ParserOptions } from './options' ; import { ensureValidSchemaNames } from './schema-naming' ; import { resolveBound } from './swagger-parser-workaround' ; param document param options returns internal export async function createRefs ( document , options ) { return OpenApiDocumentRefs . createRefs ( document , options ) ; } internal export class OpenApiDocumentRefs { param document param options returns static async createRefs ( document , options ) { return new OpenApiDocumentRefs ( await resolveBound ( document ) , OpenApiDocumentRefs . parseSchemaRefMapping ( document , options ) ) ; } param document param options returns private static parseSchemaRefMapping ( document , options ) { const originalNames = Object . keys ( document . components ?. schemas || { } ) ; const validSchemaNames = ensureValidSchemaNames ( originalNames , options ) ; const schemaNames = ensureUniqueNames ( validSchemaNames , options , { format : pascalCase , separator : '_' } ) ; const fileNames = ensureUniqueNames ( schemaNames , options , { format : kebabCase , separator : '-' , reservedWords : [ 'index' ] } ) ; return originalNames . reduce ( ( mapping , originalName , i ) => ( { ... mapping , [ `#/components/schemas/${ originalName }` ] : { schemaName : schemaNames [ i ] , fileName : fileNames [ i ] } } ) , { } ) ; } param refs param schemaRefMapping private constructor ( private refs , private schemaRefMapping ) { } param obj returns resolveObject < T > ( obj ) { return isReferenceObject ( obj ) ? this . refs . get ( obj . $ref ) : obj ; } param obj returns getSchemaNaming ( obj ) { const ref = isReferenceObject ( obj ) ? obj . $ref : obj ; if ( ! ref . startsWith ( '#/components/schemas' ) ) { throw new Error ( `Could not get schema naming for reference path '${ ref }'. Path does not reference a schema.` ) ; } const schemaNaming = this . schemaRefMapping [ ref ] ; if ( ! schemaNaming ) { throw new Error ( `Could not find schema naming for reference path '${ ref }'. Schema does not exist.` ) ; } return schemaNaming ; } }