Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: route schema generation #933

Open
PandaWorker opened this issue Jan 14, 2025 · 1 comment
Open

bug: route schema generation #933

PandaWorker opened this issue Jan 14, 2025 · 1 comment
Labels

Comments

@PandaWorker
Copy link

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
@yusukebe
Copy link
Member

@PandaWorker Thank you for raising the issue.

Hey @curtislarson. Can you handle this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants