-
Notifications
You must be signed in to change notification settings - Fork 53
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
Add custom filter to parse default value from dict to json #801
Conversation
Hi @gXkch. |
@Sispheor Let me explain: I require a user to provide a JSON input on a creation operation in a survey field. I load this json in the ansible playbook and use it there. All good. This case is a bit more about aesthethics and usability rather than how it should be handled in the ansible playbook. How it is handled in ansible is no issue at all. I could live with whatever input comes from the survey. The main issue, however, is when the user wants to do an update operation, they will need to provide a JSON again. It might be the same or they might have changed some values in the JSON or they are happy with the default value provided from the spec of the instance. When using a field validator that checks if the default input in the field is JSON, it fails. I do not expect the user to take the dict output and convert it to JSON themselves. Also, it might not immediately visible for the user what the validation problem is because dict and JSON look similar at first. Furthermore, it looks inconsistent that on day 1 you provide a valid JSON and on day 2 you request something and you see a python dict as the default in the survey. If you do not want to merge this, I would understand. I am probably the only one that has this problem. I am capable of just mounting the file with my changes as a file in the container and be happy with that. |
Did you try with native jinja filters? Seems that tojson exists. |
Yes, I tried. It just returns nothing whereas with my to_json filter it works. |
Amazing! Thanks for sharing because I thought the usage of tojson() was to put the dict in the brackets instead of using it with "|". I was basically doing |
Glad to know it works for you now ! |
@Sispheor just as a sidenote: When applying the tojson() filter in the survey, the review task will throw an error "Object of type Undefined is not JSON serializable". I can request a day 2 operation and it prefills the values just fine but then wanting to review it, I think it tries to apply the filter again on the review screen but this time to the json string of the survey field. Can you verify this on your end? |
@Sispheor I am retarded i did not do default({}) like in your example and this seemed to fix the issue |
I have the issue that I have a dict as the value in the admin spec of the instance. It looks like json in the web UI but in reality it is a dict. When populating the survey field of the day 2 operation (update) with that value using
{{ instance.spec.dictItem }}
, I get a dict when I would actually like a JSON since I am also requesting a JSON value on the creation operation. This adds another custom filter "to_json" to parse the dict as valid json. I have tested this in my environment and it is working.{{ instance.spec.dictItem | to_json }}
will now display a json value in the survey field when the requestor is creating a day 2 operation