From 7f764af523a4e7461a18cec876a45a2f7f32f3c8 Mon Sep 17 00:00:00 2001 From: SamuelBerger Date: Mon, 15 Aug 2022 14:24:19 +0200 Subject: [PATCH] #1547: Make RemoveNullability virtual for extendability (#1548) This allows to implement different logic e.g. treat JsonSchema.OneOf to have multiple (non nullable) items instead of zero or one. --- .../ApiSurfaceGuard.cs | 23 +++++++++++++++++++ .../TypeResolverBase.cs | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/NJsonSchema.CodeGeneration.Tests/ApiSurfaceGuard.cs diff --git a/src/NJsonSchema.CodeGeneration.Tests/ApiSurfaceGuard.cs b/src/NJsonSchema.CodeGeneration.Tests/ApiSurfaceGuard.cs new file mode 100644 index 000000000..18e88e331 --- /dev/null +++ b/src/NJsonSchema.CodeGeneration.Tests/ApiSurfaceGuard.cs @@ -0,0 +1,23 @@ +namespace NJsonSchema.CodeGeneration.Tests; + +public class ApiSurfaceGuard +{ + private abstract class TypeResolverBaseApiGuard : TypeResolverBase + { + protected TypeResolverBaseApiGuard(CodeGeneratorSettingsBase settings) : base(settings) + { + } + + // dummy implementation making sure this method stays overridable + public override string GetOrGenerateTypeName(JsonSchema schema, string typeNameHint) + { + throw new System.NotImplementedException(); + } + + // dummy implementation making sure this method stays overridable + public override JsonSchema RemoveNullability(JsonSchema schema) + { + throw new System.NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration/TypeResolverBase.cs b/src/NJsonSchema.CodeGeneration/TypeResolverBase.cs index f3d0f1152..2df45ad42 100644 --- a/src/NJsonSchema.CodeGeneration/TypeResolverBase.cs +++ b/src/NJsonSchema.CodeGeneration/TypeResolverBase.cs @@ -84,7 +84,7 @@ public void RegisterSchemaDefinitions(IDictionary definition /// Removes a nullable oneOf reference if available. /// The schema. /// The actually resolvable schema - public JsonSchema RemoveNullability(JsonSchema schema) + public virtual JsonSchema RemoveNullability(JsonSchema schema) { // TODO: Method on JsonSchema4? return schema.OneOf.FirstOrDefault(o => !o.IsNullable(SchemaType.JsonSchema)) ?? schema;