From 73393674e6deba1c2576672441f2d92ea4cf874f Mon Sep 17 00:00:00 2001 From: Voronkov-A Date: Mon, 22 Feb 2021 17:24:52 +0300 Subject: [PATCH 1/6] fix typo in template for Text.Json.Serialization.JsonPropertyName (#1321) Co-authored-by: avoronkov --- src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid index b2e03702f..223498e35 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid @@ -49,7 +49,7 @@ /// {{ property.Description | csharpdocs }} {% endif -%} {% if UseSystemTextJson -%} - [System.Text.Json.Serialization.JsonPropertyName("{{ property.Name }}"] + [System.Text.Json.Serialization.JsonPropertyName("{{ property.Name }}")] {% if property.IsStringEnumArray -%} // TODO(system.text.json): Add string enum item converter {% endif -%} From 6dfd1292fa9bd0076f565e103778a5f065919f94 Mon Sep 17 00:00:00 2001 From: mmdtgdev <79144107+mmdtgdev@users.noreply.github.com> Date: Tue, 23 Feb 2021 17:42:47 +0100 Subject: [PATCH 2/6] Add schema title property to ClassTemplateModelBase (#1320) * Add schema title property to ClassTemplateModelBase. * Make SchemaTitle property non-virtual. --- .../Models/ClassTemplateModelBase.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/NJsonSchema.CodeGeneration/Models/ClassTemplateModelBase.cs b/src/NJsonSchema.CodeGeneration/Models/ClassTemplateModelBase.cs index 07ac4287e..5afd37eae 100644 --- a/src/NJsonSchema.CodeGeneration/Models/ClassTemplateModelBase.cs +++ b/src/NJsonSchema.CodeGeneration/Models/ClassTemplateModelBase.cs @@ -27,10 +27,17 @@ protected ClassTemplateModelBase(TypeResolverBase resolver, JsonSchema schema, o _schema = schema; _rootObject = rootObject; _resolver = resolver; + + SchemaTitle = _schema?.Title; } /// Gets the class. public abstract string ClassName { get; } + + /// + /// Gets the original title of the class (schema title). + /// + public string SchemaTitle { get; } /// Gets a value indicating whether this class represents a JSON object with fixed amount of properties. public bool IsObject => _schema.ActualTypeSchema.IsObject; From e83515b065b486cd5890f7ddefc605ebe9aa1cc8 Mon Sep 17 00:00:00 2001 From: Rico Suter Date: Tue, 23 Feb 2021 21:11:38 +0100 Subject: [PATCH 3/6] v10.3.8 --- .../NJsonSchema.CodeGeneration.CSharp.csproj | 2 +- .../NJsonSchema.CodeGeneration.TypeScript.csproj | 2 +- .../NJsonSchema.CodeGeneration.csproj | 2 +- src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj | 2 +- src/NJsonSchema/NJsonSchema.csproj | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj b/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj index 2cc47cdb2..de1f7264c 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj +++ b/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 10.3.7 + 10.3.8 json schema validation generator .net Copyright © Rico Suter, 2020 http://NJsonSchema.org diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj b/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj index 0d231fcf8..a3fb48ba5 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj +++ b/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 10.3.7 + 10.3.8 json schema validation generator .net Copyright © Rico Suter, 2020 http://NJsonSchema.org diff --git a/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj b/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj index 772e0b4f2..6f1d2c801 100644 --- a/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj +++ b/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 10.3.7 + 10.3.8 json schema validation generator .net Copyright © Rico Suter, 2020 http://NJsonSchema.org diff --git a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj index bbf2957fc..f873994c0 100644 --- a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj +++ b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net45 JSON Schema reader, generator and validator for .NET - 10.3.7 + 10.3.8 json schema validation generator .net Copyright © Rico Suter, 2020 http://NJsonSchema.org diff --git a/src/NJsonSchema/NJsonSchema.csproj b/src/NJsonSchema/NJsonSchema.csproj index bcc2b28a4..4392a1163 100644 --- a/src/NJsonSchema/NJsonSchema.csproj +++ b/src/NJsonSchema/NJsonSchema.csproj @@ -2,7 +2,7 @@ netstandard1.0;netstandard2.0;net40;net45 JSON Schema reader, generator and validator for .NET - 10.3.7 + 10.3.8 json schema validation generator .net Copyright © Rico Suter, 2020 http://NJsonSchema.org From 8f245dbd8f4822655456846f17607a8606fe7830 Mon Sep 17 00:00:00 2001 From: Martin Shields Date: Wed, 24 Feb 2021 09:52:41 +0000 Subject: [PATCH 4/6] Added uuid format for schema validation (#1325) - Created new uuid format validator - Added testing for format --- .../Validation/FormatUuidTests.cs | 46 +++++++++++++++++++ .../FormatValidators/UuidFormatValidator.cs | 34 ++++++++++++++ .../Validation/JsonSchemaValidator.cs | 3 +- .../Validation/ValidationErrorKind.cs | 5 +- 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/NJsonSchema.Tests/Validation/FormatUuidTests.cs create mode 100644 src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs diff --git a/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs b/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs new file mode 100644 index 000000000..ae88bc9da --- /dev/null +++ b/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs @@ -0,0 +1,46 @@ +using System; +using System.Linq; +using Newtonsoft.Json.Linq; +using NJsonSchema.Validation; +using Xunit; + +namespace NJsonSchema.Tests.Validation +{ + public class FormatUuidTests + { + [Fact] + public void When_format_uuid_incorrect_then_validation_succeeds() + { + //// Arrange + var schema = new JsonSchema(); + schema.Type = JsonObjectType.String; + schema.Format = JsonFormatStrings.Uuid; + + var token = new JValue("test"); + + //// Act + var errors = schema.Validate(token); + + //// Assert + Assert.Equal(ValidationErrorKind.UuidExpected, errors.First().Kind); + } + + [Fact] + public void When_format_uuid_correct_then_validation_succeeds() + { + //// Arrange + var schema = new JsonSchema(); + schema.Type = JsonObjectType.String; + schema.Format = JsonFormatStrings.Uuid; + + var uuid = Guid.NewGuid().ToString(); + var token = new JValue(uuid); + + //// Act + var errors = schema.Validate(token); + + //// Assert + Assert.Empty(errors); + } + } +} \ No newline at end of file diff --git a/src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs new file mode 100644 index 000000000..f8ddf4da9 --- /dev/null +++ b/src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs @@ -0,0 +1,34 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using Newtonsoft.Json.Linq; +using System; + +namespace NJsonSchema.Validation.FormatValidators +{ + /// Validator for "Uuid" format. + public class UuidFormatValidator : IFormatValidator + { + /// Gets the format attribute's value. + #pragma warning disable CS0618 // Type or member is obsolete + public string Format { get; } = JsonFormatStrings.Uuid; + #pragma warning restore CS0618 // Type or member is obsolete + + /// Gets the kind of error produced by validator. + public ValidationErrorKind ValidationErrorKind { get; } = ValidationErrorKind.UuidExpected; + + /// Validates format of given value. + /// String value. + /// Type of token holding the value. + /// True if value is correct for given format, False - if not. + public bool IsValid(string value, JTokenType tokenType) + { + return Guid.TryParse(value, out Guid guid); + } + } +} diff --git a/src/NJsonSchema/Validation/JsonSchemaValidator.cs b/src/NJsonSchema/Validation/JsonSchemaValidator.cs index 0154add03..ab96bd35e 100644 --- a/src/NJsonSchema/Validation/JsonSchemaValidator.cs +++ b/src/NJsonSchema/Validation/JsonSchemaValidator.cs @@ -33,7 +33,8 @@ public class JsonSchemaValidator new TimeSpanFormatValidator(), new UriFormatValidator(), new ByteFormatValidator(), - new Base64FormatValidator() + new Base64FormatValidator(), + new UuidFormatValidator() }; private readonly IDictionary _formatValidatorsMap; diff --git a/src/NJsonSchema/Validation/ValidationErrorKind.cs b/src/NJsonSchema/Validation/ValidationErrorKind.cs index 84d5d08ee..ffbff6a5c 100644 --- a/src/NJsonSchema/Validation/ValidationErrorKind.cs +++ b/src/NJsonSchema/Validation/ValidationErrorKind.cs @@ -141,6 +141,9 @@ public enum ValidationErrorKind Base64Expected, /// No type of the types does validate (check error details in ). - NoTypeValidates + NoTypeValidates, + + /// A valid UUID is expected. + UuidExpected, } } \ No newline at end of file From 48946d7b3bd35db81393ecf94d5a8482cce95326 Mon Sep 17 00:00:00 2001 From: Chebotov Nickolay Date: Wed, 24 Feb 2021 23:56:25 +0300 Subject: [PATCH 5/6] Fix System.Text.Json.Serialization.JsonStringEnumConverter parameter for System.Text.Json.Serialization.JsonConverter (#1327) --- .../Templates/Class.liquid | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid index 223498e35..851cb759d 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid @@ -76,7 +76,7 @@ {% endif -%} {% if property.IsStringEnum -%} {% if UseSystemTextJson -%} - [System.Text.Json.Serialization.JsonConverter(System.Text.Json.Serialization.JsonStringEnumConverter)] + [System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))] {% else -%} [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] {% endif -%} @@ -98,11 +98,11 @@ get { return {{ property.FieldName }}; } {% if property.HasSetter -%} {% if RenderInpc -%} - {{PropertySetterAccessModifier}}set + {{PropertySetterAccessModifier}}set { if ({{ property.FieldName }} != value) { - {{ property.FieldName }} = value; + {{ property.FieldName }} = value; RaisePropertyChanged(); } } From d8f5302ed5315167c361358735a90718de7abcd0 Mon Sep 17 00:00:00 2001 From: twodogmbh <47099800+twodogmbh@users.noreply.github.com> Date: Thu, 25 Feb 2021 11:24:24 +0100 Subject: [PATCH 6/6] Issue in generation of C# classes with inheritance fixed (#1319) * Bug in JsonReferenceResolver fixed which prevented the referencing of definitions in other files. * Issue in the JsonSchemaAppender for the generation of classes fixed when the definitions reference the root schema. When the passed schema is already the root schema the method returns immediately instead of throwing an exception. Issue in the CSharpTypeResolver for the generation of base classes without properties fixed. When classes don't contain properties but contain the "descriminator" attribute they should still be generated. Co-authored-by: Egli, Florian {DMMA~Rotkreuz-Tro} --- .../InheritanceTests.cs | 31 ++++++++++++++++++- ...nSchema.CodeGeneration.CSharp.Tests.csproj | 4 +++ .../References/Animal.json | 25 +++++++++++++++ .../CSharpTypeResolver.cs | 1 + src/NJsonSchema/JsonSchemaAppender.cs | 2 +- 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Animal.json diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs index 188c83fde..066cdc0c7 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; +using System.IO; +using System.Reflection; using System.Runtime.Serialization; using System.Threading.Tasks; using Newtonsoft.Json; @@ -132,5 +133,33 @@ public async Task When_property_references_any_schema_with_inheritance_then_prop //// Assert Assert.Contains("public Dog Dog { get; set; }", code); } + + [Fact] + public async Task When_definitions_inherit_from_root_schema() + { + //// Arrange + var path = GetTestDirectory() + "/References/Animal.json"; + + //// Act + var schema = await JsonSchema.FromFileAsync(path); + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Record }); + + //// Act + var code = generator.GenerateFile(); + + //// Assert + Assert.Contains("public abstract partial class Animal", code); + Assert.Contains("public partial class Cat : Animal", code); + Assert.Contains("public partial class PersianCat : Cat", code); + Assert.Contains("[JsonInheritanceAttribute(\"Cat\", typeof(Cat))]", code); + Assert.Contains("[JsonInheritanceAttribute(\"PersianCat\", typeof(PersianCat))]", code); + } + + private string GetTestDirectory() + { + var codeBase = Assembly.GetExecutingAssembly().CodeBase; + var uri = new UriBuilder(codeBase); + return Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path)); + } } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj index 36fd4994c..5a8583248 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj @@ -7,6 +7,7 @@ + @@ -16,6 +17,9 @@ Always + + Always + Always diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Animal.json b/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Animal.json new file mode 100644 index 000000000..6e8b7fd21 --- /dev/null +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Animal.json @@ -0,0 +1,25 @@ +{ + "title": "Animal", + "type": "object", + "x-abstract": true, + "discriminator": "discriminator", + + "definitions": { + "Cat": { + "type": "object", + "allOf": [ + { + "$ref": "Animal.json" + } + ] + }, + "PersianCat": { + "type": "object", + "allOf": [ + { + "$ref": "#/definitions/Cat" + } + ] + } + } +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs index fb26820d8..2fb426cdd 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs @@ -78,6 +78,7 @@ schema is JsonSchemaProperty property && var markAsNullableType = Settings.GenerateNullableReferenceTypes && isNullable; if (schema.ActualTypeSchema.IsAnyType && + schema.ActualDiscriminator == null && schema.InheritedSchema == null && // not in inheritance hierarchy schema.AllOf.Count == 0 && !Types.Keys.Contains(schema) && diff --git a/src/NJsonSchema/JsonSchemaAppender.cs b/src/NJsonSchema/JsonSchemaAppender.cs index 5591a77ef..019b073e0 100644 --- a/src/NJsonSchema/JsonSchemaAppender.cs +++ b/src/NJsonSchema/JsonSchemaAppender.cs @@ -44,7 +44,7 @@ public virtual void AppendSchema(JsonSchema schema, string typeNameHint) if (schema == RootObject) { - throw new ArgumentException("The root schema cannot be appended."); + return; } if (!RootSchema.Definitions.Values.Contains(schema))