udamir / allof-merge

Simplify your JsonSchema by combining allOf safely.
MIT License
17 stars 5 forks source link

`mergeCombinarySibling` strips away all other fields from the object except oneOf/anyOf #137

Open vaibhavrajsingh2001 opened 2 days ago

vaibhavrajsingh2001 commented 2 days ago

When using mergeCombinarySibling to combine anyOf/oneOf with sibling content, the output object only contains anyOf/oneOf and strips away other fields, like title.

e.g.

A Schema where the parent object has a title field and the oneOf variants also have title fields:

{
  "title": "Sample title",
  "required": ["id"],
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    }
  },
  "oneOf": [
    {
      "title": "First Variant",
      "type": "object",
      "properties": {
        "key": {
          "type": "string"
        }
      }
    },
    {
      "title": "Second Variant",
      "type": "object",
      "additionalProperties": {
        "type": "string"
      }
    }
  ]
}

Would result in an output where the parent object's title field is lost:

{
  "oneOf": [
    {
      "title": "First Variant",
      "required": ["id"],
      "type": "object",
      "properties": { "id": { "type": "string" }, "key": { "type": "string" } }
    },
    {
      "title": "Second Variant",
      "required": ["id"],
      "type": "object",
      "properties": { "id": { "type": "string" } },
      "additionalProperties": { "type": "string" }
    }
  ]
}
vaibhavrajsingh2001 commented 2 days ago

@udamir please let me know if you need additional information/usecases from my side. Thanks for this awesome project! ❤️

udamir commented 1 day ago

Hi, the current behavior is correct. After merging the title from oneOf with the root title, you get the title from oneOf, as the merge strategy for the title field is to use the last title encountered.