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

oneOf support #126

Open lifenautjoe opened 4 months ago

lifenautjoe commented 4 months ago

Is there a way to use the oneOf attribute on the response specification?

Like this:

export class PreviewLink extends OpenAPIRoute {
  static schema: OpenAPIRouteSchema = {
    tags: ["Link Preview"],
    summary: "Previews the contents of a given link",
    requestBody: {
      description: "Preview request options",
      required: true,
      content: {
        "application/json": {
          schema: PreviewLinkRequestData,
        },
        "application/x-www-form-urlencoded": {
          schema: PreviewLinkRequestData,
        },
        "multipart/form-data": {
          schema: PreviewLinkRequestData,
        },
      },
    },
    responses: {
      "200": {
        description: "Returns the link preview",
        schema: {
          oneOf: [
            ResponseDocLink,
            ResponseXUserLink,
            ResponseXPostLink,
            ResponseYoutubeChannelLink,
            ResponseYoutubeVideoLink,
            ResponseHtmlLink,
            ResponseImageLink,
            ResponseVideoLink,
            ResponseAudioLink,
            ResponseInstagramUserLink,
            ResponseInstagramPostLink,
          ],
        },
      },
    },
  };
goblinfactory commented 4 months ago

@lifenautjoe I was just browsing this repo for the first time and stumbled across your question. Have you tested this? do you know that anyOf, oneOf or allOf do not work for a fact? It's mentioned briefly here in the readme talking about zod schemas; https://github.com/cloudflare/itty-router-openapi/blob/89255c154a0421f7efb68b7d83d97b1964b8bc6a/docs/pages/types.md?plain=1#L221-L225