Skip to content

Commit

Permalink
add possibility to use oneOf in response and body
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Kraft committed Oct 21, 2024
1 parent e08649e commit fc36241
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 13 deletions.
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

0 comments on commit fc36241

Please sign in to comment.