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

@hono/zod-openapi outputs examples as an array, causing incompatibility with Swagger UI #879

Open
k3ntar0 opened this issue Dec 10, 2024 · 1 comment

Comments

@k3ntar0
Copy link

k3ntar0 commented Dec 10, 2024

Summary

According to the OpenAPI specification, the examples field in a response must be an object. However, when using Schema.openapi({ examples }), if examples is provided as an array, it is output as an array in the generated OpenAPI spec. This causes Swagger UI to fail to parse the examples correctly. Additionally, the examples provided in the schema are not displayed at all in Swagger UI.

Implementation

import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";

const ResourceOutputSchema = z
  .object({
    status: z.enum(["pending", "completed"]),
  })
  .openapi({
    description: "The status of the resource",
    examples: [{ status: "pending" }, { status: "completed" }],
  });

const apiDefinition = createRoute({
  method: "get",
  path: "/resource/:id",
  request: {
    params: z.object({
      id: z.string(),
    }),
  },
  responses: {
    200: {
      description: "Successful response",
      content: {
        "application/json": {
          schema: ResourceOutputSchema,
        },
      },
    },
  },
});

export const getResourceRoute = (app: OpenAPIHono) => {
  return app.openapi(apiDefinition, async (c) => {
    return c.json({ status: "completed" as const }, 200);
  });
};

Generated OpenAPI Spec

{
  "openapi": "3.1.0",
  "info": {
    "version": "1.0.0",
    "title": "Example API"
  },
  "components": { "schemas": {}, "parameters": {} },
  "paths": {
    "/resource/:id": {
      "get": {
        "parameters": [
          {
            "schema": { "type": "string" },
            "required": true,
            "name": "id",
            "in": "path"
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "string",
                      "enum": ["pending", "completed"]
                    }
                  },
                  "required": ["status"],
                  "description": "The status of the resource",
                  "examples": [
                    { "status": "pending" },
                    { "status": "completed" }
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  "webhooks": {}
}

The examples provided in the schema are not displayed in Swagger UI:

Screenshot 2024-12-10 at 23 29 40

Additional Notes

If my understanding is incorrect and examples being an array is intended behavior, I’d appreciate clarification. Thank you!

@noworker
Copy link

I have same issue

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

No branches or pull requests

2 participants