mokkabonna / json-schema-merge-allof

Simplify your schema by combining allOf
97 stars 23 forks source link

how to keep the local references? #32

Closed tadamovsky closed 1 year ago

tadamovsky commented 3 years ago

I'd like to eliminate allOf but to keep the references, in fact I have to keep them, because the schema is recursive. If not, can you consider supporting local refs. json-schema-ref-parser has a mode to make all refs local.

mokkabonna commented 1 year ago

This is probably what you want I imagine:

 const result = merge(
    {
      $ref: "#/components/schemas/MySchema",
      allOf: [
        {
          $ref: "#/components/schemas/2",
        },
      ],
    },
    {
      complexResolvers: {
        $ref: {
          keywords: ["$ref"],
          resolver(values, path, mergeSchemas, options) {
            return {
              ...values[0],
              allOf: values.slice(1).map((v) => v),
            };
          },
        },
      },
    }
  );

produces:

{
  "$ref": "#/components/schemas/MySchema",
  "allOf": [
    {
      "$ref": "#/components/schemas/2"
    }
  ]
}

Could probably be a easier way to achieve this though.