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

Content parts with get-only collections can't retain their values (JOptions configuration) #15533

Closed
sarahelsaig opened this issue Mar 17, 2024 · 1 comment · Fixed by #15534
Labels
Milestone

Comments

@sarahelsaig
Copy link
Contributor

sarahelsaig commented Mar 17, 2024

Describe the bug

Right now if your content elements contain any get-only collections, those don't get populated by System.Text.Json. they are just left empty. This is an unintended breaking change compared to Newtonsoft.

Why have get-only collection props in the first place? See the explanation in the static code analysis warning CA2227: Collection properties should be read only.

To Reproduce

  1. Have a model that contains a get-only collection property, such as this one.
  2. Add it to a content type, create and save a content item with some strings in the collection property.
  3. Load it back from the DB.
  4. Observe that the collection is now empty

Expected behavior

In Newtonsoft get-only collections (lists, dictionaries, etc) are automatically populated (see mode on the comparison here).

Solution

JOptions.Base should contain the configurations:

  • PreferredObjectCreationHandling = JsonObjectCreationHandling.Populate
  • ReferenceHandler = null

Note: I'm not entirely sure what the second one means, but it's necessary otherwise the site crashes with the message "System.InvalidOperationException: JsonObjectCreationHandling.Populate is incompatible with reference handling." (see). I suspect it's about handling circular references, but I'm not sure. If anyone has some insight, please chime in.

Screenshots

image
A minimal sample (not using OC) to show the problem (the "B.Items") and solution (the "B.Items (with options)").

image
A real life example in Lombiq Content Editors for Orchard Core.

@MikeAlhayek
Copy link
Member

Yes. As a workaround, add init; to the property and it will get populated. This is also documented in the 1.9 release. Your fix is good, but not sure if it has impact to other options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants