Skip to content

Commit

Permalink
🔧 fix: duplicate object reference
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Oct 8, 2024
1 parent 90816d9 commit 8c4634a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.14 - 9 Oct 2024
Bug fix:
- Fix duplicate object reference

# 1.1.2 - 5 Sep 2024
Feature:
- add provenance publish
Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elysiajs/swagger",
"version": "1.1.3",
"version": "1.1.4",
"description": "Plugin for Elysia to auto-generate Swagger page",
"author": {
"name": "saltyAom",
Expand Down Expand Up @@ -62,7 +62,7 @@
"devDependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@types/bun": "1.1.6",
"elysia": ">= 1.1.0-rc.2",
"elysia": "1.1.18",
"eslint": "9.6.0",
"tsup": "^8.1.0",
"typescript": "^5.5.3"
Expand Down
73 changes: 41 additions & 32 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ const mapTypesResponse = (

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

for (const type of types)
for (const type of types) {
// console.log(schema)

responses[type] = {
schema:
typeof schema === 'string'
Expand All @@ -78,6 +80,7 @@ const mapTypesResponse = (
}
: { ...(schema as any) }
}
}

return responses
}
Expand All @@ -101,6 +104,12 @@ export const generateOperationId = (method: string, paths: string) => {
return operationId
}

const cloneHook = <T>(hook: T) => {
if (!hook) return

return { ...hook }
}

export const registerSchemaPath = ({
schema,
path,
Expand All @@ -114,7 +123,7 @@ export const registerSchemaPath = ({
method: HTTPMethod
hook?: LocalHook<any, any, any, any, any, any, any>
models: Record<string, TSchema>
}) => {
}) => {
const contentType = hook?.type ?? [
'application/json',
'multipart/form-data',
Expand All @@ -126,13 +135,13 @@ export const registerSchemaPath = ({
const contentTypes =
typeof contentType === 'string'
? [contentType]
: contentType ?? ['application/json']
: (contentType ?? ['application/json'])

const bodySchema = hook?.body
const paramsSchema = hook?.params
const headerSchema = hook?.headers
const querySchema = hook?.query
let responseSchema = hook?.response as unknown as OpenAPIV3.ResponsesObject
const bodySchema = cloneHook(hook?.body)
const paramsSchema = cloneHook(hook?.params)
const headerSchema = cloneHook(hook?.headers)
const querySchema = cloneHook(hook?.query)
let responseSchema: OpenAPIV3.ResponsesObject = cloneHook(hook?.response)

if (typeof responseSchema === 'object') {
if (Kind in responseSchema) {
Expand Down Expand Up @@ -292,36 +301,36 @@ export const registerSchemaPath = ({
}

export const filterPaths = (
paths: Record<string, any>,
docsPath: string,
{
excludeStaticFile = true,
exclude = []
}: {
excludeStaticFile: boolean
exclude: (string | RegExp)[]
}
paths: Record<string, any>,
docsPath: string,
{
excludeStaticFile = true,
exclude = []
}: {
excludeStaticFile: boolean
exclude: (string | RegExp)[]
}
) => {
const newPaths: Record<string, any> = {}

// exclude docs path and OpenAPI json path
const excludePaths = [`/${docsPath}`, `/${docsPath}/json`].map((p) =>
// exclude docs path and OpenAPI json path
const excludePaths = [`/${docsPath}`, `/${docsPath}/json`].map((p) =>
normalize(p)
)

for (const [key, value] of Object.entries(paths))
if (
!exclude.some((x) => {
if (typeof x === 'string') return key === x

return x.test(key)
}) &&
!excludePaths.includes(key) &&
!key.includes('*') &&
(excludeStaticFile ? !key.includes('.') : true)
) {
Object.keys(value).forEach((method) => {
const schema = value[method]
for (const [key, value] of Object.entries(paths))
if (
!exclude.some((x) => {
if (typeof x === 'string') return key === x

return x.test(key)
}) &&
!excludePaths.includes(key) &&
!key.includes('*') &&
(excludeStaticFile ? !key.includes('.') : true)
) {
Object.keys(value).forEach((method) => {
const schema = value[method]

if (key.includes('{')) {
if (!schema.parameters) schema.parameters = []
Expand Down

0 comments on commit 8c4634a

Please sign in to comment.