cloudflare / chanfana

OpenAPI 3 and 3.1 schema generator and validator for Hono, itty-router and more!
https://chanfana.pages.dev
MIT License
263 stars 35 forks source link

Security not applying to the securitySchemes or schema #140

Open jcodog opened 3 months ago

jcodog commented 3 months ago

I have added an api key to my router so that when you go to docs a new authorise button appears, however when you try to make a request, it does not apply the API key you specify into the headers as expected, how do I fix this? I want to be able to have the docs show the same security etc as the main API.

export const router = OpenAPIRouter({
    docs_url: "/",
    schema: {
        info: {
            title: "JCoNet LTD API",
            version: "1",
        },
    },
});
router.registry.registerComponent("securitySchemes", "api_key", {
    type: "apiKey",
    in: "header",
    name: "x-api-key",
});

image As you can see in the code the scheme is shown but is not being included in the docs examples so isn't applying the header, have I done something wrong or does the docs not display the headers you provide in authorise?

jcodog commented 3 months ago

Is this the correct way, this is what I have tried most recently.

code:

export const router = OpenAPIRouter({
    docs_url: "/",
    schema: {
        info: {
            title: "JCoNet LTD API",
            version: "1",
        },
        security: {
            api_key: [
                {
                    type: "apiKey",
                    in: "header",
                    name: "x-api-key",
                },
            ],
        },
    },
});
router.registry.registerComponent("securitySchemes", "api_key", {
    type: "apiKey",
    in: "header",
    name: "x-api-key",
});

console.log(router.schema);

console:

⎔ Reloading local server...
{
  openapi: '3.1.0',
  info: { title: 'JCoNet LTD API', version: '1' },
  security: { api_key: [ [Object] ] },
  components: {
    securitySchemes: { api_key: [Object] },
    schemas: {},
    parameters: {}
  },
  paths: {},
  webhooks: {}
}