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

Ordered fields for Add-Ons #185

Open
duckduckgrayduck opened this issue Dec 2, 2023 · 1 comment
Open

Ordered fields for Add-Ons #185

duckduckgrayduck opened this issue Dec 2, 2023 · 1 comment
Assignees

Comments

@duckduckgrayduck
Copy link
Contributor

Disclose brought up the fact that fields aren't ordered in Add-Ons, which is something we have noted before and is a not ideal.

From my understanding, the front-end loads the JSON parameters that are pulled from addons/models.py from the originating config.yaml file for the Add-On.

Currently in our addons/models.py and in our python-documentcloud library we use the standard pyyaml (import yaml).
The YAML spec doesn't traditionally support ordering- but it also doesn't prohibit it. It gets loaded into a dict, which before Python 3.7, was unordered. Python >= 3.7, dicts maintain order.

In Python 3.7+, the dict we get from running yaml.safe_load returns a dictionary with order preserved.
https://medium.com/@reorx/tips-that-may-save-you-from-the-hell-of-pyyaml-572cde7e1d6f

This is also noted in this discussion on PyYaml's official issues here:
yaml/pyyaml#110 (comment)

For a sanity check, I even tested safe_load on PII detector's config.yaml file locally and confirmed that order, even within properties, was preserved:
Order preserved

I think the issue here is JSON not maintaining order in parameters

parameters = models.JSONField(
("parameters"),
default=dict,
help_text=
("The user supplied parameters to run the add-on with"),
)

Is there some other way we could preserve order?

@mitchelljkotler
Copy link
Member

I think the best way to preserve order is to add a list which specifies field order - this is guaranteed to be ordered by JSON, unlike object properties, which by spec have no order, despite some implementations possibly preserving order under some conditions. Previously we used a library which did not support specifying filed order (it is not part of JSON Schema), but Chris replaced it with his own form rendering library, so adding this should be fairly easy.

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