Skip to content

Dynamically computed meta with defineRouteMeta() #2641

Closed as not planned
Closed as not planned

Description

@zRelux

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

Activity

  1. added
    pending triage
    on Aug 2, 2024
  2. michaelkplai commented on Nov 1, 2024

    @michaelkplai

    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'
    })
  3. Ttou commented on Nov 17, 2024

    @Ttou

    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,
            },
          },
        },
      },
    })
  4. pi0 commented on Nov 18, 2024

    @pi0
    Member

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

  5. changed the title [-]defineRouteMeta not working with imported values[/-] [+]Dynamically computed meta with `defineRouteMeta()`[/+] on Nov 18, 2024
  6. added
    enhancement
    and removed
    pending triage
    on Nov 18, 2024
  7. GentleGhostCoder commented on Nov 18, 2024

    @GentleGhostCoder

    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.

  8. AkarinServer commented on Nov 29, 2024

    @AkarinServer

    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

  9. pi0 commented on Jan 7, 2025

    @pi0
    Member

    Moving to #2974

  10. alex-eri commented on Feb 14, 2025

    @alex-eri

    I have typescript error

    Type 'SchemaObject | SchemaObject | ReferenceObject' is not assignable to type 'ReferenceObject | SchemaObject | undefined'.
      Type 'SchemaObject' is not assignable to type 'ReferenceObject | SchemaObject | undefined'.
        Type 'SchemaObject' is not assignable to type '({ [key: `x-${string}`]: any; discriminator?: DiscriminatorObject | undefined; xml?: XMLObject | undefined; externalDocs?: ExternalDocumentationObject | undefined; ... 15 more ...; required?: string[] | undefined; } & StringSubtype) | ... 6 more ... | ({ ...; } & { ...; })'.
          Type 'SchemaObject' is not assignable to type '{ [key: `x-${string}`]: any; discriminator?: DiscriminatorObject | undefined; xml?: XMLObject | undefined; externalDocs?: ExternalDocumentationObject | undefined; ... 15 more ...; required?: string[] | undefined; } & ArraySubtype'.
            Type 'SchemaObject' is not assignable to type '{ [key: `x-${string}`]: any; discriminator?: DiscriminatorObject | undefined; xml?: XMLObject | undefined; externalDocs?: ExternalDocumentationObject | undefined; ... 15 more ...; required?: string[] | undefined; }'.
              Types of property 'oneOf' are incompatible.
                Type '(SchemaObject | ReferenceObject)[] | undefined' is not assignable to type '(ReferenceObject | SchemaObject)[] | undefined'.
                  Type '(SchemaObject | ReferenceObject)[]' is not assignable to type '(ReferenceObject | SchemaObject)[]'.
                    Type 'SchemaObject | ReferenceObject' is not assignable to type 'ReferenceObject | SchemaObject'.
                      Type 'SchemaObject' is not assignable to type 'ReferenceObject | SchemaObject'.
                        Type 'SchemaObject' is not assignable to type '({ [key: `x-${string}`]: any; discriminator?: DiscriminatorObject | undefined; xml?: XMLObject | undefined; externalDocs?: ExternalDocumentationObject | undefined; ... 15 more ...; required?: string[] | undefined; } & StringSubtype) | ... 6 more ... | ({ ...; } & { ...; })'.
                          Type 'SchemaObject' is not assignable to type '{ [key: `x-${string}`]: any; discriminator?: DiscriminatorObject | undefined; xml?: XMLObject | undefined; externalDocs?: ExternalDocumentationObject | undefined; ... 15 more ...; required?: string[] | undefined; } & ArraySubtype'.
                            Type 'SchemaObject' is not assignable to type 'ArraySubtype'.
                              Types of property 'type' are incompatible.
                                Type 'SchemaObjectType | SchemaObjectType[] | undefined' is not assignable to type '"array" | ["array", "null"]'.
                                  Type 'undefined' is not assignable to type '"array" | ["array", "null"]'.ts(2322)
    

    trying to use { createSchema, extendZodWithOpenApi } from 'zod-openapi'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions