Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonref removes existing key-values if reference is present #60

Open
lucasrodes opened this issue Aug 29, 2023 · 2 comments
Open

jsonref removes existing key-values if reference is present #60

lucasrodes opened this issue Aug 29, 2023 · 2 comments

Comments

@lucasrodes
Copy link

lucasrodes commented Aug 29, 2023

Given the schema:

{
  "foo": {
    "$ref": "#/def/url",
    "title": "something additional that should not be removed"
  },
  "def": {
      "url": {
        "pattern": "pattern",
        "type": "string"
      }
  }
}

When loading it with jsonref.loads, the key title is removed:

import jsonref

loaded = jsonref.loads('''
{
  "foo": {
    "$ref": "#/def/url",
    "title": "something additional that should not be removed"
  },
  "def": {
      "url": {
        "pattern": "pattern",
        "type": "string"
      }
  }
}
''')
loaded

Outputs:

{'foo': {'pattern': 'pattern', 'type': 'string'},
 'def': {'url': {'pattern': 'pattern', 'type': 'string'}}}

But I'd expect

{'foo': {'pattern': 'pattern', 'type': 'string', 'title': 'something additional that should not be removed'},
 'def': {'url': {'pattern': 'pattern', 'type': 'string'}}}

Is this expected?

Thanks!

@lucasrodes lucasrodes changed the title jsonref overwrites key-values jsonref removes existing key-values if reference is present Aug 29, 2023
@gazpachoking
Copy link
Owner

gazpachoking commented Aug 29, 2023

This is expected, and is how the json reference spec is written. If you desire the two objects (the referenced object and the json reference object) to be merged, there is the merge_props option. This option is outside the spec, and may be different between different libraries though. https://jsonref.readthedocs.io/en/latest/#the-replace-refs-function

EDIT:
Here's some language from the JSON Ref 0.4 spec, which explains the default behavior.

Objects with a $ref property, such as { "$ref": <URI> }, are entirely replaced by the value pointed to by <URI>. This value is called the replacement-value.
All other properties of an object containing a $ref key are ignored.

JSON Schema diverged from the original JSON Reference spec (especially in more recent iterations.) Using the mentioned merge_props option along with the jsonschema option in this library gets closer to the behavior JSON Schema uses with the $ref keyword, and may match for most basic cases.

@dtheodor
Copy link

dtheodor commented Mar 25, 2024

Shouldn't you be using this schema?

{
  "foo": {
    "allOf": [{"$ref": "#/def/url"}],
    "title": "something additional that should not be removed"
  },
  "def": {
      "url": {
        "pattern": "pattern",
        "type": "string"
      }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants