gazpachoking / jsonref

Automatically dereferences JSON references within a JSON document.
http://jsonref.readthedocs.org
MIT License
122 stars 28 forks source link

replace_ref() loses additional fields #23

Closed y5wang closed 6 years ago

y5wang commented 6 years ago

With a schema similar to this:


"myProps": {
       "description": "Properties unique to my networks",
       "type": "object",
       "properties": {
              "freqInfo": {
                   "$ref": "../types/freqInfo.json",
                   "additionalNote": "frequency"
              }
       }
}```

with `replace_ref()`, the `$ref` is correctly resolved, but the additional field `additionalNote` is
discarded, which should have been remained in the resolved schema.
gazpachoking commented 6 years ago

The JSON reference documents say that a JSON reference should be an object with only the $ref key. All other keys will be ignored.

A JSON Reference is a JSON object, which contains a member named "$ref", which has a JSON string value. Example:

{ "$ref": "http://example.com/example.json#/foo/bar" }

If a JSON value does not have these characteristics, then it SHOULD NOT be interpreted as a JSON Reference.

The "$ref" string value contains a URI [RFC3986], which identifies the location of the JSON value being referenced. It is an error condition if the string value does not conform to URI syntax rules. Any members other than "$ref" in a JSON Reference object SHALL be ignored.

simonjupp commented 5 years ago

The may be ignored by validators but it would be good if jsonref didn't overwrite any properties that happened to be there, it isn't invalid JSON schema. We use some custom keywords that are currently getting overwritten so it would be great if you could support this.

gazpachoking commented 5 years ago

@simonjupp I'm a bit hesitant on that, for one, because the reference might be replaced by another type that isn't a dictionary. e.g.

example = {
  "key1": {"$ref": "#/key2", "extra": "key"},
  "key2": "some text"
}

It is possible now to access your extra keys if you want, it just isn't very clean. Like so, example["key1"].__reference__["extra"]