diff --git a/packages/wormhole/src/plugins/presets/payloadModifier/hepler.ts b/packages/wormhole/src/plugins/presets/payloadModifier/hepler.ts index 0478832..06b2d35 100644 --- a/packages/wormhole/src/plugins/presets/payloadModifier/hepler.ts +++ b/packages/wormhole/src/plugins/presets/payloadModifier/hepler.ts @@ -19,10 +19,21 @@ import type { // Convert Schema (custom spec) -> OpenAPI SchemaObject function toSchemaObject(base: SchemaObject, s: Schema): SchemaObject { const result: SchemaObject = { ...base } - + const cleanType = (schema: SchemaObject) => { + delete schema.type + delete schema.enum + delete schema.oneOf + delete schema.anyOf + delete schema.allOf + delete (schema as any).items + delete schema.properties + delete schema.required + return schema + } // Legacy union as array (treated as oneOf) if (Array.isArray(s)) { const baseOneOf = base.oneOf || [] + cleanType(result) result.oneOf = s.map((item, idx) => toSchemaObject(baseOneOf[idx] || {}, item)) return result } @@ -37,16 +48,19 @@ function toSchemaObject(base: SchemaObject, s: Schema): SchemaObject { if ((s as SchemaOneOf).oneOf) { const spec = s as SchemaOneOf const baseOneOf = base.oneOf || [] + cleanType(result) result.oneOf = spec.oneOf.map((item, idx) => toSchemaObject(baseOneOf[idx] || {}, item)) } if ((s as SchemaAnyOf).anyOf) { const spec = s as SchemaAnyOf const baseAnyOf = base.anyOf || [] + cleanType(result) result.anyOf = spec.anyOf.map((item, idx) => toSchemaObject(baseAnyOf[idx] || {}, item)) } if ((s as SchemaAllOf).allOf) { const spec = s as SchemaAllOf const baseAllOf = base.allOf || [] + cleanType(result) result.allOf = spec.allOf.map((item, idx) => toSchemaObject(baseAllOf[idx] || {}, item)) } @@ -55,7 +69,7 @@ function toSchemaObject(base: SchemaObject, s: Schema): SchemaObject { const spec = s as SchemaEnum result.enum = spec.enum if (spec.type) { - result.type = spec.type + result.type = spec.type as any } } diff --git a/packages/wormhole/src/plugins/presets/payloadModifier/type.ts b/packages/wormhole/src/plugins/presets/payloadModifier/type.ts index a55666c..217d28e 100644 --- a/packages/wormhole/src/plugins/presets/payloadModifier/type.ts +++ b/packages/wormhole/src/plugins/presets/payloadModifier/type.ts @@ -1,5 +1,5 @@ export type ModifierScope = 'params' | 'pathParams' | 'data' | 'response' -export type SchemaPrimitive = 'number' | 'string' | 'boolean' | 'undefined' | 'null' | 'unknown' | 'any' | 'never' +export type SchemaPrimitive = 'number' | 'string' | 'boolean' | 'undefined' | 'null' | 'unknown' | 'any' | 'never' | ({} & string) /** * 表示数组类型 @@ -23,7 +23,7 @@ export interface SchemaReference { */ export interface SchemaEnum { enum: Array - type?: 'string' | 'number' | 'integer' | 'boolean' | 'null' + type?: SchemaPrimitive } /** diff --git a/packages/wormhole/src/plugins/presets/rename.ts b/packages/wormhole/src/plugins/presets/rename.ts index f57bd6c..5d15649 100644 --- a/packages/wormhole/src/plugins/presets/rename.ts +++ b/packages/wormhole/src/plugins/presets/rename.ts @@ -32,7 +32,7 @@ export interface RenameConfig { * Custom transformation function * Will be applied before style transformation */ - transform?: (apiDescriptor: ApiDescriptor) => string + transform?: (apiDescriptor: ApiDescriptor, value: string) => string } function toCamelCase(str: string): string { @@ -69,7 +69,7 @@ function applyRenameRule(value: string, config: RenameConfig, apiDescriptor: Api } if (config.transform) { - value = config.transform(apiDescriptor) + value = config.transform(apiDescriptor, value) } if (!config.style) { diff --git a/packages/wormhole/test/plugins/payloadModifier.spec.ts b/packages/wormhole/test/plugins/payloadModifier.spec.ts index 97a5331..05af761 100644 --- a/packages/wormhole/test/plugins/payloadModifier.spec.ts +++ b/packages/wormhole/test/plugins/payloadModifier.spec.ts @@ -11,7 +11,7 @@ describe('payloadModifier plugin tests', () => { it('modifies query/path parameters and removes matched ones', () => { const handleApi = getHandleApi([ - { scope: 'params', match: 'age', handler: () => ({ required: true, value: 'string' }) }, + { scope: 'params', match: 'age', handler: () => ({ required: true, value: ['string', 'number', 'boolean'] }) }, { scope: 'params', match: 'debug', handler: () => null }, { scope: 'pathParams', match: 'id', handler: () => ({ required: false, value: 'string' }) }, ]) @@ -21,7 +21,7 @@ describe('payloadModifier plugin tests', () => { method: 'get', parameters: [ { name: 'id', in: 'path', required: true, schema: { type: 'integer' } }, - { name: 'age', in: 'query', required: false, schema: { type: 'integer' } }, + { name: 'age', in: 'query', required: false, schema: { type: 'integer', description: 'hello age' } }, { name: 'debug', in: 'query', required: false, schema: { type: 'boolean' } }, { name: 'q', in: 'query', required: false, schema: { type: 'string' } }, ], @@ -32,7 +32,10 @@ describe('payloadModifier plugin tests', () => { const result = handleApi(api)! // age converted to string and required true const ageParam = result.parameters!.find(p => p.in === 'query' && p.name === 'age')! - expect((ageParam.schema as SchemaObject)?.type).toBe('string') + expect(ageParam.schema).toEqual({ + description: 'hello age', + oneOf: [{ type: 'string' }, { type: 'number' }, { type: 'boolean' }], + }) expect(ageParam.required).toBe(true) // debug removed diff --git a/packages/wormhole/typings/plugins.d.ts b/packages/wormhole/typings/plugins.d.ts index f2c2b10..1bf8946 100644 --- a/packages/wormhole/typings/plugins.d.ts +++ b/packages/wormhole/typings/plugins.d.ts @@ -309,7 +309,7 @@ export declare function filterApiDescriptor(apiDescriptor: ApiDescriptor, config export declare function apiFilter(config: FilterApiConfig | FilterApiConfig[]): ApiPlugin; export declare function importType(config: Record): ApiPlugin; export type ModifierScope = "params" | "pathParams" | "data" | "response"; -export type SchemaPrimitive = "number" | "string" | "boolean" | "undefined" | "null" | "unknown" | "any" | "never"; +export type SchemaPrimitive = "number" | "string" | "boolean" | "undefined" | "null" | "unknown" | "any" | "never" | ({} & string); /** * 表示数组类型 */ @@ -329,7 +329,7 @@ export interface SchemaReference { */ export interface SchemaEnum { enum: Array; - type?: "string" | "number" | "integer" | "boolean" | "null"; + type?: SchemaPrimitive; } /** * 组合类型表示(与/或/交叉) @@ -398,7 +398,7 @@ export interface RenameConfig { * Custom transformation function * Will be applied before style transformation */ - transform?: (apiDescriptor: ApiDescriptor) => string; + transform?: (apiDescriptor: ApiDescriptor, value: string) => string; } /** * Creates a rename plugin that transforms API descriptors