import { createLogger } from '@sap-cloud-sdk/util' ; import { OpenAPIV3 } from 'openapi-types' ; import { isReferenceObject } from '../schema-util' ; import { OpenApiArraySchema , OpenApiEnumSchema , OpenApiObjectSchema , OpenApiObjectSchemaProperty , OpenApiReferenceSchema , OpenApiSchema , OpenApiSchemaProperties } from '../openapi-types' ; import { getType } from './type-mapping' ; import { OpenApiDocumentRefs } from './refs' ; import { ParserOptions } from './options' ; const logger = createLogger ( 'openapi-generator' ) ; param schema param refs param options returns internal export function parseSchema ( schema , refs , options ) { if ( ! schema ) { logger . verbose ( "No schema provided, continuing with 'any'." ) ; return { type : 'any' } ; } if ( isReferenceObject ( schema ) ) { return parseReferenceSchema ( schema , refs ) ; } if ( schema . type === 'array' ) { return parseArraySchema ( schema , refs , options ) ; } if ( schema . type === 'object' || schema . properties || 'additionalProperties' in schema ) { return parseObjectSchema ( schema , refs , options ) ; } if ( schema . enum ?. length ) { return parseEnumSchema ( schema , options ) ; } if ( schema . oneOf ?. length ) { return parseXOfSchema ( schema , refs , 'oneOf' , options ) ; } if ( schema . allOf ?. length ) { return parseXOfSchema ( schema , refs , 'allOf' , options ) ; } if ( schema . anyOf ?. length ) { return parseXOfSchema ( schema , refs , 'anyOf' , options ) ; } if ( schema . not ) { return { not : parseSchema ( schema . not , refs , options ) } ; } return { type : getType ( schema . type ) } ; } function parseReferenceSchema ( schema , refs ) { return { ... schema , ... refs . getSchemaNaming ( schema ) } ; } param schema param refs param options returns function parseArraySchema ( schema , refs , options ) { return { uniqueItems : schema . uniqueItems , items : parseSchema ( schema . items , refs , options ) } ; } param schema param refs param options returns function parseObjectSchema ( schema , refs , options ) { const properties = parseObjectSchemaProperties ( schema , refs , options ) ; if ( schema . additionalProperties === false ) { if ( ! properties . length ) { throw new Error ( 'Could not parse object schema without neither properties nor additional properties.' ) ; } return { properties } ; } const additionalProperties = typeof schema . additionalProperties === 'object' && Object . keys ( schema . additionalProperties ) . length ? parseSchema ( schema . additionalProperties , refs , options ) : { type : 'any' } ; return { properties , additionalProperties } ; } param schema param refs param options returns function parseObjectSchemaProperties ( schema , refs , options ) { return Object . entries ( schema . properties || { } ) . reduce ( ( props , [ propName , propSchema ] ) => [ ... props , { schema : parseSchema ( propSchema , refs , options ) , description : isReferenceObject ( propSchema ) ? undefined : propSchema . description , name : propName , required : schema . required ?. includes ( propName ) || false , schemaProperties : { ... parseSchemaProperties ( propSchema ) } } ] , [ ] ) ; } param schema param options returns function parseEnumSchema ( schema , options ) { const type = schema . type ? getType ( schema . type ) : 'string' ; return { type , enum : ( schema . enum || [ ] ) . map ( entry => { if ( type === 'string' && entry !== null ) { return getEnumStringValue ( String ( entry ) ) ; } if ( entry === null && ! schema . nullable ) { if ( options . strictNaming ) { throw new Error ( 'null was used as a parameter in an enum, although the schema was not declared as nullable' ) ; } else { logger . warn ( 'null was used as a parameter in an enum, although the schema was not declared as nullable' ) ; } } return entry ; } ) } ; } function getEnumStringValue ( input ) { return `'${ input . replace ( /'/g , "\\'" ) }'` ; } param schema param refs param xOf param options returns function parseXOfSchema ( schema , refs , xOf , options ) { return { [ xOf ] : ( schema [ xOf ] || [ ] ) . map ( entry => parseSchema ( entry , refs , options ) ) } ; } param schema returns internal export function parseSchemaProperties ( schema ) { if ( isReferenceObject ( schema ) ) { return { } ; } const schemaPropertyNames = [ 'deprecated' , 'example' , 'format' , 'default' , 'multipleOf' , 'maximum' , 'minimum' , 'maxLength' , 'minLength' , 'minItems' , 'maxItems' , 'pattern' ] ; return schemaPropertyNames . reduce ( ( properties , propertyName ) => { if ( schema [ propertyName ] ) { return { ... properties , [ propertyName ] : schema [ propertyName ] } ; } return properties ; } , { } ) ; }