From 5a5ceb6263cd65d2d6e73711fcbebdf40fd9bd83 Mon Sep 17 00:00:00 2001 From: ardelean-vlad <70897761+ardelean-vlad@users.noreply.github.com> Date: Sun, 15 May 2022 22:39:12 +0300 Subject: [PATCH] Pass entire ContextualType to SchemaProcessorContext (#1504) --- src/NJsonSchema/Generation/JsonSchemaGenerator.cs | 2 +- .../Generation/SchemaProcessorContext.cs | 13 +++++++++---- .../DiscriminatorSchemaProcessor.cs | 3 +-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs index 526c96456..bd03810c4 100644 --- a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs +++ b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs @@ -798,7 +798,7 @@ private void ApplyAdditionalProperties(TSchemaType schema, Type typ private void ApplySchemaProcessors(JsonSchema schema, ContextualType contextualType, JsonSchemaResolver schemaResolver) { - var context = new SchemaProcessorContext(contextualType.OriginalType, schema, schemaResolver, this, Settings); + var context = new SchemaProcessorContext(contextualType, schema, schemaResolver, this, Settings); foreach (var processor in Settings.SchemaProcessors) { processor.Process(context); diff --git a/src/NJsonSchema/Generation/SchemaProcessorContext.cs b/src/NJsonSchema/Generation/SchemaProcessorContext.cs index fd8cc8ecc..2e22a62b1 100644 --- a/src/NJsonSchema/Generation/SchemaProcessorContext.cs +++ b/src/NJsonSchema/Generation/SchemaProcessorContext.cs @@ -6,6 +6,7 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- +using Namotion.Reflection; using System; namespace NJsonSchema.Generation @@ -14,14 +15,14 @@ namespace NJsonSchema.Generation public class SchemaProcessorContext { /// Initializes a new instance of the class. - /// The source type. + /// The source contextual type. /// The JSON Schema. /// The resolver. /// The generator. /// The settings. - public SchemaProcessorContext(Type type, JsonSchema schema, JsonSchemaResolver resolver, JsonSchemaGenerator generator, JsonSchemaGeneratorSettings settings) + public SchemaProcessorContext(ContextualType contextualType, JsonSchema schema, JsonSchemaResolver resolver, JsonSchemaGenerator generator, JsonSchemaGeneratorSettings settings) { - Type = type; + ContextualType = contextualType; Schema = schema; Resolver = resolver; Generator = generator; @@ -29,7 +30,11 @@ public SchemaProcessorContext(Type type, JsonSchema schema, JsonSchemaResolver r } /// The source type. - public Type Type { get; } + [Obsolete("Use ContextualType to obtain this instead.")] + public Type Type { get => ContextualType.OriginalType; } + + /// The source contextual type. + public ContextualType ContextualType { get; } /// The JSON Schema to process. public JsonSchema Schema { get; } diff --git a/src/NJsonSchema/Generation/SchemaProcessors/DiscriminatorSchemaProcessor.cs b/src/NJsonSchema/Generation/SchemaProcessors/DiscriminatorSchemaProcessor.cs index 9f0720613..a80b0890d 100644 --- a/src/NJsonSchema/Generation/SchemaProcessors/DiscriminatorSchemaProcessor.cs +++ b/src/NJsonSchema/Generation/SchemaProcessors/DiscriminatorSchemaProcessor.cs @@ -7,7 +7,6 @@ //----------------------------------------------------------------------- #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member -#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously using NJsonSchema.Converters; using System; @@ -32,7 +31,7 @@ public DiscriminatorSchemaProcessor(Type baseType, string discriminator) public void Process(SchemaProcessorContext context) { - if (context.Type == BaseType) + if (context.ContextualType.OriginalType == BaseType) { var schema = context.Schema; schema.Discriminator = Discriminator;