avian2 / jsonmerge

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

oneOf/anyOf of required incorrectly treated as branch #45

Closed mrehder closed 4 years ago

mrehder commented 4 years ago

The below JSON Schema construct is purely about enforcement and not structure. This implements a variant schema enforcement (in the below it says that either A&B must be present or A&C must be present). This should be ignored for descent and not generate an error.

{ "type" : "object", "anyOf" : [ { "required" : [ "A", "B" ] }, { "required" : [ "A", "C" ] } ],

avian2 commented 4 years ago

Hi. Can you please post a complete code example (with the head and base objects) that generates the error you mention? Thanks.

mrehder commented 4 years ago

I can't share the full schemas. This is the essential info.

      {
 "$schema": "http://json-schema.org/draft-04/schema#",
 "X": {
     "type": "array",
     "mergeStrategy": "arrayMergeById",
     "mergeOptions": {
         "idRef": "/Y"
    },
     "items": {
         "type": "object",
         "anyOf": [
            {
                 "required": [
                    "Y",
                    "Z"
                ]
            },
            {
                 "required": [
                    "Y",
                    "K"
                ]
            }
        ],
         "properties": {
             "Y": {
                 "type": "string"
            },
             "Z": {
                 "type": "string"
            },
             "K": {
                 "type": "string"
            }
        },
         "additionalProperties": false
    },
     "minItems": 1,
     "uniqueItems": true
}

}

avian2 commented 4 years ago

I suspect you're just missing a "mergeStrategy": "objectMerge" at the same level as the anyOf keyword.

mrehder commented 4 years ago

What would it do at that level (under "type":"object" as a peer of "properties"?). This definition fragment defines no JSON content so it should be ignored in the merge. Basically "anyOf"/"oneOf" of "required" arrays should be skipped.

mrehder commented 4 years ago

Also if the strategy is declared as a peer of the anyOf within the object of an array items, this could override the "arraymergeById" strategy declared above for the array.

avian2 commented 4 years ago

No, sorry. Given the sparse information you provided (no actual error message, no data and code to reproduce your problem apart from the partial schema), the best guess I can give you is in my previous comment. If you would like to learn more about why jsonmerge handles anyOf/allOf keywords the way it does, I invite you to browse the commit logs and search past issues (e.g. #29). I also suggest you read through jsonmerge's README file. I'm not going to discuss this further.

mrehder commented 4 years ago

I can't provide actual examples due to them being proprietary.

This is the traceback ( you didn't mention you needed that): Traceback (most recent call last): File "/usr/bin/jsonmerge", line 38, in main() File "/usr/bin/jsonmerge", line 32, in main result = jsonmerge.merge(result, json.load(file_handle), schema) File "/usr/lib/python2.7/site-packages/jsonmerge/init.py", line 372, in merge return merger.merge(base, head) File "/usr/lib/python2.7/site-packages/jsonmerge/init.py", line 330, in merge return walk.descend(schema, base, head).val File "/usr/lib/python2.7/site-packages/jsonmerge/init.py", line 85, in descend rv = self.work(strategy, schema, args, opts) File "/usr/lib/python2.7/site-packages/jsonmerge/init.py", line 126, in work rv = strategy.merge(self, base, head, schema, objclass_menu=self.merger.objclass_menu, kwargs) File "/usr/lib/python2.7/site-packages/jsonmerge/strategies.py", line 307, in merge base[k] = walk.descend(subschema, base.get(k), v) File "/usr/lib/python2.7/site-packages/jsonmerge/init.py", line 85, in descend rv = self.work(strategy, schema, args, opts) File "/usr/lib/python2.7/site-packages/jsonmerge/init.py", line 126, in work rv = strategy.merge(self, base, head, schema, objclass_menu=self.merger.objclass_menu, kwargs) File "/usr/lib/python2.7/site-packages/jsonmerge/strategies.py", line 217, in merge base.append(walk.descend(subschema, JSONValue(undef=True), head_item)) File "/usr/lib/python2.7/site-packages/jsonmerge/init.py", line 59, in descend rv = self.call_descender(descender, schema, *args) File "/usr/lib/python2.7/site-packages/jsonmerge/init.py", line 109, in call_descender return descender.descend_instance(self, schema, base, head) File "/usr/lib/python2.7/site-packages/jsonmerge/descenders.py", line 124, in descend_instance return self.descend(schema) File "/usr/lib/python2.7/site-packages/jsonmerge/descenders.py", line 121, in descend raise SchemaError("Can't descend to 'allOf' and 'anyOf' keywords", schema) jsonmerge.exceptions.SchemaError: 'arrayMergeById' merge strategy: Can't descend to 'allOf' and 'anyOf' keywords: #/properties/SubInterfaceDHCPConfigurations/items

Thanks Mike

From: Tomaž Šolc notifications@github.com Sent: Monday, December 9, 2019 11:44 AM To: avian2/jsonmerge jsonmerge@noreply.github.com Cc: Michael Rehder Michael.Rehder@Amdocs.com; Author author@noreply.github.com Subject: Re: [avian2/jsonmerge] oneOf/anyOf of required incorrectly treated as branch (#45)

No, sorry. Given the sparse information you provided (no actual error message, no data and code to reproduce your problem apart from the partial schema), the best guess I can give you is in my previous comment. If you would like to learn more about why jsonmerge handles anyOf/allOf keywords the way it does, I invite you to browse the commit logs and search past issues (e.g. #29https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Favian2%2Fjsonmerge%2Fpull%2F29&data=02%7C01%7CMichael.Rehder%40Amdocs.com%7Cac7224b625b9441ec91308d77cc72431%7Cc8eca3ca127646d59d9da0f2a028920f%7C0%7C0%7C637115067053715604&sdata=UIOvFnAvKWcF3LGj0zEsu4Pqo6wUuexBwX9LrWDmXFo%3D&reserved=0). I also suggest you read through jsonmerge's README file. I'm not going to discuss this further.

- You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Favian2%2Fjsonmerge%2Fissues%2F45%3Femail_source%3Dnotifications%26email_token%3DAC44PZ2R4DLUNVXCVS7ALNTQXZYVTA5CNFSM4JXA5YA2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGJ2ZYQ%23issuecomment-563326178&data=02%7C01%7CMichael.Rehder%40Amdocs.com%7Cac7224b625b9441ec91308d77cc72431%7Cc8eca3ca127646d59d9da0f2a028920f%7C0%7C0%7C637115067053725598&sdata=09wuUCQcaMxXIAj4kzPfmb8ijWo068jP1FxkSTCo7Og%3D&reserved=0, or unsubscribehttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAC44PZ3BXGHENALJLUMVTJTQXZYVTANCNFSM4JXA5YAQ&data=02%7C01%7CMichael.Rehder%40Amdocs.com%7Cac7224b625b9441ec91308d77cc72431%7Cc8eca3ca127646d59d9da0f2a028920f%7C0%7C0%7C637115067053725598&sdata=qBDRKlDc9qo6DAg5P6G0dYOGA%2FVz%2BCWOgh9Wiv%2FP2fA%3D&reserved=0. This email and the information contained herein is proprietary and confidential and subject to the Amdocs Email Terms of Service, which you may review at https://www.amdocs.com/about/email-terms-of-service https://www.amdocs.com/about/email-terms-of-service