Skip to content

Commit a26a6c4

Browse files
Artur KraftManweill
Artur Kraft
authored andcommitted
add possibility to use oneOf in response and body
1 parent d6e83e1 commit a26a6c4

File tree

5 files changed

+107
-13
lines changed

5 files changed

+107
-13
lines changed

example/oneOf.json

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"openapi": "3.0.3",
3+
"info": {
4+
"title": "Swagger Petstore - OpenAPI 3.0",
5+
"description": "",
6+
"version": "1.0.11"
7+
},
8+
"paths": {
9+
"/pet/": {
10+
"post": {
11+
"operationId": "createPet",
12+
"tags": ["test"],
13+
"requestBody": {
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"oneOf": [
18+
{
19+
"$ref": "#/components/schemas/Cat"
20+
},
21+
{
22+
"$ref": "#/components/schemas/Dog"
23+
}
24+
]
25+
}
26+
}
27+
}
28+
},
29+
"responses": {
30+
"200": {
31+
"description": "Updated",
32+
"content": {
33+
"application/json": {
34+
"schema": {
35+
"oneOf": [
36+
{
37+
"$ref": "#/components/schemas/Cat"
38+
},
39+
{
40+
"$ref": "#/components/schemas/Dog"
41+
}
42+
]
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
},
51+
"components": {
52+
"schemas": {
53+
"Dog": {
54+
"type": "object",
55+
"properties": {
56+
"bark": {
57+
"type": "boolean"
58+
},
59+
"breed": {
60+
"type": "string",
61+
"enum": [
62+
"Dingo",
63+
"Husky",
64+
"Retriever",
65+
"Shepherd"
66+
]
67+
}
68+
}
69+
},
70+
"Cat": {
71+
"type": "object",
72+
"properties": {
73+
"hunts": {
74+
"type": "boolean"
75+
},
76+
"age": {
77+
"type": "integer"
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}

example/swagger/oneOf.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// const { codegen } = require('swagger-axios-codegen')
2+
const { codegen } = require('../../dist/index.js')
3+
4+
codegen({
5+
methodNameMode: 'operationId',
6+
source: require('../oneOf.json'),
7+
outputDir: '.',
8+
fileName: 'oneOf.ts',
9+
strictNullChecks: false,
10+
modelMode: 'interface'
11+
})

src/requestCodegen/getRequestBody.ts

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export function getRequestBody(requestBody: IRequestBody) {
3030
} else if (reqBody.schema.$ref) {
3131
bodyType = refClassName(reqBody.schema.$ref)
3232
// console.log('propType', refClassName(p.schema.$ref))
33+
} else if (reqBody.schema.oneOf?.length > 0) {
34+
bodyType = reqBody.schema.oneOf.map((refType) => {
35+
const ref = refClassName(refType.$ref);
36+
imports.push(ref)
37+
return ref;
38+
}).join(" | ")
3339
}
3440
if (bodyType) {
3541
imports.push(bodyType)

src/requestCodegen/getResponseType.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@ export function getResponseType(reqProps: IRequestMethod, isV3: boolean): { resp
3939
const refType = refClassName(resSchema.items.$ref)
4040
isRef = true
4141
result = refType + '[]'
42-
} else {
42+
} else {
4343
const refType = toBaseType(resSchema.items.type, resSchema.items?.format)
4444
result = refType + '[]'
4545
}
4646
} else if (resSchema.$ref) {
4747
// 如果是引用对象
4848
result = refClassName(resSchema.$ref) || 'any'
4949
isRef = true
50+
} else if (resSchema.oneOf) {
51+
result = resSchema.oneOf.map((refType) => refClassName(refType.$ref)).join(" | ")
52+
isRef = true
5053
} else {
5154
result = checkType
5255
result = toBaseType(result, format)

src/swaggerInterfaces.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,11 @@ export interface IRequestMethod {
3131
[key: string]: {
3232
description: string
3333
// v2
34-
schema: {
35-
'$ref': string,
36-
'type'?: string,
37-
'items'?: IParameterItems,
38-
'format'?: string,
39-
},
34+
schema: Omit<ISchema, "properties">,
4035
// v3
4136
content: {
4237
[key: string]: {
43-
schema: {
44-
'$ref': string,
45-
'type'?: string,
46-
'items'?: IParameterItems,
47-
'format'?: string,
48-
}
38+
schema: Omit<ISchema, "properties">
4939
}
5040
}
5141
}
@@ -129,6 +119,7 @@ export interface ISchema {
129119
'type'?: string
130120
'items'?: IParameterItems
131121
'format'?: string,
122+
'oneOf'?: { $ref: string }[];
132123
'properties'?: { [key: string]: IParameterItems }
133124
}
134125

0 commit comments

Comments
 (0)