Skip to content

Bug: Incorrect $ref for nested schemas #200

@deptyped

Description

@deptyped

Hi. I'm using valibot to define a schema and use metadata to give schemas references, as shown in the docs. I've encountered a bug: in the generated OpenAPI schema, the $ref for nested schemas is incorrect.

Instead of #/components/schemas/Schema it generates #/components/schemas/ents/schemas/Schema.

Code to reproduce on playground.hono.dev:

import { Hono } from 'https://esm.sh/hono'
import { describeRoute, openAPIRouteHandler, resolver } from 'https://esm.sh/hono-openapi'
import * as v from 'https://esm.sh/valibot'

const app = new Hono()

const userSchema = v.pipe(
  v.object({
    id: v.string(),
  }),
  v.metadata({
    ref: 'UserSchema',
  }),
)

const responseSchema = v.pipe(
  v.object({
    user: userSchema,
  }),
  v.metadata({
    ref: 'ResponseSchema',
  }),
)

app.get(
  '/',
  describeRoute({
    responses: {
      200: {
        content: {
          'application/json': {
            schema: resolver(responseSchema),
          },
        },
      },
    },
  }),
)

app.get('/openapi.json', openAPIRouteHandler(app))

export default app

Expected schema:

{
   "openapi":"3.1.0",
   "info":{
      "title":"Hono Documentation",
      "description":"Development documentation",
      "version":"0.0.0"
   },
   "paths":{
      "/":{
         "get":{
            "operationId":"getIndex",
            "responses":{
               "200":{
                  "content":{
                     "application/json":{
                        "schema":{
                           "$ref":"#/components/schemas/ResponseSchema"
                        }
                     }
                  }
               }
            }
         }
      }
   },
   "components":{
      "schemas":{
         "UserSchema":{
            "type":"object",
            "properties":{
               "id":{
                  "type":"string"
               }
            },
            "required":[
               "id"
            ]
         },
         "ResponseSchema":{
            "type":"object",
            "properties":{
               "user":{
-                  "$ref":"#/components/schemas/ents/schemas/UserSchema"
+                  "$ref":"#/components/schemas/UserSchema"
               }
            },
            "required":[
               "user"
            ]
         }
      }
   }
}

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions