From fc36241f93309ede07d6c2e8b7c92b26803af1ac Mon Sep 17 00:00:00 2001 From: Artur Kraft Date: Mon, 21 Oct 2024 21:03:34 +0200 Subject: [PATCH] add possibility to use oneOf in response and body --- example/oneOf.json | 83 +++++++++++++++++++++++++++ example/swagger/oneOf.js | 11 ++++ src/requestCodegen/getRequestBody.ts | 6 ++ src/requestCodegen/getResponseType.ts | 5 +- src/swaggerInterfaces.ts | 15 +---- 5 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 example/oneOf.json create mode 100644 example/swagger/oneOf.js diff --git a/example/oneOf.json b/example/oneOf.json new file mode 100644 index 0000000..e890da2 --- /dev/null +++ b/example/oneOf.json @@ -0,0 +1,83 @@ +{ + "openapi": "3.0.3", + "info": { + "title": "Swagger Petstore - OpenAPI 3.0", + "description": "", + "version": "1.0.11" + }, + "paths": { + "/pet/": { + "post": { + "operationId": "createPet", + "tags": ["test"], + "requestBody": { + "content": { + "application/json": { + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/Cat" + }, + { + "$ref": "#/components/schemas/Dog" + } + ] + } + } + } + }, + "responses": { + "200": { + "description": "Updated", + "content": { + "application/json": { + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/Cat" + }, + { + "$ref": "#/components/schemas/Dog" + } + ] + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "Dog": { + "type": "object", + "properties": { + "bark": { + "type": "boolean" + }, + "breed": { + "type": "string", + "enum": [ + "Dingo", + "Husky", + "Retriever", + "Shepherd" + ] + } + } + }, + "Cat": { + "type": "object", + "properties": { + "hunts": { + "type": "boolean" + }, + "age": { + "type": "integer" + } + } + } + } + } +} \ No newline at end of file diff --git a/example/swagger/oneOf.js b/example/swagger/oneOf.js new file mode 100644 index 0000000..6181607 --- /dev/null +++ b/example/swagger/oneOf.js @@ -0,0 +1,11 @@ +// const { codegen } = require('swagger-axios-codegen') +const { codegen } = require('../../dist/index.js') + +codegen({ + methodNameMode: 'operationId', + source: require('../oneOf.json'), + outputDir: '.', + fileName: 'oneOf.ts', + strictNullChecks: false, + modelMode: 'interface' +}) diff --git a/src/requestCodegen/getRequestBody.ts b/src/requestCodegen/getRequestBody.ts index 384b952..55c8932 100644 --- a/src/requestCodegen/getRequestBody.ts +++ b/src/requestCodegen/getRequestBody.ts @@ -30,6 +30,12 @@ export function getRequestBody(requestBody: IRequestBody) { } else if (reqBody.schema.$ref) { bodyType = refClassName(reqBody.schema.$ref) // console.log('propType', refClassName(p.schema.$ref)) + } else if (reqBody.schema.oneOf?.length > 0) { + bodyType = reqBody.schema.oneOf.map((refType) => { + const ref = refClassName(refType.$ref); + imports.push(ref) + return ref; + }).join(" | ") } if (bodyType) { imports.push(bodyType) diff --git a/src/requestCodegen/getResponseType.ts b/src/requestCodegen/getResponseType.ts index f14a309..4d90aa7 100644 --- a/src/requestCodegen/getResponseType.ts +++ b/src/requestCodegen/getResponseType.ts @@ -39,7 +39,7 @@ export function getResponseType(reqProps: IRequestMethod, isV3: boolean): { resp const refType = refClassName(resSchema.items.$ref) isRef = true result = refType + '[]' - } else { + } else { const refType = toBaseType(resSchema.items.type, resSchema.items?.format) result = refType + '[]' } @@ -47,6 +47,9 @@ export function getResponseType(reqProps: IRequestMethod, isV3: boolean): { resp // 如果是引用对象 result = refClassName(resSchema.$ref) || 'any' isRef = true + } else if (resSchema.oneOf) { + result = resSchema.oneOf.map((refType) => refClassName(refType.$ref)).join(" | ") + isRef = true } else { result = checkType result = toBaseType(result, format) diff --git a/src/swaggerInterfaces.ts b/src/swaggerInterfaces.ts index 8ffa212..9ecfc68 100644 --- a/src/swaggerInterfaces.ts +++ b/src/swaggerInterfaces.ts @@ -31,21 +31,11 @@ export interface IRequestMethod { [key: string]: { description: string // v2 - schema: { - '$ref': string, - 'type'?: string, - 'items'?: IParameterItems, - 'format'?: string, - }, + schema: Omit, // v3 content: { [key: string]: { - schema: { - '$ref': string, - 'type'?: string, - 'items'?: IParameterItems, - 'format'?: string, - } + schema: Omit } } } @@ -128,6 +118,7 @@ export interface ISchema { 'type'?: string 'items'?: IParameterItems 'format'?: string, + 'oneOf'?: { $ref: string }[]; 'properties'?: { [key: string]: IParameterItems } }