Skip to content

Commit

Permalink
[Generator] Rendering string constants and anyOf with complex members (
Browse files Browse the repository at this point in the history
…#887)

- 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`

Signed-off-by: Theo Truong <[email protected]>
  • Loading branch information
nhtruong authored Oct 22, 2024
1 parent 0519950 commit 9288450
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
24 changes: 8 additions & 16 deletions api_generator/src/renderers/render_types/TypesFileRenderder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -46,21 +42,14 @@ 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'
if (schema.$ref != null) return this._container.ref_to_obj(schema.$ref)
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'
Expand All @@ -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 {
Expand All @@ -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)}`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import * as {{{name}}} from '{{{path}}}'
{{/imports}}

{{#types}}
export {{{declarative}}} {{{definition}}}
export type {{{name}}} = {{{definition}}}

{{/types}}

0 comments on commit 9288450

Please sign in to comment.