avian2 / jsonmerge

Merge a series of JSON documents.
MIT License
214 stars 25 forks source link

arrayMergeById not working when "items" is an array #59

Closed vman049 closed 1 year ago

vman049 commented 1 year ago

In example 6 here, when providing the full schema obtained here as shown below:

    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "array",
        "mergeStrategy": "arrayMergeById",
        "items": [
            {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "string"
                    },
                    "field": {
                        "type": "integer"
                    }
                }
            }
        ]
    }

the command base = merger.merge(base, a) breaks with the error:

jsonmerge.exceptions.SchemaError: 'arrayMergeById' merge strategy: This strategy is not supported when 'items' is an array: #/items

The expected result is for base to equal a after the merge.

avian2 commented 1 year ago

I don't have a good answer for this beyond what the error message says.

In the future I might look into implementing arrayMergeById for schemas where "items" is an array, but I can't make any promises.

vman049 commented 1 year ago

Thanks, @avian2. I'm just wondering -- how is what I did different from what is in the test case (example 6 here)? My understanding is that the only difference is that I added additional information about the schema than what is in the case. But otherwise, the case is the same. So if it works in the provided test case, it should also work for my schema. Am I missing something?

avian2 commented 1 year ago

arrayMergeById needs to look down into items to figure out where in the schema to descend into. It needs this to find the merge strategy to use when merging individual array items (and possibly further objects, arrays, etc. down the hierarchy). The logic to do this when items is an array is not implemented, so it just throws an error when it sees an array.

In your example, it doesn't need to look into items because there is nothing useful further defined there, but in general that is not the case.

vman049 commented 1 year ago

I see. It looks like it's an issue with the example itself since it's not using the the function as intended and causes the function to break when the complete schema is provided. Thanks for your response.