Skip to content

[OAS] Support recursive and ref in Zod OAS conventor #187335

Open
@maryam-saeidi

Description

Summary

While adding support for Zod, in the first iteration, we decided to skip supporting recursive in the first step and cover it in a separate issue.

During adding the test as follows:

const baseCategorySchema = z.object({
  name: z.string(),
});
type ZodRecursiveType = z.infer<typeof baseCategorySchema> & {
  subcategories: ZodRecursiveType[];
};

it('handles recursive schemas', () => {
      const recursiveSchema: z.ZodType<ZodRecursiveType> = baseCategorySchema.extend({
        subcategories: z.lazy(() => recursiveSchema.array()),
      });
      expect(
        generateOpenApiDocument(
          {
            routers: [
              createRouter({
                routes: [
                  {
                    isVersioned: false,
                    path: '/recursive',
                    method: 'get',
                    validationSchemas: {
                      request: {
                        body: recursiveSchema,
                      },
                      response: {
                        [200]: {
                          body: () => schema.string({ maxLength: 10, minLength: 1 }),
                        },
                      },
                    },
                    options: { tags: ['foo'] },
                    handler: jest.fn(),
                  },
                ],
              }),
            ],
            versionedRouters: [],
          },
          {
            title: 'test',
            baseUrl: 'https://test.oas',
            version: '99.99.99',
          }
        )
      ).toMatchSnapshot();
    });
  });

We also noticed the following warning: (comment)

@jloleysens pointed out in this comment that the snapshot should look something like this:

  "components": Object {
    "schemas": Object {
      "recursive": Object {
        "additionalProperties": false,
        "properties": Object {
          "name": Object {
            "type": "string",
          },
          "self": Object {
            "$ref": "#/components/schemas/recursive",
          },
        },
        "required": Array [
          "name",
          "self",
        ],
        "type": "object",
      },
    },    

Comment:

I think we will need to update $refStrategy to something other than none... Probably root? In packages/kbn-router-to-openapispec/src/oas_converter/zod/lib.ts

Metadata

Assignees

No one assigned

    Labels

    Feature:OASWork or issues related to Core-provided mechanisms for generating OASTeam:CoreCore services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions