It’s soooooo obvious!

While using the Newtonsoft Json.Net library I received the error

"Explicitly define an Id for the type using a JsonObject/JsonArray attribute or automatically generate a type Id using the UndefinedSchemaIdHandling property."

When attempting to generate a schema for some types that were created that have circular references. My types were created in EntityFramework database first strategy, so I didn’t want to figure out how to hack the T4 generator to add the Json attribute, and I didn’t quite understand what it mean by using the “UndefinedSchemaIdHandling property”. I suppose it might seem obvious to the author as when I searched the interwebs for an example, this was one of the first results

His response was pretty much recursive error output.

if (user.hasError)
  while (user.doesNotUnderstandsError) 

Now, unfortunately, I was in that loop for a little bit, looking at the method overloads, thinking about maybe a partial class to add the attribute, staring at the message expecting some epiphany and looked online with no success. It clearly must have been obvious to everyone else besides me, because I couldn’t find any articles on it. So I downloaded the source code for the library, read through the logic until I got to the method that leveraged a public property on the generator class itself, in which you can how you want to handle the undefined schema Id.

So in case any other developers out there, not quite as dumb as I am, still are not able to figure out that error from the exception message alone. Here is a easy code snippet to move past this problem.

JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator();
schemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;

var result = schemaGenerator.Generate(typeof(MyComplicatedRecursiveTypeWithNoIds));

Happy coding!

Leave a Reply