From 58ad29c86f5c54a2155e131b1a8937e2c5d03502 Mon Sep 17 00:00:00 2001 From: Akash Gupta Date: Wed, 4 Sep 2024 16:24:45 +0530 Subject: [PATCH 1/3] feat: add enum support for javascript and typescript --- src/generators/gen.ts | 10 +++++++--- src/generators/javascript/javascript.ts | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/generators/gen.ts b/src/generators/gen.ts index 4c8d8452..42c502c6 100644 --- a/src/generators/gen.ts +++ b/src/generators/gen.ts @@ -1,5 +1,5 @@ import { JSONSchema7 } from 'json-schema'; -import { parse, Schema, getPropertiesSchema, Type } from './ast.js'; +import { parse, Schema, getPropertiesSchema, Type, PrimitiveTypeSchema } from './ast.js'; import { javascript } from './javascript/index.js'; import { objc } from './objc/index.js'; import { swift } from './swift/index.js'; @@ -100,7 +100,11 @@ export declare type Generator< > = { namer: NamerOptions; setup: (options: GenOptions) => Promise; - generatePrimitive: (client: GeneratorClient, schema: Schema, parentPath: string) => Promise

; + generatePrimitive: ( + client: GeneratorClient, + schema: PrimitiveTypeSchema, + parentPath: string, + ) => Promise

; generateArray: ( client: GeneratorClient, schema: Schema, @@ -255,7 +259,7 @@ async function runGenerator< let p: P; if ([Type.ANY, Type.STRING, Type.BOOLEAN, Type.INTEGER, Type.NUMBER].includes(schema.type)) { // Primitives are any type that doesn't require generating a "subtype". - p = await generator.generatePrimitive(client, schema, parentPath); + p = await generator.generatePrimitive(client, schema as PrimitiveTypeSchema, parentPath); } else if (schema.type === Type.OBJECT) { // For objects, we need to recursively generate each property first. const properties: (P & BasePropertyContext)[] = []; diff --git a/src/generators/javascript/javascript.ts b/src/generators/javascript/javascript.ts index 6c008e5f..3770ec1a 100644 --- a/src/generators/javascript/javascript.ts +++ b/src/generators/javascript/javascript.ts @@ -81,7 +81,9 @@ export const javascript: Generator< }, generatePrimitive: async (client, schema) => { let type = 'any'; - if (schema.type === Type.STRING) { + if (schema.enum) { + type = schema.enum.map((e) => (typeof e === 'string' ? `'${e}'` : `${e}`)).join(' | '); + } else if (schema.type === Type.STRING) { type = 'string'; } else if (schema.type === Type.BOOLEAN) { type = 'boolean'; From f870475fbf41ef3fa84a77db72c6234ed8601de1 Mon Sep 17 00:00:00 2001 From: Akash Gupta Date: Wed, 4 Sep 2024 16:26:13 +0530 Subject: [PATCH 2/3] feat: add enum support for android --- src/generators/android/android.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/generators/android/android.ts b/src/generators/android/android.ts index d6a20175..c8508a87 100644 --- a/src/generators/android/android.ts +++ b/src/generators/android/android.ts @@ -57,7 +57,9 @@ export const android: Generator< generatePrimitive: async (client, schema, parentPath) => { let type = 'Object'; - if (schema.type === Type.STRING) { + if (schema.enum) { + type = schema.enum.map((e) => (typeof e === 'string' ? `'${e}'` : `${e}`)).join(' | '); + } else if (schema.type === Type.STRING) { type = 'String'; } else if (schema.type === Type.BOOLEAN) { type = 'Boolean'; From 002c242c1e71eb08f6a5e74d0cd93fc4035ff744 Mon Sep 17 00:00:00 2001 From: Akash Gupta Date: Thu, 5 Sep 2024 13:36:18 +0530 Subject: [PATCH 3/3] fix: revert enum for android --- src/generators/android/android.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/generators/android/android.ts b/src/generators/android/android.ts index c8508a87..d6a20175 100644 --- a/src/generators/android/android.ts +++ b/src/generators/android/android.ts @@ -57,9 +57,7 @@ export const android: Generator< generatePrimitive: async (client, schema, parentPath) => { let type = 'Object'; - if (schema.enum) { - type = schema.enum.map((e) => (typeof e === 'string' ? `'${e}'` : `${e}`)).join(' | '); - } else if (schema.type === Type.STRING) { + if (schema.type === Type.STRING) { type = 'String'; } else if (schema.type === Type.BOOLEAN) { type = 'Boolean';