an2deg / pyraml-parser

pyraml-parser - Python parser to RAML, the RESTful API Modeling Language.
81 stars 31 forks source link

Order not conserved #11

Closed pimpreneil closed 9 years ago

pimpreneil commented 9 years ago

Hi,

I have tryed you parser on one of my RAML project and I have noticed a pretty annoying issue: Even if the resources are provided in the form of an OrderedDict, the order of the items is not conserved between the RAML file and the resulting OrderedDict. It seems to be linked to the YAML parser itself that does not keep the order...

Do you think there is any simple solution to fix this issue?

Thanks :)

pimpreneil commented 9 years ago

I have just found a quick fix for this issue: Adding these lines right before calling the load method solves the problem:

_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG

def dict_representer(dumper, data):
    return dumper.represent_dict(data.iteritems())

def dict_constructor(loader, node):
    return OrderedDict(loader.construct_pairs(node))

yaml.add_representer(OrderedDict, dict_representer)
yaml.add_constructor(_mapping_tag, dict_constructor)
an2deg commented 9 years ago

I've tried your solution but some of tests failed with it. I'll will think how to fix the issue

postatum commented 9 years ago

Hi @pimpreneil

Thanks for reporting an issue.

Your fix does work and preserves the order. It is in master already. https://github.com/an2deg/pyraml-parser/commit/a68387f6b53f7715615893d62fbc463bcdc6a73f

Thanks!