@@ -19,6 +19,7 @@ import { promises as fsPromises } from 'fs';
1919import * as path from 'path' ;
2020import { fileHeader , inFileDisclaimer } from './consts' ;
2121import { definitionsDir , isObject , reset , schemaDir , toPascalCase } from './utils' ;
22+ import * as yaml from 'js-yaml' ;
2223
2324const { writeFile, readFile } = fsPromises ;
2425
@@ -60,6 +61,7 @@ const metadataProperties = ['title', 'description', 'default', 'type'];
6061 * Embellishes the provided schema to increase its compatibility with json-schema-to-typescript, the resulting schema should keep the validation properties as the input one (phase 1)
6162 * - adds missing type:object properties // not necessary ?
6263 * - adds missing titles to objects
64+ * - removes extra titles on value types
6365 * @param schema The schema to embellish
6466 * @param path The current path of the schema relative to the original schema
6567 * @param parentTitle The title of the parent object, if any
@@ -74,7 +76,7 @@ function prepareSchema(schema: any, path: string[] = ['#'], parentTitle: string
7476 }
7577 const newSchema = JSON . parse ( JSON . stringify ( schema ) ) ;
7678 const parent = path . slice ( - 1 ) [ 0 ] ;
77- const schemaKeys = Object . keys ( newSchema ) ;
79+ let schemaKeys = Object . keys ( newSchema ) ;
7880 const isItemWithAdditionalProperties =
7981 parent === 'additionalProperties' && path . slice ( - 2 ) [ 0 ] === 'items' && newSchema . properties ; // only "useful" for SwitchTask.Switch.Cases
8082 if ( ! structuralObjectProperties . includes ( parent ) || isItemWithAdditionalProperties ) {
@@ -86,6 +88,16 @@ function prepareSchema(schema: any, path: string[] = ['#'], parentTitle: string
8688 if ( newSchema . title ) {
8789 parentTitle = newSchema . title ;
8890 }
91+ if (
92+ newSchema . title &&
93+ newSchema . type &&
94+ newSchema . type !== 'object' &&
95+ newSchema . type !== 'array' &&
96+ newSchema . title != 'RuntimeExpression' // RuntimeExpression is a string but used as its own type, we want to keep its title to build a JSON pointer later
97+ ) {
98+ delete newSchema . title ;
99+ schemaKeys = schemaKeys . filter ( ( key ) => key === 'title' ) ;
100+ }
89101 if (
90102 ! newSchema . title &&
91103 ( ! newSchema . type || newSchema . type === 'object' || newSchema . type === 'array' ) && // only naming object or array types
@@ -169,8 +181,16 @@ function mutateSchema(schema: any, path: string[] = ['#']): any {
169181 */
170182async function generate ( srcFile : string , destFile : string ) : Promise < void > {
171183 const options : Partial < Options > = {
172- customName : ( schema : JSONSchema , keyNameFromDefinition : string | undefined ) =>
173- schema . $id ?. includes ( 'serverlessworkflow.io' ) ? 'Workflow' : keyNameFromDefinition ,
184+ customName : ( schema : JSONSchema , keyNameFromDefinition : string | undefined ) => {
185+ if ( schema . $id ?. includes ( 'serverlessworkflow.io' ) ) {
186+ return 'Workflow' ;
187+ }
188+ if ( keyNameFromDefinition === 'oauth2Token' ) {
189+ // seems to ignore the title from this object, so forcing it...
190+ return schema . title ;
191+ }
192+ return keyNameFromDefinition ;
193+ } ,
174194 bannerComment : `${ fileHeader }
175195${ inFileDisclaimer }
176196
@@ -181,8 +201,11 @@ ${inFileDisclaimer}
181201 //unreachableDefinitions: true,
182202 } ;
183203 const schemaText = await readFile ( srcFile , { encoding : 'utf-8' } ) ;
184- let schema = prepareSchema ( JSON . parse ( schemaText ) ) ;
185- await writeFile ( srcFile . replace ( 'workflow' , '__internal_workflow' ) , JSON . stringify ( schema , null , 2 ) ) ;
204+ let schema = prepareSchema ( yaml . load ( schemaText ) ) ;
205+ await writeFile (
206+ srcFile . replace ( 'workflow' , '__internal_workflow' ) . replace ( '.yaml' , '.json' ) ,
207+ JSON . stringify ( schema , null , 2 ) . replace ( 'workflow.yaml' , 'workflow.json' ) ,
208+ ) ;
186209 schema = mutateSchema ( schema ) ;
187210 //await writeFile(srcFile.replace('workflow', '__mutated_workflow'), JSON.stringify(schema, null, 2));
188211 const declarations = await compile ( schema , 'Workflow' , options ) ;
@@ -192,7 +215,7 @@ ${inFileDisclaimer}
192215 await writeFile ( path . resolve ( destDir , 'index.ts' ) , `${ fileHeader } export * as Specification from './specification';` ) ;
193216}
194217
195- const srcFile = path . resolve ( schemaDir , 'workflow.json ' ) ;
218+ const srcFile = path . resolve ( schemaDir , 'workflow.yaml ' ) ;
196219const destFile = path . resolve ( definitionsDir , 'specification.ts' ) ;
197220
198221generate ( srcFile , destFile ) . then ( console . log . bind ( console ) ) . catch ( console . error . bind ( console ) ) ;
0 commit comments