Skip to content

Commit 72019bb

Browse files
committed
improv: support union/intersection in schema name
1 parent b917d4a commit 72019bb

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

src/resolve.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ export function resolve (
160160
const subjectType = type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0]
161161
const name = type.getSymbol()?.getEscapedName() !== '__type' ? type.getSymbol()?.getEscapedName() : helperName
162162
typeName = `${name}_${retrieveTypeName(subjectType)}`
163+
} else if ((type.getAliasTypeArguments().length === 1 || type.getTypeArguments().length === 1) && (type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0])?.isUnion() === true) { // i.e. Serialized<WorkerDatasource | ProxyDatasource> -> Serialized_Union_WorkerDatasource_ProxyDatasource
164+
const subjectType = type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0]
165+
const name = type.getSymbol()?.getEscapedName() !== '__type' ? type.getSymbol()?.getEscapedName() : helperName
166+
typeName = `${name}_Union_${subjectType.getUnionTypes().map(t => retrieveTypeName(t)).join('_')}`
167+
} else if ((type.getAliasTypeArguments().length === 1 || type.getTypeArguments().length === 1) && (type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0])?.isIntersection() === true) { // i.e. Serialized<WorkerDatasource & ProxyDatasource> -> Serialized_Intersection_WorkerDatasource_ProxyDatasource
168+
const subjectType = type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0]
169+
const name = type.getSymbol()?.getEscapedName() !== '__type' ? type.getSymbol()?.getEscapedName() : helperName
170+
typeName = `${name}_Intersection_${subjectType.getIntersectionTypes().map(t => retrieveTypeName(t)).join('_')}`
163171
} else if (isTypeIdentifier(type) === false) { // For other and anonymous types, don't use ref
164172
return resolveObjectType(type, spec)
165173
}

tests/fixture/response-object.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ type SuccessResponse<T extends any> = { data: T }
55
type Entity = {
66
name: string
77
}
8+
type Entity2 = {
9+
name: number
10+
}
811

912
@Route('/my-3nd-controller/')
1013
@Security({ company: [] })
@@ -13,6 +16,14 @@ export class My3ndController {
1316
public async list (): Promise<SuccessResponse<Entity[]>> {
1417
return { data: [{ name: 'foo' }] }
1518
}
19+
@Get('/example')
20+
public async getIntersection (): Promise<SuccessResponse<Entity & { example: string }>> {
21+
return { data: { name: 'foo', example: '' } }
22+
}
23+
@Get('/union')
24+
public async getUnion (): Promise<SuccessResponse<Entity| Entity2>> {
25+
return { data: { name: 'foo' } }
26+
}
1627
@Post('/')
1728
public async create (): Promise<SuccessResponse<Entity>> {
1829
return { data: { name: 'foo' } }

tests/snapshots/openapi.test.ts.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,54 @@ Generated by [AVA](https://avajs.dev).
8282
],␊
8383
"operationId": "Update"␊
8484
}␊
85+
},␊
86+
"/my-3nd-controller/example": {␊
87+
"get": {␊
88+
"parameters": [],␊
89+
"responses": {␊
90+
"200": {␊
91+
"description": "Ok",␊
92+
"content": {␊
93+
"application/json": {␊
94+
"schema": {␊
95+
"$ref": "#/components/schemas/SuccessResponse_Intersection_Entity_example-string"␊
96+
}␊
97+
}␊
98+
}␊
99+
}␊
100+
},␊
101+
"tags": [],␊
102+
"security": [␊
103+
{␊
104+
"company": []␊
105+
}␊
106+
],␊
107+
"operationId": "GetIntersection"␊
108+
}␊
109+
},␊
110+
"/my-3nd-controller/union": {␊
111+
"get": {␊
112+
"parameters": [],␊
113+
"responses": {␊
114+
"200": {␊
115+
"description": "Ok",␊
116+
"content": {␊
117+
"application/json": {␊
118+
"schema": {␊
119+
"$ref": "#/components/schemas/SuccessResponse_Union_Entity_Entity2"␊
120+
}␊
121+
}␊
122+
}␊
123+
}␊
124+
},␊
125+
"tags": [],␊
126+
"security": [␊
127+
{␊
128+
"company": []␊
129+
}␊
130+
],␊
131+
"operationId": "GetUnion"␊
132+
}␊
85133
}␊
86134
},␊
87135
"components": {␊
@@ -111,6 +159,61 @@ Generated by [AVA](https://avajs.dev).
111159
"data"␊
112160
]␊
113161
},␊
162+
"SuccessResponse_Intersection_Entity_example-string": {␊
163+
"type": "object",␊
164+
"properties": {␊
165+
"data": {␊
166+
"allOf": [␊
167+
{␊
168+
"$ref": "#/components/schemas/Entity"␊
169+
},␊
170+
{␊
171+
"type": "object",␊
172+
"properties": {␊
173+
"example": {␊
174+
"type": "string"␊
175+
}␊
176+
},␊
177+
"required": [␊
178+
"example"␊
179+
]␊
180+
}␊
181+
]␊
182+
}␊
183+
},␊
184+
"required": [␊
185+
"data"␊
186+
]␊
187+
},␊
188+
"Entity2": {␊
189+
"type": "object",␊
190+
"properties": {␊
191+
"name": {␊
192+
"type": "number"␊
193+
}␊
194+
},␊
195+
"required": [␊
196+
"name"␊
197+
]␊
198+
},␊
199+
"SuccessResponse_Union_Entity_Entity2": {␊
200+
"type": "object",␊
201+
"properties": {␊
202+
"data": {␊
203+
"oneOf": [␊
204+
{␊
205+
"$ref": "#/components/schemas/Entity"␊
206+
},␊
207+
{␊
208+
"$ref": "#/components/schemas/Entity2"␊
209+
}␊
210+
]␊
211+
}␊
212+
},␊
213+
"required": [␊
214+
"data"␊
215+
]␊
216+
},␊
114217
"SuccessResponse_Entity": {␊
115218
"type": "object",␊
116219
"properties": {␊

tests/snapshots/openapi.test.ts.snap

141 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)