@@ -19,6 +19,7 @@ import { promises as fsPromises } from 'fs';
19
19
import * as path from 'path' ;
20
20
import { fileHeader , inFileDisclaimer } from './consts' ;
21
21
import { definitionsDir , isObject , reset , schemaDir , toPascalCase } from './utils' ;
22
+ import * as yaml from 'js-yaml' ;
22
23
23
24
const { writeFile, readFile } = fsPromises ;
24
25
@@ -60,6 +61,7 @@ const metadataProperties = ['title', 'description', 'default', 'type'];
60
61
* 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)
61
62
* - adds missing type:object properties // not necessary ?
62
63
* - adds missing titles to objects
64
+ * - removes extra titles on value types
63
65
* @param schema The schema to embellish
64
66
* @param path The current path of the schema relative to the original schema
65
67
* @param parentTitle The title of the parent object, if any
@@ -74,7 +76,7 @@ function prepareSchema(schema: any, path: string[] = ['#'], parentTitle: string
74
76
}
75
77
const newSchema = JSON . parse ( JSON . stringify ( schema ) ) ;
76
78
const parent = path . slice ( - 1 ) [ 0 ] ;
77
- const schemaKeys = Object . keys ( newSchema ) ;
79
+ let schemaKeys = Object . keys ( newSchema ) ;
78
80
const isItemWithAdditionalProperties =
79
81
parent === 'additionalProperties' && path . slice ( - 2 ) [ 0 ] === 'items' && newSchema . properties ; // only "useful" for SwitchTask.Switch.Cases
80
82
if ( ! structuralObjectProperties . includes ( parent ) || isItemWithAdditionalProperties ) {
@@ -86,6 +88,16 @@ function prepareSchema(schema: any, path: string[] = ['#'], parentTitle: string
86
88
if ( newSchema . title ) {
87
89
parentTitle = newSchema . title ;
88
90
}
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
+ }
89
101
if (
90
102
! newSchema . title &&
91
103
( ! 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 {
169
181
*/
170
182
async function generate ( srcFile : string , destFile : string ) : Promise < void > {
171
183
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
+ } ,
174
194
bannerComment : `${ fileHeader }
175
195
${ inFileDisclaimer }
176
196
@@ -181,8 +201,11 @@ ${inFileDisclaimer}
181
201
//unreachableDefinitions: true,
182
202
} ;
183
203
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
+ ) ;
186
209
schema = mutateSchema ( schema ) ;
187
210
//await writeFile(srcFile.replace('workflow', '__mutated_workflow'), JSON.stringify(schema, null, 2));
188
211
const declarations = await compile ( schema , 'Workflow' , options ) ;
@@ -192,7 +215,7 @@ ${inFileDisclaimer}
192
215
await writeFile ( path . resolve ( destDir , 'index.ts' ) , `${ fileHeader } export * as Specification from './specification';` ) ;
193
216
}
194
217
195
- const srcFile = path . resolve ( schemaDir , 'workflow.json ' ) ;
218
+ const srcFile = path . resolve ( schemaDir , 'workflow.yaml ' ) ;
196
219
const destFile = path . resolve ( definitionsDir , 'specification.ts' ) ;
197
220
198
221
generate ( srcFile , destFile ) . then ( console . log . bind ( console ) ) . catch ( console . error . bind ( console ) ) ;
0 commit comments