openapi-sampler icon indicating copy to clipboard operation
openapi-sampler copied to clipboard

BUG: Recursive schemas are not handled correctly

Open valerii15298 opened this issue 2 months ago • 0 comments

Reproduction:

import { sample } from "openapi-sampler";

const recursive = {
  type: "object",
  properties: { human: { $ref: "#/definitions/person" } },
  definitions: {
    person: {
      type: "object",
      properties: {
        name: { type: "string" },
        friend: { $ref: "#/definitions/person" },
        friends: { type: "array", items: { $ref: "#/definitions/person" } },
      },
      required: ["name", "friends"],
    },
  },
}

const fromSampler = sample(recursive, {}, recursive);
console.log(fromSampler);

Reproduction Invalid Output

{ human: { name: 'string', friend: {}, friends: [ {} ] } }

Which is invalid according to the schema, the friends array should be either empty or contain one valid person and friend field should be either omitted or valid item.

Expected Output

Where friends array is just empty and friend item omitted.

{ human: { name: "string", friends: [] } }

Or where it contains one valid item:

{
  human: {
    name: "string",
    friend: { name: "string", friends: [] },
    friends: [{ name: "string", friends: [] }],
  },
}

valerii15298 avatar Nov 13 '25 16:11 valerii15298