Exception when deserializing non-nullable boolean (Deserialize_NonNullableBoolean)

It’s an inexplicable issue when an editor copy-pastes content in a structured text field, trying to deserialize it for front-end usage and failing when deserializing a non-nullable Boolean as the parameter obj is NULL.

When we “clean up” the content via copy-pasting it in notepad, the exact same content is suddenly working… I have tried to update the custom JsonField or Boolean serializer but to no avail.

We’re using Strawberry Shake (v15.1.12) as the interface between DatoCMS and .NET and it fails in the following method (throwing the 1st ArgumentNullException):

private global::System.Boolean Deserialize_NonNullableBoolean(global::System.Text.Json.JsonElement ? obj) {
  if (!obj.HasValue) {
    throw new global::System.ArgumentNullException();
  }

  if (obj.Value.ValueKind == global::System.Text.Json.JsonValueKind.Null) {
    throw new global::System.ArgumentNullException();
  }

  return\ _booleanTypeParser.Parse(obj.Value.GetBoolean() !);
}

The project is “pgb-Europe” and a record that is still failing has the ID “VeRZaJ36TDC1BrtsjC8uyg”. I can provide you with a front-end URL when needed (it’s currently in beta).

Hello @kevin.cocquyt,

I’m so sorry, I’m having a bit of trouble keeping up with this and understanding what exactly is happening. Could you perhaps please provide a step-by-step reproduction of what you’re doing so I can better follow along? (Either in text or a screen recording, etc., or even just the structured text JSON you’re trying to parse — the original, unmodified version, before you did anything to it in Notepad)

Are you saying you’re trying to copy and paste a structured text field from the admin interface directly into a .NET app as a string literal and trying to deserialize it there…? If so, I think that’s a bad idea since that’s an internal-use serialization meant for the UI itself. The public serialization would instead be fetching the field via the CDA or CMA APIs (which gets you clean JSON and helper utils). Our SDKs are only in JS and not .NET, but if you let me know what you’re trying to do in the app, I can try to help figure it out?

Otherwise, if I’m totally misunderstanding, could you please provide a clearer reproduction?

Thank you and sorry about that! We don’t usually use .NET, so even if this is a common workflow in that ecosystem, it is totally new to me… thank you for your patience :sweat_smile: :folded_hands:

It sounds like maybe there’s some sort of rich text or encoding that notepad might be stripping out, but which is otherwise tripping up the deserializer…? But without seeing the JSON you’re trying to parse, I can’t say for sure.

If it’s private, you can DM me here on the forum or email us at support@datocms.com.

I’m unable to copy-paste the structured text JSON (“An error occurred: Body is limited to 32000 characters; you entered 54568.“).

The steps that are taken:

  1. the customer’s editor is copy-pasting content into a structured text field
  2. we use Strawberry Shake as the interface between .NET and DatoCMS. This is a tool that creates full typed classes according to the schema it reads via the GraphQL query we setup for reading a content page (where the structured text was added)
  3. Strawberry Shake tries to read the content (using a GraphQL Client) and serializes the received JSON into POCO objects

=> it fails as it tries to serialize a non-nullable boolean. There are actually booleans in the GraphQL response but these are always filled in so it’s kinda unclear why the exception actually happens. After “cleaning” the text, it suddenly seems to work again.

PS: As I wasn’t able to paste the whole JSON in here, I’ll send that via a direct e-mail.

Thanks in advance!

As a quick follow-up. Maybe I should try contacting Strawberry Shake itself but maybe you (or a contact) already encountered the same issue and already know a solution to it?

Thank you! I received the JSON and checked it out.

Unfortunately, it looks totally fine to me — just our standard output, as you’d expect, and validates as correct JSON.

We don’t use dotNET and aren’t familiar with Strawberry Shake, unfortunately. But from the error messages, it seems like a type error. How does this client determine the GraphQL types? Is it actually doing a GraphQL introspection on your behalf and erroring out because of that, or are you manually feeding it types somehow?

I do think this is something you’ll probably be able to get better support for directly from Strawberry Shake, since the JSON itself is fine.

