From caa838044755374738fe0156a1c0ef647d5cb84a Mon Sep 17 00:00:00 2001 From: Munir Husseini Date: Thu, 16 Sep 2021 09:39:46 +0200 Subject: [PATCH] Added (on)SchemaGenerated to JSchemaGenerator The proposed addition is intended to allow users to easily manipulate the JSchema after is gets generated. This is especially helpful with inline schemas that are not reachable otherwise. --- .../Generation/JSchemaGenerator.cs | 15 +++++++++++++++ .../Generation/JSchemaGeneratorInternal.cs | 2 ++ 2 files changed, 17 insertions(+) diff --git a/Src/Newtonsoft.Json.Schema/Generation/JSchemaGenerator.cs b/Src/Newtonsoft.Json.Schema/Generation/JSchemaGenerator.cs index a1f89a19..d6a6bfbc 100644 --- a/Src/Newtonsoft.Json.Schema/Generation/JSchemaGenerator.cs +++ b/Src/Newtonsoft.Json.Schema/Generation/JSchemaGenerator.cs @@ -97,6 +97,12 @@ public virtual IContractResolver ContractResolver set => _contractResolver = value; } + /// + /// Is called each time a schema is generated. + /// Allows the user to manipulate the schema. + /// + public Action? SchemaGenerated; + /// /// Initializes a new instance of the class. /// @@ -135,5 +141,14 @@ public virtual JSchema Generate(Type type, bool rootSchemaNullable) JSchemaGeneratorInternal generator = new JSchemaGeneratorInternal(this); return generator.Generate(type, required); } + + /// + /// Is called each time a schema is generated. + /// Allows the user to manipulate the schema. + /// + internal protected virtual void OnSchemaGenerated(Type type, JSchema schema) + { + this.SchemaGenerated?.Invoke(type, schema); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs b/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs index c583c7d4..10c61940 100644 --- a/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs +++ b/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs @@ -309,6 +309,8 @@ private JSchema GenerateInternal(Type type, Required? valueRequired, JsonPropert PopulateSchema(schema, contract, memberProperty, resolvedRequired); } + _generator.OnSchemaGenerated(type, schema); + return schema; }