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

It seems that the lib does not support union types (or discriminatedUnion) on querystring #215

Open
juanmagalhaes opened this issue Dec 23, 2024 · 4 comments

Comments

@juanmagalhaes
Copy link

juanmagalhaes commented Dec 23, 2024

I'm using it with:

import fastifySwagger from "@fastify/swagger";
import fastifySwaggerUI from "@fastify/swagger-ui";

As it is described in the documentation of this lib.

Try the following:

  app.get(
    "/",
    {
      schema: z.union([z.object({ x: 1 }), z.object({ a: 2 }) ]),
    },

I believe that z.union should be mapped to anyOf while z.discriminatedUnion should be mapped to oneOf

My complete setup is more or less the following:

import 'zod-openapi/extend';
import fastifySwagger from "@fastify/swagger";
import fastifySwaggerUI from "@fastify/swagger-ui";
import Fastify from "fastify";

import {
  type FastifyZodOpenApiTypeProvider,
  fastifyZodOpenApiPlugin,
  fastifyZodOpenApiTransform,
  fastifyZodOpenApiTransformObject,
  serializerCompiler,
  validatorCompiler,
} from 'fastify-zod-openapi';
import { type ZodOpenApiVersion } from 'zod-openapi';

import { logger } from "@/logger";


export async function buildFastifyApp() {
  const app = Fastify({
    loggerInstance: logger,
  }).withTypeProvider<FastifyZodOpenApiTypeProvider>();

  // Add schema validator and serializer
  app.setValidatorCompiler(validatorCompiler);
  app.setSerializerCompiler(serializerCompiler);

  await app.register(fastifyZodOpenApiPlugin, {
    documentOpts: {
      unionOneOf: true,
    }
  });

  await app.register(fastifySwagger, {
    openapi: {
      openapi: '3.0.3' satisfies ZodOpenApiVersion,
      info: {
        title: "My Project",
        description: "My Project API",
        version: "1.0.0",
      },
      servers: [
      ],
    },
    transform: fastifyZodOpenApiTransform,
    transformObject: fastifyZodOpenApiTransformObject,
  });

  await app.register(fastifySwaggerUI, {
    routePrefix: "/documentation",
  });

Any thoughts?

@samchungy
Copy link
Owner

Hey, openapi does not support unions for parameters. If you can find me some examples where it does I'd be happy to help you out

@samchungy
Copy link
Owner

Though, it may be possible to "flatten" the properties in the headers out into an "optional"-esque main object.

@juanmagalhaes
Copy link
Author

I'm referring to this: https://swagger.io/docs/specification/v3_0/data-models/oneof-anyof-allof-not/

From the OpenAPI specs:

image

And the link to it: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#openapi-object

Like I said

I believe that z.union should be mapped to anyOf while z.discriminatedUnion should be mapped to oneOf

Does that make sense?

@samchungy
Copy link
Owner

Yes but you mentioned query strings which are parameters which are cannot use oneOf or anyOf.

https://learn.openapis.org/specification/parameters.html

it('should support create document options', async () => {

Here's oneOf working.

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