However, here’s a few things you could try while waiting for them to answer… sorry, these are really just my random guesses since it’s not a ecosystem I’m familiar with or that we officially support:

  • Is Strawberry Shake Unicode-aware? You do have a few non-ASCII characters in the text, like • in • Geschikt voor zowel de holle…. I don’t know if that matters at all (maybe Notepad is implictly convering that somehow? you could do use a diff tool to see exactly what is different between our original output and the Notepad version)
  • Is Strawberry Shake actually able to get the correct network response back from the CDA (as in API tokens, async issues, etc.?) (It wasn’t clear to me whether your Notepad test was skipping that, like if you were just copying & pasting the raw JSON vs fetching it over the network)
  • Maybe traverse the JSON tree and replace all the nulls (like sections[0].settings.optBackgroundImage with false or whatever non-null value that app requires)? Note that JS probably has a different truthiness model than .NET (C#?) though.
  • You could also try feeding it just one part of the tree at a time, e.g. one object or even property at a time, to see which one is actually erroring out
  • It’s possible you might not need a special GraphQL client to fetch from the CDA? If your use case allows it, perhaps you could make a regular HTTP call to the CDA and then parse the JSON result using native JSON built-ins like System.Text.Json?

Sorry I don’t have a better answer for you :frowning: As far as I can tell, what’s coming out of our API is valid and correct in this case, but something is mangling either the types or the parsing.

And just in case I’m misunderstanding this part… I wanted to clarify:

  • The JSON you emailed us, that was without the cleaning, right? Like it’s what your client is currently erroring out on?
  • How exactly is Notepad being used here? Like are you saying:
    • AFTER you receive the broken JSON in your app, then you copy and paste it into Notepad, and then copy and paste it back into the app?
    • Or does the editor copy and paste the content into Notepad, and back out, before entering it into the Structured Text to begin with? (If so, what was the original content like? Could you email the original broken stuff to me too, please?)

I’m asking because I’m trying to ascertain if this is actually a parsing error in that client of yours, or if it’s a serialization problem on our side with certain kinds of input (the pre-Notepad original text) going into structured text in the first place.

Also, as a last resort, if this doesn’t require any paid purchases (Visual Studio and Strawberry Shake are both free, right?), maybe you can make a small reproduction for us with just the relevant snippet (like the query, fetch, parsing, and error) and I can try to run it on a Windows machine to replicate the issue more clearly?

If I can do that, I’ll try my best to troubleshoot and debug it… but I have to warn you that it’s not something any of us on the team are very familiar with, sorry! Haven’t written C# or used .NET in more than 20 years… :sweat_smile:

I still think probably you would be able to get better support from Strawberry Shake directly, but I’m happy to try to debug it on my side if you can share a repro with us.

Good question :slight_smile: As far as I know, we use Strawberry’s CLI to download the schema (according to the supplied query) and it creates fully typed classes in .NET but the way of detection is kinda unclear for me and I should probably ask that directly towards Strawberry’s team.

At the start of our project, we used to serialize the content using a UTF8-writer (.NET) which broke lots of content articles but once we switched to a more general serializer, most of the content articles now work just fine except for a couple of unexplicable ones.

I have also thought about:

  • async issues: maybe Strawberry is streaming the response and not able to get the full payload but tries to serialize it anyway, whilst being incomplete. A question I should ask Strawberry too…
  • replace the nulls with false but that wouldn’t explain why it’s suddenly working after copy-pasting is from notepad

The JSON I sent via e-mail was copied from the CDA Playground within DatoCMS itself so without any updates done by me or Strawberry. The “cleaning” part was actually done by copying the text directly from the structured text editor into notepad and back. There used to be a time where editors copy-pasted content right out of Microsoft Word and that always contained lots of hidden markup and notepad was something that could clean up a lot of that stuff so it didn’t break webpages. That’s why I looked towards Notepad as a “solution”.

I’ll check with Strawberry support and let you know if anything comes up. Thanks anyway to follow along!

And we will advice the editor to copy-paste content paragraph by paragraph to see where it breaks.

1 Like

A quick FYI, also for other users. I created a minimal Nuxt application and copied the GraphQL query + fragments in there, generated a TypeScript-safe client and that solution was able to render the content as it should be.

Issue seems to be situated in Strawberry and how it serializes the payload from the API.

1 Like

Yeah, it sounds like a JSON deserialization issue within Strawberry itself. Wish I could help more there, sorry! But if their team has any follow-up, please let us know. Please also feel free to invite them to post here (or connect us via email at support@datocms.com) if we can help them troubleshoot this in any way.

Thank you!