aromanovich / jsl

A Python DSL for describing JSON schemas
http://jsl.readthedocs.org/
Other
218 stars 21 forks source link

Consistently order schema definitions when "ordered=True" #24

Closed ncoghlan closed 8 years ago

ncoghlan commented 8 years ago

With hash randomisation enabled, the key order of the definitions namespace can change between runs. The typical solution to that kind of problem is to sort the keys when calling json.dump, but that doesn't work for ordered=True as sorting when dumping to JSON would also lose the definition order for properties.

The technique I am currently using is to sort the definitions before dumping to JSON:

schema = MyDocument().get_schema(ordered=True)
schema["definitions"] = OrderedDict(sorted(schema["definitions"].items()))
with open(schema_file, "w") as f:
    json.dump(schema, f, indent=4)

Would it be reasonable to make sorting the subschema definitions and putting them in an OrderedDict instance the default behaviour for both get_schema and get_definitions_and_schema when ordered=True?

aromanovich commented 8 years ago

Thanks for the proposal. I've released jsl==0.2.4 with definitions sorting on PyPI.

ncoghlan commented 8 years ago

Thank you!