Skip to content

Output type created for unusedIO: 'input' schemas #534

@jaens

Description

@jaens

When putting an object inside an union (or something similar to that), and setting unusedIO: 'input', it still results in an Output type being generated.
Just having a top-level object does not seem to have the same effect.

Tried to get rid of it, but got lost in the createComponents -> createIOSchemas -> createSchemas call tree.
The Output schema gets generated in createIOSchemas -> renameComponents when io: "output", but I could not find a way to prevent that since I did not figure out where the schema got there in the first place.
I tried adding ctx.io !== ...zodType.unusedIO in various places (createComponents, createIOSchemas) but it still somehow got included...

Reproducer test (into document.test.ts):

it('unusedIO input', () => {
  const Obj = z
    .object({
      field: z.string(),
    })
    .meta({ id: 'Obj', unusedIO: 'input'  });

  const Union = z.union([Obj]).meta({ id: 'Union', unusedIO: 'input' });

  const document = createDocument({
    info: {
      title: 'My API',
      version: '1.0.0',
    },
    openapi: '3.1.0',
    components: {
      schemas: {
        Union,
      },
    },
  });

  expect(document).toMatchInlineSnapshot(`
    {
      "components": {
        "schemas": {
          "Obj": {
            "properties": {
              "field": {
                "type": "string",
              },
            },
            "required": [
              "field",
            ],
            "type": "object",
          },
          "ObjOutput": {
            "additionalProperties": false,
            "properties": {
              "field": {
                "type": "string",
              },
            },
            "required": [
              "field",
            ],
            "type": "object",
          },
          "Union": {
            "anyOf": [
              {
                "$ref": "#/components/schemas/Obj",
              },
            ],
          },
        },
      },
      "info": {
        "title": "My API",
        "version": "1.0.0",
      },
      "openapi": "3.1.0",
    }
  `);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions