asdf icon indicating copy to clipboard operation
asdf copied to clipboard

Make sure a referenced schema value doesn't clobber the same schema entry in the parent schema

Open jdavies-st opened this issue 5 years ago • 0 comments

If I have a schema

%YAML 1.1
---
$schema: "http://json-schema.org/draft-04/schema"
id: "http://stsci.edu/schemas/jwst_datamodel/my.schema"
type: object
properties:
  date:
    title: "[yyyy-mm-ddThh:mm:ss.ss] UTC date file created"
    type: string
  $ref: "another.schema"
...

and another.schema is:

%YAML 1.1
---
$schema: "http://json-schema.org/draft-04/schema"
id: "http://stsci.edu/schemas/jwst_datamodel/another.schema"
date:
  type: object
  properties:
    mjd:
      title: "Time in MJD"
      type: string
...

The referenced schema another.schema will overwrite the parent schema my.schema. This seems like it should throw an error. Originally in the parent schema, date is a string. But with the referenced schema added, it now is an object with a property mjd and the original definition is overwritten.

Using the above schemas:

In [1]: import asdf                                                                                                                          

In [2]: schema = asdf.schema.load_schema("http://stsci.edu/schemas/jwst_datamodel/my.schema")                                                

In [3]: schema                                                                                                                               
Out[3]: 
OrderedDict([('$schema', 'http://json-schema.org/draft-04/schema'),
             ('id', 'http://stsci.edu/schemas/jwst_datamodel/my.schema'),
             ('type', 'object'),
             ('properties',
              OrderedDict([('date',
                            OrderedDict([('title',
                                          '[yyyy-mm-ddThh:mm:ss.ss] UTC date file created'),
                                         ('type', 'string')])),
                           ('$ref', 'another.schema')]))])

In [4]: schema = asdf.schema.load_schema("http://stsci.edu/schemas/jwst_datamodel/my.schema", resolve_references=True)                       

In [5]: schema                                                                                                                               
Out[5]: 
OrderedDict([('$schema', 'http://json-schema.org/draft-04/schema'),
             ('id', 'http://stsci.edu/schemas/jwst_datamodel/my.schema'),
             ('type', 'object'),
             ('properties',
              OrderedDict([('$schema',
                            'http://json-schema.org/draft-04/schema'),
                           ('id',
                            'http://stsci.edu/schemas/jwst_datamodel/another.schema'),
                           ('date',
                            OrderedDict([('type', 'object'),
                                         ('properties',
                                          OrderedDict([('mjd',
                                                        OrderedDict([('title',
                                                                      'Time in MJD'),
                                                                     ('type',
                                                                      'string')]))]))]))]))])

It would preferable to get a warning or error that something has been clobbered once the references have been resolved.

jdavies-st avatar Feb 27 '20 22:02 jdavies-st