middleware icon indicating copy to clipboard operation
middleware copied to clipboard

bug: route schema generation

Open PandaWorker opened this issue 1 year ago • 1 comments

Which middleware has the bug?

@hono/typebox-validator

What version of the middleware?

0.3.0

What version of Hono are you using?

4.6.16

What runtime/platform is your app running on? (with version if possible)

Node

What steps can reproduce the bug?

import { serve } from '@hono/node-server';
import { swaggerUI } from '@hono/swagger-ui';
import { tbValidator } from '@hono/typebox-validator';
import * as t from '@sinclair/typebox';
import { Hono } from 'hono';
import { describeRoute, openAPISpecs } from 'hono-openapi';
import { logger } from 'hono/logger';

const CreateUser = t.Object({
  name: t.String()
});

const AuthHeader = t.Object({
  'x-auth': t.String()
});

const api = new Hono();

api.use(logger());

api.post(
  '/',
  describeRoute({
    tags: ['Public'],
    requestBody: {
      content: {
        'application/json': {
          schema: CreateUser
        }
      },
    },
    responses: {
      200: {
        content: {
          'application/json': {
            schema: t.Object({ ok: t.Boolean(), user: CreateUser })
          }
        }
      }
    }
  }),
  tbValidator('json', CreateUser), (c) => {
    const user = c.req.valid('json');

    return c.json({ ok: true, user });
  }
);

api.put(
  '/',

  describeRoute({
    tags: ['Public'],
    summary: 'Empty schema'
  }),

  tbValidator('json', CreateUser),
  tbValidator('header', AuthHeader),

  (c) => {
    const headers = c.req.valid('header');
    const user = c.req.valid('json');

    return c.json({ ok: true, user });
  }
);

api.get(
  "/openapi.json",
  openAPISpecs(api, {
    documentation: {
      info: {
        title: "Hono",
        version: "1.0.0",
        description: "API for greeting users",
      },
      servers: [
        {
          url: "http://localhost:3000",
          description: "Local server",
        },
      ],
    },
  })
);

api.get(
  "/docs",
  swaggerUI({
    url: '/openapi.json',
  })
);

serve(api, (info) => {
  console.log(`Listening on http://localhost:${info.port}`); // Listening on http://localhost:3000
});

What is the expected behavior?

The openapi schema should be generated automatically based on validator and router data.

What do you see instead?

{
  "openapi": "3.1.0",
  "info": {
    "title": "Hono",
    "description": "API for greeting users",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "http://localhost:3000",
      "description": "Local server"
    }
  ],
  "paths": {
    "/": {
      "post": {
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "user": {
                      "type": "object",
                      "properties": {
                        "name": {
                          "type": "string"
                        }
                      },
                      "required": [
                        "name"
                      ]
                    }
                  },
                  "required": [
                    "ok",
                    "user"
                  ]
                }
              }
            }
          }
        },
        "operationId": "postIndex",
        "tags": [
          "Public"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  }
                },
                "required": [
                  "name"
                ]
              }
            }
          }
        }
      },
      "put": {
        "responses": {

        },
        "operationId": "putIndex",
        "tags": [
          "Public"
        ],
        "summary": "Empty schema"
      }
    }
  },
  "components": {
    "schemas": {

    }
  }
}

Additional information

What's the point of documentation if there's nothing there and everything needs to be hand-rolled for the right version of openapi?

I don't want to spend a lot of time manually describing routers, but I want the scheme to be automatically generated, as it was successfully done in python frameworks (fastapi, litestar)

image

PandaWorker avatar Jan 14 '25 10:01 PandaWorker

@PandaWorker Thank you for raising the issue.

Hey @curtislarson. Can you handle this?

yusukebe avatar Jan 15 '25 02:01 yusukebe