From aa73a14db6f899971cf17defc3e741b57a208b39 Mon Sep 17 00:00:00 2001 From: Theo Truong Date: Tue, 22 Oct 2024 09:49:04 -0600 Subject: [PATCH] [Generator] Rendering string constants and anyOf with complex members - Added ability to render string constants - Added ability to render anyOf objects where members can be anonymous compound objects (allOf) - All type definitions are now `type` instead of a mix of `type` and `interface` --- .../render_types/TypesFileRenderder.ts | 24 +++++++------------ .../templates/type.containers.mustache | 2 +- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/api_generator/src/renderers/render_types/TypesFileRenderder.ts b/api_generator/src/renderers/render_types/TypesFileRenderder.ts index ed256b992..944de2f65 100644 --- a/api_generator/src/renderers/render_types/TypesFileRenderder.ts +++ b/api_generator/src/renderers/render_types/TypesFileRenderder.ts @@ -33,11 +33,7 @@ export default class TypesFileRenderder extends BaseRenderer { const con = this._container return { is_function: con.is_function, - types: _.entries(con.schemas).map(([name, schema]) => { - const definition = this.#render_schema(schema) - const declarative = this.#is_interface(definition) ? `interface ${name}` : `type ${name} =` - return { declarative, definition } - }), + types: _.entries(con.schemas).map(([name, schema]) => ({ name, definition: this.#render_schema(schema) })), imports: Array.from(con.referenced_containers) .sort((a, b) => a.import_name.localeCompare(b.import_name)) .map(container => { @@ -46,14 +42,6 @@ export default class TypesFileRenderder extends BaseRenderer { } } - #is_interface (definition: string): boolean { - if (definition.includes('} |')) return false - if (definition.includes('| {')) return false - if (definition.includes('} &')) return false - if (definition.includes('& {')) return false - return definition.includes('}') - } - #render_schema (schema: Schema): string { if (Array.isArray(schema.items)) throw new Error('Unhandled positioned array schema') if (typeof schema === 'object' && _.keys(schema).length === 0) return 'any' @@ -61,6 +49,7 @@ export default class TypesFileRenderder extends BaseRenderer { if (schema.items != null) return `${this.#render_schema(schema.items as Schema)}[]` if (schema.type === 'array') return 'any[]' if (schema.enum != null) return schema.enum.map(str => `'${str as string}'`).join(' | ') + if (schema.type === 'string' && schema.const != null) return `'${schema.const as string}'` if (schema.type === 'string') return 'string' if (schema.type === 'number') return 'number' if (schema.type === 'integer') return 'number' @@ -75,7 +64,10 @@ export default class TypesFileRenderder extends BaseRenderer { #render_anyOf (schema: Schema): string { const schemas = schema.oneOf ?? schema.anyOf ?? [] - return schemas.map((sch) => this.#render_schema(sch as Schema)).join(' | ') + return schemas.map((sch) => { + const render = this.#render_schema(sch as Schema) + return render.includes(' & ') ? `(${render})` : render + }).join(' | ') } #render_allOf (schemas: Schema[]): string { @@ -92,10 +84,10 @@ export default class TypesFileRenderder extends BaseRenderer { }, {}) const inline_schemas_render = this.#render_schema(compound_inline) - const named_schemas_render = named_schemas.map(schema => this.#render_schema(schema)).join(', ') + const named_schemas_render = named_schemas.map(schema => this.#render_schema(schema)).join(' & ') if (named_schemas.length === 0) return inline_schemas_render - if (inline_schemas_render.includes('{')) return `extends ${named_schemas_render} ${inline_schemas_render}` + if (inline_schemas_render.includes('{')) return `${named_schemas_render} & ${inline_schemas_render}` return `${named_schemas_render} & ${this.#render_simple_obj(compound_inline)}` } diff --git a/api_generator/src/renderers/templates/type.containers.mustache b/api_generator/src/renderers/templates/type.containers.mustache index 2e9780cd2..5abfbf7ff 100644 --- a/api_generator/src/renderers/templates/type.containers.mustache +++ b/api_generator/src/renderers/templates/type.containers.mustache @@ -9,6 +9,6 @@ import * as {{{name}}} from '{{{path}}}' {{/imports}} {{#types}} -export {{{declarative}}} {{{definition}}} +export type {{{name}}} = {{{definition}}} {{/types}} \ No newline at end of file