Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possibility to use oneOf in response and body #201

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions example/oneOf.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}
11 changes: 11 additions & 0 deletions example/swagger/oneOf.js
Original file line number Diff line number Diff line change
@@ -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'
})
6 changes: 6 additions & 0 deletions src/requestCodegen/getRequestBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/requestCodegen/getResponseType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ 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 + '[]'
}
} else if (resSchema.$ref) {
// 如果是引用对象
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)
Expand Down
15 changes: 3 additions & 12 deletions src/swaggerInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,11 @@ export interface IRequestMethod {
[key: string]: {
description: string
// v2
schema: {
'$ref': string,
'type'?: string,
'items'?: IParameterItems,
'format'?: string,
},
schema: Omit<ISchema, "properties">,
// v3
content: {
[key: string]: {
schema: {
'$ref': string,
'type'?: string,
'items'?: IParameterItems,
'format'?: string,
}
schema: Omit<ISchema, "properties">
}
}
}
Expand Down Expand Up @@ -128,6 +118,7 @@ export interface ISchema {
'type'?: string
'items'?: IParameterItems
'format'?: string,
'oneOf'?: { $ref: string }[];
'properties'?: { [key: string]: IParameterItems }
}

Expand Down
Loading