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

Dynamically computed meta with defineRouteMeta() #2641

Closed
zRelux opened this issue Aug 2, 2024 · 6 comments
Closed

Dynamically computed meta with defineRouteMeta() #2641

zRelux opened this issue Aug 2, 2024 · 6 comments
Labels
enhancement New feature or request experiment

Comments

@zRelux
Copy link

zRelux commented Aug 2, 2024

Environment

Nitro npm:nitropack-nightly@latest
Bun 1.1.21

Reproduction

const OpenApiUserProperties: Record<string, SchemaObject | ReferenceObject> = {
	id: { type: "string" },
};

defineRouteMeta({
	openAPI: {
		tags: ["user"],
		description: "Get user information",
		responses: {
			200: {
				description: "OK",
				content: {
					"application/json": {
						schema: {
							type: "object",
							properties: {
								status: { type: "string" },
								result: {
									type: "object",
									properties: OpenApiUserProperties,
								},
							},
						},
					},
				},
			},
		},
	},
});

Describe the bug

Seems like I cannot import any variables inside the openAPI object, it would be very useful to be able to do so since I for example this api returns a value from DB

Additional context

No response

Logs

No response

@michaelkplai
Copy link

Additional info: defineRouteMeta also doesn't work when called from within another function. I encountered this when trying to define a function that would accept input schemas and set the route meta based on the input schema as well as perform input validation in the page handler. I suspect this is caused by parsing route meta using an AST rather than evaluating the code directly.

As an alternative, I'd be interested if it would be possible to define route meta using the event handler object format:

export default defineEventHandler({
  meta: {
    // Specify meta
  },
  handler: async (event) => 'Hello World'
})

@Ttou
Copy link

Ttou commented Nov 17, 2024

when i use zod-openapi to generate schema, defineRouteMeta can't recognize

import { z } from 'zod'
import { createSchema, extendZodWithOpenApi } from 'zod-openapi'

extendZodWithOpenApi(z)

const TestLoginDto = z.object({
  username: z.string().openapi({ description: 'Username' }),
  password: z.string().openapi({ description: 'Password' }),
})

const { schema: TestLoginSchema } = createSchema(TestLoginDto)

defineRouteMeta({
  openAPI: {
    summary: 'Login',
    requestBody: {
      content: {
        'application/json': {
          schema: TestLoginSchema,
        },
      },
    },
  },
})

@pi0
Copy link
Member

pi0 commented Nov 18, 2024

Currently, defineRouteMeta is an experimental idea and only accepts static inlined meta.

@pi0 pi0 changed the title defineRouteMeta not working with imported values Dynamically computed meta with defineRouteMeta() Nov 18, 2024
@pi0 pi0 added enhancement New feature or request experiment and removed pending triage labels Nov 18, 2024
@GentleGhostCoder
Copy link

I think it's a good idea, and without this meta-definition, the entire OpenAPI documentation generation wouldn't make sense. Alternatively, everything could be generated automatically through TypeScript.

@AkarinServer
Copy link

when i use zod-openapi to generate schema, defineRouteMeta can't recognize

import { z } from 'zod'
import { createSchema, extendZodWithOpenApi } from 'zod-openapi'

extendZodWithOpenApi(z)

const TestLoginDto = z.object({
  username: z.string().openapi({ description: 'Username' }),
  password: z.string().openapi({ description: 'Password' }),
})

const { schema: TestLoginSchema } = createSchema(TestLoginDto)

defineRouteMeta({
  openAPI: {
    summary: 'Login',
    requestBody: {
      content: {
        'application/json': {
          schema: TestLoginSchema,
        },
      },
    },
  },
})

I have same problem with you. I think it is very necessary to support dynamic variables for defineRouteMeta

@pi0
Copy link
Member

pi0 commented Jan 7, 2025

Moving to #2974

@pi0 pi0 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request experiment
Projects
None yet
Development

No branches or pull requests

6 participants