Skip to content

Commit 87dde06

Browse files
committed
feat: extend to support type-builder
1 parent 461de1c commit 87dde06

File tree

10 files changed

+33
-13
lines changed

10 files changed

+33
-13
lines changed

integration-tests-definitions/todo-lists.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ components:
171171
created:
172172
type: string
173173
format: date-time
174+
x-alpha-transform:
175+
fn: (it) => new Date(it)
176+
type: Date
174177
updated:
175178
type: string
176179
format: date-time

integration-tests/typescript-angular/src/generated/todo-lists.yaml/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type t_CreateUpdateTodoList = {
1212
}
1313

1414
export type t_TodoList = {
15-
created: string
15+
created: Date
1616
id: string
1717
incompleteItemCount: number
1818
name: string

integration-tests/typescript-axios/src/generated/todo-lists.yaml/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type t_CreateUpdateTodoList = {
1212
}
1313

1414
export type t_TodoList = {
15-
created: string
15+
created: Date
1616
id: string
1717
incompleteItemCount: number
1818
name: string

integration-tests/typescript-fetch/src/generated/todo-lists.yaml/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type t_CreateUpdateTodoList = {
1212
}
1313

1414
export type t_TodoList = {
15-
created: string
15+
created: Date
1616
id: string
1717
incompleteItemCount: number
1818
name: string

integration-tests/typescript-koa/src/generated/todo-lists.yaml/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type t_Error = {
88
}
99

1010
export type t_TodoList = {
11-
created: string
11+
created: Date
1212
id: string
1313
incompleteItemCount: number
1414
name: string

integration-tests/typescript-koa/src/generated/todo-lists.yaml/schemas.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ export const s_TodoList = z.object({
1111
name: z.string(),
1212
totalItemCount: z.coerce.number(),
1313
incompleteItemCount: z.coerce.number(),
14-
created: z.string().datetime({ offset: true }),
14+
created: z
15+
.string()
16+
.datetime({ offset: true })
17+
.transform((it) => new Date(it)),
1518
updated: z.string().datetime({ offset: true }),
1619
})
1720

packages/openapi-code-generator/src/core/openapi-types-normalized.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ export interface IRModelBase {
99
nullable: boolean /* false */
1010
readOnly: boolean /* false */
1111

12-
"x-alpha-transform"?: string | undefined
12+
"x-alpha-transform"?:
13+
| {
14+
fn?: string | undefined
15+
type?: string | undefined
16+
}
17+
| undefined
1318
}
1419

1520
export type IRModelNumericFormat = "int32" | "int64" | "float" | "double"

packages/openapi-code-generator/src/core/openapi-types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,13 @@ export interface Schema {
249249
deprecated?: boolean | undefined
250250
// xml?: XML | undefined
251251

252-
// TODO: not yet supported by type-builder or joi
253-
"x-alpha-transform"?: string | undefined
252+
// TODO: not yet supported by joi
253+
"x-alpha-transform"?:
254+
| {
255+
fn?: string | undefined
256+
type?: string | undefined
257+
}
258+
| undefined
254259
}
255260

256261
export interface Discriminator {

packages/openapi-code-generator/src/typescript/common/schema-builders/abstract-schema-builder.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@ export abstract class AbstractSchemaBuilder<
180180
oneOf: [],
181181
required: [],
182182
anyOf: [parameter.schema, parameter.schema.items],
183-
"x-alpha-transform": ((it: unknown) =>
184-
Array.isArray(it) || it === undefined ? it : [it]).toString(),
183+
"x-alpha-transform": {
184+
fn: ((it: unknown) =>
185+
Array.isArray(it) || it === undefined ? it : [it]).toString(),
186+
},
185187
}
186188
} else {
187189
model.properties[parameter.name] = parameter.schema
@@ -326,8 +328,8 @@ export abstract class AbstractSchemaBuilder<
326328

327329
result = required ? this.required(result) : this.optional(result)
328330

329-
if (model["x-alpha-transform"]) {
330-
result = this.transform(result, model["x-alpha-transform"])
331+
if (model["x-alpha-transform"]?.fn) {
332+
result = this.transform(result, model["x-alpha-transform"]?.fn)
331333
}
332334

333335
return result

packages/openapi-code-generator/src/typescript/common/type-builder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ export class TypeBuilder implements ICompilable {
145145
result.push(...schemaObject.anyOf.flatMap(this.schemaObjectToTypes))
146146
}
147147

148-
if (result.length === 0) {
148+
if (schemaObject["x-alpha-transform"]?.type) {
149+
result.push(schemaObject["x-alpha-transform"]?.type)
150+
} else if (result.length === 0) {
149151
switch (schemaObject.type) {
150152
case "array": {
151153
result.push(array(this.schemaObjectToType(schemaObject.items)))

0 commit comments

Comments
 (0)