{"record":{"id":"cd6600ac28698b23","repo":"JamesNK/Newtonsoft.Json","slug":"unexpected-contract-type-0","errorCode":null,"errorMessage":"Unexpected contract type: {0}","messagePattern":"Unexpected contract type: (.+?)","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs","lineNumber":387,"sourceCode":"                                CurrentSchema.AdditionalProperties = GenerateInternal(valueType, Required.Default, false);\n                            }\n                        }\n                        break;\n#if HAVE_BINARY_SERIALIZATION\n                    case JsonContractType.Serializable:\n                        CurrentSchema.Type = AddNullType(JsonSchemaType.Object, valueRequired);\n                        CurrentSchema.Id = GetTypeId(type, false);\n                        GenerateISerializableContract(type, (JsonISerializableContract)contract);\n                        break;\n#endif\n#if HAVE_DYNAMIC\n                    case JsonContractType.Dynamic:\n#endif\n                    case JsonContractType.Linq:\n                        CurrentSchema.Type = JsonSchemaType.Any;\n                        break;\n                    default:\n                        throw new JsonException(\"Unexpected contract type: {0}\".FormatWith(CultureInfo.InvariantCulture, contract));\n                }\n            }\n\n            return Pop().Schema;\n        }\n\n        private JsonSchemaType AddNullType(JsonSchemaType type, Required valueRequired)\n        {\n            if (valueRequired != Required.Always)\n            {\n                return type | JsonSchemaType.Null;\n            }\n\n            return type;\n        }\n\n        private bool HasFlag(DefaultValueHandling value, DefaultValueHandling flag)\n        {","sourceCodeStart":369,"sourceCodeEnd":405,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs#L369-L405","documentation":"Thrown by JsonSchemaGenerator.GenerateInternal in the default case of the contract-type switch, meaning the resolved JsonContract has a ContractType value not handled by the schema generator (Object, Array, Primitive, String, Dictionary, Serializable, Dynamic, Linq). This indicates an unexpected or custom contract type.","triggerScenarios":"Generating a schema for a type whose resolved contract is an uncommon or custom JsonContract subclass whose ContractType is outside the handled set, or a contract returned by a custom IContractResolver that fabricates a novel contract type.","commonSituations":"Using a custom IContractResolver that returns a non-standard JsonContract; a third-party contract type the generator was never taught; version mismatch between Newtonsoft.Json contract types and the schema generator; feeding a dynamically-built contract.","solutions":["Ensure the type resolves to one of the standard contract types (use the default DefaultContractResolver).","If a custom IContractResolver is in use, make it return standard contract types (JsonObjectContract, JsonArrayContract, JsonDictionaryContract, JsonPrimitiveContract, JsonLinqContract, JsonStringContract, JsonISerializableContract).","Migrate schema generation to the dedicated Newtonsoft.Json.Schema package, which has broader contract support."],"exampleFix":"// before\ngen.ContractResolver = new CustomResolverReturningUnknownContract();\ngen.Generate(typeof(Foo)); // throws\n// after\ngen.ContractResolver = new DefaultContractResolver();\ngen.Generate(typeof(Foo));","handlingStrategy":"validation","validationCode":"static bool UsesStandardContract(Type type, IContractResolver resolver)\n{\n    var ct = resolver.ResolveContract(type).ContractType;\n    return ct == JsonContractType.Object || ct == JsonContractType.Array ||\n           ct == JsonContractType.Primitive || ct == JsonContractType.String ||\n           ct == JsonContractType.Dictionary || ct == JsonContractType.Linq;\n}","typeGuard":null,"tryCatchPattern":"try { generator.Generate(typeof(T)); }\ncatch (JsonException ex) when (ex.Message.StartsWith(\"Unexpected contract type\"))\n{ /* custom resolver returned a non-standard contract; switch to default resolver */ }","preventionTips":["Ensure custom IContractResolver implementations return standard JsonContract types.","Use DefaultContractResolver when generating schemas.","Migrate to the Newtonsoft.Json.Schema package for broader support."],"tags":["schema","json","jsonschema","codegen","contract"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}