Skip to content

Commit 49f83e7

Browse files
committed
openapi3-ts v3.0.0
1 parent fbbdc96 commit 49f83e7

File tree

4 files changed

+80
-42
lines changed

4 files changed

+80
-42
lines changed

package-lock.json

Lines changed: 58 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"lodash.capitalize": "^4.2.1",
3333
"lodash.merge": "^4.6.2",
3434
"lodash.startcase": "^4.4.0",
35-
"openapi3-ts": "^2.0.2",
35+
"openapi3-ts": "^3.0.0",
3636
"path-to-regexp": "^6.2.1",
3737
"reflect-metadata": "^0.1.13",
3838
"tslib": "^2.4.1"

src/decorators.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import _merge from 'lodash.merge'
2-
import {
3-
OperationObject,
4-
ReferenceObject,
5-
ResponsesObject,
6-
SchemaObject,
7-
} from 'openapi3-ts'
2+
import { OperationObject, ReferenceObject, ResponsesObject } from 'openapi3-ts'
83
import 'reflect-metadata'
94

105
import { getContentType, getStatusCode, IRoute } from './index'
@@ -115,9 +110,7 @@ export function ResponseSchema(
115110
const reference: ReferenceObject = {
116111
$ref: `#/components/schemas/${responseSchemaName}`,
117112
}
118-
const schema: SchemaObject = isArray
119-
? { items: reference, type: 'array' }
120-
: reference
113+
const schema = isArray ? { items: reference, type: 'array' } : reference
121114
const responses: ResponsesObject = {
122115
[statusCode]: {
123116
content: {

src/generateSpec.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function getFullPath(route: IRoute): string {
3232
*/
3333
export function getOperation(
3434
route: IRoute,
35-
schemas: { [p: string]: oa.SchemaObject }
35+
schemas: { [p: string]: oa.SchemaObject | oa.ReferenceObject }
3636
): oa.OperationObject {
3737
const operation: oa.OperationObject = {
3838
operationId: getOperationId(route),
@@ -52,9 +52,9 @@ export function getOperation(
5252
([_, value]) => value && (value.length || Object.keys(value).length)
5353
)
5454
.reduce((acc, [key, value]) => {
55-
acc[key] = value
55+
acc[key as keyof oa.OperationObject] = value
5656
return acc
57-
}, {} as unknown as oa.OperationObject)
57+
}, {} as oa.OperationObject)
5858

5959
return applyOpenAPIDecorator(cleanedOperation, route)
6060
}
@@ -71,7 +71,7 @@ export function getOperationId(route: IRoute): string {
7171
*/
7272
export function getPaths(
7373
routes: IRoute[],
74-
schemas: { [p: string]: oa.SchemaObject }
74+
schemas: { [p: string]: oa.SchemaObject | oa.ReferenceObject }
7575
): oa.PathObject {
7676
const routePaths = routes.map((route) => ({
7777
[getFullPath(route)]: {
@@ -156,7 +156,7 @@ export function getPathParams(route: IRoute): oa.ParameterObject[] {
156156
*/
157157
export function getQueryParams(
158158
route: IRoute,
159-
schemas: { [p: string]: oa.SchemaObject }
159+
schemas: { [p: string]: oa.SchemaObject | oa.ReferenceObject }
160160
): oa.ParameterObject[] {
161161
const queries: oa.ParameterObject[] = route.params
162162
.filter((p) => p.type === 'query')
@@ -177,15 +177,17 @@ export function getQueryParams(
177177
const paramSchemaName = paramSchema.$ref.split('/').pop() || ''
178178
const currentSchema = schemas[paramSchemaName]
179179

180-
for (const [name, schema] of Object.entries(
181-
currentSchema?.properties || {}
182-
)) {
183-
queries.push({
184-
in: 'query',
185-
name,
186-
required: currentSchema.required?.includes(name),
187-
schema,
188-
})
180+
if (oa.isSchemaObject(currentSchema)) {
181+
for (const [name, schema] of Object.entries(
182+
currentSchema?.properties || {}
183+
)) {
184+
queries.push({
185+
in: 'query',
186+
name,
187+
required: currentSchema.required?.includes(name),
188+
schema,
189+
})
190+
}
189191
}
190192
}
191193
return queries
@@ -243,8 +245,9 @@ export function getRequestBody(route: IRoute): oa.RequestBodyObject | void {
243245
const bodyMeta = route.params.find((d) => d.type === 'body')
244246
if (bodyMeta) {
245247
const bodySchema = getParamSchema(bodyMeta)
246-
const { $ref } =
248+
const items =
247249
'items' in bodySchema && bodySchema.items ? bodySchema.items : bodySchema
250+
const $ref = oa.isReferenceObject(items) ? items.$ref : ''
248251

249252
return {
250253
content: {
@@ -308,7 +311,7 @@ export function getResponses(route: IRoute): oa.ResponsesObject {
308311
*/
309312
export function getSpec(
310313
routes: IRoute[],
311-
schemas: { [p: string]: oa.SchemaObject }
314+
schemas: { [p: string]: oa.SchemaObject | oa.ReferenceObject }
312315
): oa.OpenAPIObject {
313316
return {
314317
components: { schemas: {} },

0 commit comments

Comments
 (0)