Skip to content

Commit 8c4634a

Browse files
committed
🔧 fix: duplicate object reference
1 parent 90816d9 commit 8c4634a

File tree

4 files changed

+47
-34
lines changed

4 files changed

+47
-34
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.1.14 - 9 Oct 2024
2+
Bug fix:
3+
- Fix duplicate object reference
4+
15
# 1.1.2 - 5 Sep 2024
26
Feature:
37
- add provenance publish

bun.lockb

440 Bytes
Binary file not shown.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elysiajs/swagger",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "Plugin for Elysia to auto-generate Swagger page",
55
"author": {
66
"name": "saltyAom",
@@ -62,7 +62,7 @@
6262
"devDependencies": {
6363
"@apidevtools/swagger-parser": "^10.1.0",
6464
"@types/bun": "1.1.6",
65-
"elysia": ">= 1.1.0-rc.2",
65+
"elysia": "1.1.18",
6666
"eslint": "9.6.0",
6767
"tsup": "^8.1.0",
6868
"typescript": "^5.5.3"

src/utils.ts

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ const mapTypesResponse = (
6969

7070
const responses: Record<string, OpenAPIV3.MediaTypeObject> = {}
7171

72-
for (const type of types)
72+
for (const type of types) {
73+
// console.log(schema)
74+
7375
responses[type] = {
7476
schema:
7577
typeof schema === 'string'
@@ -78,6 +80,7 @@ const mapTypesResponse = (
7880
}
7981
: { ...(schema as any) }
8082
}
83+
}
8184

8285
return responses
8386
}
@@ -101,6 +104,12 @@ export const generateOperationId = (method: string, paths: string) => {
101104
return operationId
102105
}
103106

107+
const cloneHook = <T>(hook: T) => {
108+
if (!hook) return
109+
110+
return { ...hook }
111+
}
112+
104113
export const registerSchemaPath = ({
105114
schema,
106115
path,
@@ -114,7 +123,7 @@ export const registerSchemaPath = ({
114123
method: HTTPMethod
115124
hook?: LocalHook<any, any, any, any, any, any, any>
116125
models: Record<string, TSchema>
117-
}) => {
126+
}) => {
118127
const contentType = hook?.type ?? [
119128
'application/json',
120129
'multipart/form-data',
@@ -126,13 +135,13 @@ export const registerSchemaPath = ({
126135
const contentTypes =
127136
typeof contentType === 'string'
128137
? [contentType]
129-
: contentType ?? ['application/json']
138+
: (contentType ?? ['application/json'])
130139

131-
const bodySchema = hook?.body
132-
const paramsSchema = hook?.params
133-
const headerSchema = hook?.headers
134-
const querySchema = hook?.query
135-
let responseSchema = hook?.response as unknown as OpenAPIV3.ResponsesObject
140+
const bodySchema = cloneHook(hook?.body)
141+
const paramsSchema = cloneHook(hook?.params)
142+
const headerSchema = cloneHook(hook?.headers)
143+
const querySchema = cloneHook(hook?.query)
144+
let responseSchema: OpenAPIV3.ResponsesObject = cloneHook(hook?.response)
136145

137146
if (typeof responseSchema === 'object') {
138147
if (Kind in responseSchema) {
@@ -292,36 +301,36 @@ export const registerSchemaPath = ({
292301
}
293302

294303
export const filterPaths = (
295-
paths: Record<string, any>,
296-
docsPath: string,
297-
{
298-
excludeStaticFile = true,
299-
exclude = []
300-
}: {
301-
excludeStaticFile: boolean
302-
exclude: (string | RegExp)[]
303-
}
304+
paths: Record<string, any>,
305+
docsPath: string,
306+
{
307+
excludeStaticFile = true,
308+
exclude = []
309+
}: {
310+
excludeStaticFile: boolean
311+
exclude: (string | RegExp)[]
312+
}
304313
) => {
305314
const newPaths: Record<string, any> = {}
306315

307-
// exclude docs path and OpenAPI json path
308-
const excludePaths = [`/${docsPath}`, `/${docsPath}/json`].map((p) =>
316+
// exclude docs path and OpenAPI json path
317+
const excludePaths = [`/${docsPath}`, `/${docsPath}/json`].map((p) =>
309318
normalize(p)
310319
)
311320

312-
for (const [key, value] of Object.entries(paths))
313-
if (
314-
!exclude.some((x) => {
315-
if (typeof x === 'string') return key === x
316-
317-
return x.test(key)
318-
}) &&
319-
!excludePaths.includes(key) &&
320-
!key.includes('*') &&
321-
(excludeStaticFile ? !key.includes('.') : true)
322-
) {
323-
Object.keys(value).forEach((method) => {
324-
const schema = value[method]
321+
for (const [key, value] of Object.entries(paths))
322+
if (
323+
!exclude.some((x) => {
324+
if (typeof x === 'string') return key === x
325+
326+
return x.test(key)
327+
}) &&
328+
!excludePaths.includes(key) &&
329+
!key.includes('*') &&
330+
(excludeStaticFile ? !key.includes('.') : true)
331+
) {
332+
Object.keys(value).forEach((method) => {
333+
const schema = value[method]
325334

326335
if (key.includes('{')) {
327336
if (!schema.parameters) schema.parameters = []

0 commit comments

Comments
 (0)