From 8189415f31635ff441e3b4ff946b10065f910980 Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Tue, 31 Oct 2023 16:58:47 +0100 Subject: [PATCH] Extract annotations into own package (#1641) * Add NJsonSchema.Annotations.csproj * Reference NJsonSchema.Annotations.csproj in NJsonSchema.csproj * Update usings * Fix AnnotationsGenerationTests * Fix ReflectionServiceBase * Resolve NUKE build deprecation * Update usings * Fix comparing with line endings --- build/Build.cs | 23 +++-- .../CanBeNullAttribute.cs | 24 +++++ .../IJsonSchemaExtensionDataAttribute.cs | 13 ++- .../ItemsCanBeNullAttribute.cs | 13 ++- .../JsonFormatStrings.cs | 84 +++++++++++++++++ src/NJsonSchema.Annotations/JsonObjectType.cs | 39 ++++++++ .../JsonSchemaAbstractAttribute.cs | 32 +++++++ .../JsonSchemaAttribute.cs | 49 ++++++++++ .../JsonSchemaDateAttribute.cs | 20 +++++ .../JsonSchemaExtensionDataAttribute.cs | 41 +++++++++ .../JsonSchemaFlattenAttribute.cs | 32 +++++++ .../JsonSchemaIgnoreAttribute.cs | 11 ++- .../JsonSchemaPatternPropertiesAttribute.cs | 38 ++++++++ .../JsonSchemaProcessorAttribute.cs | 32 +++++++ .../JsonSchemaTypeAttribute.cs | 38 ++++++++ .../MultipleOfAttribute.cs | 33 +++++++ .../NJsonSchema.Annotations.csproj | 11 +++ .../NJsonSchema.Annotations.nuspec | 25 ++++++ .../NotNullAttribute.cs | 24 +++++ src/NJsonSchema.Annotations/NuGetIcon.png | Bin 0 -> 1477 bytes .../CSharpTypeResolver.cs | 3 +- .../CSharpValueGenerator.cs | 5 +- .../Models/EnumTemplateModel.cs | 1 + .../Models/PropertyModel.cs | 3 +- .../DefaultValueGeneratorTests.cs | 5 +- .../DataConversionGenerator.cs | 15 ++-- .../TypeScriptTypeResolver.cs | 3 +- .../TypeScriptValueGenerator.cs | 5 +- .../ValueGeneratorBase.cs | 3 +- .../Generation/AnnotationsGenerationTests.cs | 10 +-- .../PrimitiveTypeGenerationTests.cs | 1 + .../SampleJsonSchemaGeneratorTests.cs | 3 +- .../Generation/SchemaGenerationTests.cs | 3 +- .../SystemTextJsonInheritanceTests.cs | 55 ++++++++---- .../Validation/CustomValidationTests.cs | 1 + .../Validation/FormatBase64Tests.cs | 1 + .../Validation/FormatDateTests.cs | 1 + .../Validation/FormatDateTimeTests.cs | 1 + .../Validation/FormatEmailTests.cs | 1 + .../Validation/FormatGuidTests.cs | 1 + .../Validation/FormatHostnameTests.cs | 1 + .../Validation/FormatIpV4Tests.cs | 1 + .../Validation/FormatIpV6Tests.cs | 1 + .../Validation/FormatTimeSpanTests.cs | 1 + .../Validation/FormatTimeTests.cs | 1 + .../Validation/FormatUriTests.cs | 1 + .../Validation/FormatUuidTests.cs | 1 + src/NJsonSchema.sln | 19 ++++ .../Annotations/CanBeNullAttribute.cs | 25 ------ .../JsonSchemaAbstractAttribute.cs | 33 ------- .../Annotations/JsonSchemaAttribute.cs | 50 ----------- .../Annotations/JsonSchemaDateAttribute.cs | 21 ----- .../JsonSchemaExtensionDataAttribute.cs | 42 --------- .../Annotations/JsonSchemaFlattenAttribute.cs | 33 ------- .../JsonSchemaPatternPropertiesAttribute.cs | 39 -------- .../JsonSchemaProcessorAttribute.cs | 33 ------- .../Annotations/JsonSchemaTypeAttribute.cs | 39 -------- .../Annotations/MultipleOfAttribute.cs | 34 ------- .../Annotations/NotNullAttribute.cs | 25 ------ .../Generation/ReflectionServiceBase.cs | 16 +++- .../Generation/SampleJsonDataGenerator.cs | 3 +- src/NJsonSchema/JsonFormatStrings.cs | 85 ------------------ src/NJsonSchema/JsonObjectType.cs | 8 +- src/NJsonSchema/JsonSchema.cs | 1 + src/NJsonSchema/NJsonSchema.csproj | 6 +- src/NJsonSchema/SampleJsonSchemaGenerator.cs | 1 + .../FormatValidators/Base64FormatValidator.cs | 1 + .../FormatValidators/ByteFormatValidator.cs | 1 + .../FormatValidators/DateFormatValidator.cs | 1 + .../DateTimeFormatValidator.cs | 1 + .../FormatValidators/EmailFormatValidator.cs | 1 + .../FormatValidators/GuidFormatValidator.cs | 1 + .../HostnameFormatValidator.cs | 1 + .../FormatValidators/IpV4FormatValidator.cs | 1 + .../FormatValidators/IpV6FormatValidator.cs | 1 + .../FormatValidators/TimeFormatValidator.cs | 1 + .../TimeSpanFormatValidator.cs | 1 + .../FormatValidators/UriFormatValidator.cs | 1 + .../FormatValidators/UuidFormatValidator.cs | 1 + 79 files changed, 698 insertions(+), 538 deletions(-) create mode 100644 src/NJsonSchema.Annotations/CanBeNullAttribute.cs rename src/{NJsonSchema/Annotations => NJsonSchema.Annotations}/IJsonSchemaExtensionDataAttribute.cs (55%) rename src/{NJsonSchema/Annotations => NJsonSchema.Annotations}/ItemsCanBeNullAttribute.cs (55%) create mode 100644 src/NJsonSchema.Annotations/JsonFormatStrings.cs create mode 100644 src/NJsonSchema.Annotations/JsonObjectType.cs create mode 100644 src/NJsonSchema.Annotations/JsonSchemaAbstractAttribute.cs create mode 100644 src/NJsonSchema.Annotations/JsonSchemaAttribute.cs create mode 100644 src/NJsonSchema.Annotations/JsonSchemaDateAttribute.cs create mode 100644 src/NJsonSchema.Annotations/JsonSchemaExtensionDataAttribute.cs create mode 100644 src/NJsonSchema.Annotations/JsonSchemaFlattenAttribute.cs rename src/{NJsonSchema/Annotations => NJsonSchema.Annotations}/JsonSchemaIgnoreAttribute.cs (61%) create mode 100644 src/NJsonSchema.Annotations/JsonSchemaPatternPropertiesAttribute.cs create mode 100644 src/NJsonSchema.Annotations/JsonSchemaProcessorAttribute.cs create mode 100644 src/NJsonSchema.Annotations/JsonSchemaTypeAttribute.cs create mode 100644 src/NJsonSchema.Annotations/MultipleOfAttribute.cs create mode 100644 src/NJsonSchema.Annotations/NJsonSchema.Annotations.csproj create mode 100644 src/NJsonSchema.Annotations/NJsonSchema.Annotations.nuspec create mode 100644 src/NJsonSchema.Annotations/NotNullAttribute.cs create mode 100644 src/NJsonSchema.Annotations/NuGetIcon.png delete mode 100644 src/NJsonSchema/Annotations/CanBeNullAttribute.cs delete mode 100644 src/NJsonSchema/Annotations/JsonSchemaAbstractAttribute.cs delete mode 100644 src/NJsonSchema/Annotations/JsonSchemaAttribute.cs delete mode 100644 src/NJsonSchema/Annotations/JsonSchemaDateAttribute.cs delete mode 100644 src/NJsonSchema/Annotations/JsonSchemaExtensionDataAttribute.cs delete mode 100644 src/NJsonSchema/Annotations/JsonSchemaFlattenAttribute.cs delete mode 100644 src/NJsonSchema/Annotations/JsonSchemaPatternPropertiesAttribute.cs delete mode 100644 src/NJsonSchema/Annotations/JsonSchemaProcessorAttribute.cs delete mode 100644 src/NJsonSchema/Annotations/JsonSchemaTypeAttribute.cs delete mode 100644 src/NJsonSchema/Annotations/MultipleOfAttribute.cs delete mode 100644 src/NJsonSchema/Annotations/NotNullAttribute.cs delete mode 100644 src/NJsonSchema/JsonFormatStrings.cs diff --git a/build/Build.cs b/build/Build.cs index 38fcc84cd..9bc6e74f8 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -11,7 +11,6 @@ using Nuke.Common.Tools.DotNet; using Nuke.Common.Utilities.Collections; -using static Nuke.Common.IO.FileSystemTasks; using static Nuke.Common.Logger; using static Nuke.Common.Tools.DotNet.DotNetTasks; @@ -50,13 +49,13 @@ string DetermineVersionPrefix() if (!string.IsNullOrWhiteSpace(versionPrefix)) { IsTaggedBuild = true; - Info($"Tag version {versionPrefix} from Git found, using it as version prefix"); + Serilog.Log.Information("Tag version {VersionPrefix} from Git found, using it as version prefix", versionPrefix); } else { - var propsDocument = XDocument.Parse(TextTasks.ReadAllText(SourceDirectory / "Directory.Build.props")); + var propsDocument = XDocument.Parse((SourceDirectory / "Directory.Build.props").ReadAllText()); versionPrefix = propsDocument.Element("Project").Element("PropertyGroup").Element("VersionPrefix").Value; - Info($"Version prefix {versionPrefix} read from Directory.Build.props"); + Serilog.Log.Information("Version prefix {VersionPrefix} read from Directory.Build.props", versionPrefix); } return versionPrefix; @@ -85,18 +84,18 @@ protected override void OnBuildInitialized() } using var _ = Block("BUILD SETUP"); - Info("Configuration:\t" + Configuration); - Info("Version prefix:\t" + VersionPrefix); - Info("Version suffix:\t" + VersionSuffix); - Info("Tagged build:\t" + IsTaggedBuild); + Serilog.Log.Information("Configuration:\t {Configuration}" , Configuration); + Serilog.Log.Information("Version prefix:\t {VersionPrefix}" , VersionPrefix); + Serilog.Log.Information("Version suffix:\t {VersionSuffix}" , VersionSuffix); + Serilog.Log.Information("Tagged build:\t {IsTaggedBuild}" , IsTaggedBuild); } Target Clean => _ => _ .Before(Restore) .Executes(() => { - SourceDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory); - EnsureCleanDirectory(ArtifactsDirectory); + SourceDirectory.GlobDirectories("**/bin", "**/obj").ForEach(x => x.DeleteDirectory()); + ArtifactsDirectory.CreateOrCleanDirectory(); }); Target Restore => _ => _ @@ -156,7 +155,7 @@ protected override void OnBuildInitialized() nugetVersion += "-" + VersionSuffix; } - EnsureCleanDirectory(ArtifactsDirectory); + ArtifactsDirectory.CreateOrCleanDirectory(); DotNetPack(s => s .SetProcessWorkingDirectory(SourceDirectory) @@ -170,4 +169,4 @@ protected override void OnBuildInitialized() .SetContinuousIntegrationBuild(IsServerBuild) ); }); -} \ No newline at end of file +} diff --git a/src/NJsonSchema.Annotations/CanBeNullAttribute.cs b/src/NJsonSchema.Annotations/CanBeNullAttribute.cs new file mode 100644 index 000000000..0bd304c02 --- /dev/null +++ b/src/NJsonSchema.Annotations/CanBeNullAttribute.cs @@ -0,0 +1,24 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; + +namespace NJsonSchema.Annotations; + +/// Indicates that the value of the marked element is nullable. +[AttributeUsage( + AttributeTargets.Method | + AttributeTargets.Parameter | + AttributeTargets.Property | + AttributeTargets.ReturnValue | + AttributeTargets.Delegate | + AttributeTargets.Field | + AttributeTargets.Event)] +public class CanBeNullAttribute : Attribute +{ +} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/IJsonSchemaExtensionDataAttribute.cs b/src/NJsonSchema.Annotations/IJsonSchemaExtensionDataAttribute.cs similarity index 55% rename from src/NJsonSchema/Annotations/IJsonSchemaExtensionDataAttribute.cs rename to src/NJsonSchema.Annotations/IJsonSchemaExtensionDataAttribute.cs index be706493a..d440dfc44 100644 --- a/src/NJsonSchema/Annotations/IJsonSchemaExtensionDataAttribute.cs +++ b/src/NJsonSchema.Annotations/IJsonSchemaExtensionDataAttribute.cs @@ -8,12 +8,11 @@ using System.Collections.Generic; -namespace NJsonSchema.Annotations +namespace NJsonSchema.Annotations; + +/// Interface to add an extension data property to a class or property, implementation needs to inherit from System.Attribute. +public interface IJsonSchemaExtensionDataAttribute { - /// Interface to add an extension data property to a class or property, implementation needs to inherit from System.Attribute. - public interface IJsonSchemaExtensionDataAttribute - { - /// Gets the extension data. - IReadOnlyDictionary ExtensionData { get; } - } + /// Gets the extension data. + IReadOnlyDictionary ExtensionData { get; } } \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/ItemsCanBeNullAttribute.cs b/src/NJsonSchema.Annotations/ItemsCanBeNullAttribute.cs similarity index 55% rename from src/NJsonSchema/Annotations/ItemsCanBeNullAttribute.cs rename to src/NJsonSchema.Annotations/ItemsCanBeNullAttribute.cs index 95d5d67e8..d376f8c3a 100644 --- a/src/NJsonSchema/Annotations/ItemsCanBeNullAttribute.cs +++ b/src/NJsonSchema.Annotations/ItemsCanBeNullAttribute.cs @@ -8,12 +8,11 @@ using System; -namespace NJsonSchema.Annotations +namespace NJsonSchema.Annotations; + +/// Annotation to specify that array items or dictionary values are nullable. +[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue | + AttributeTargets.Field)] +public class ItemsCanBeNullAttribute : Attribute { - /// Annotation to specify that array items or dictionary values are nullable. - [AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue | - AttributeTargets.Field)] - public class ItemsCanBeNullAttribute : Attribute - { - } } \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/JsonFormatStrings.cs b/src/NJsonSchema.Annotations/JsonFormatStrings.cs new file mode 100644 index 000000000..0531c654b --- /dev/null +++ b/src/NJsonSchema.Annotations/JsonFormatStrings.cs @@ -0,0 +1,84 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; + +namespace NJsonSchema.Annotations; + +/// Class containing the constants available as format string. +public static class JsonFormatStrings +{ + /// Format for a . + public const string DateTime = "date-time"; + + /// Non-standard Format for a duration (time span). + public const string TimeSpan = "time-span"; + + /// Format for a duration (time span) as of 2019-09 . + public const string Duration = "duration"; + + /// Format for an email. + public const string Email = "email"; + + /// Format for an URI. + public const string Uri = "uri"; + + /// Format for an GUID. + public const string Guid = "guid"; + + /// Format for an UUID (same as GUID). + [Obsolete("Now made redundant. Use \"guid\" instead.")] + public const string Uuid = "uuid"; + + /// Format for an integer. + public const string Integer = "int32"; + + /// Format for a long integer. + public const string Long = "int64"; + + /// Format for a unsigned long integer. + public const string ULong = "uint64"; + + /// Format for a double number. + public const string Double = "double"; + + /// Format for a float number. + public const string Float = "float"; + + /// Format for a decimal number. + public const string Decimal = "decimal"; + + /// Format for an IP v4 address. + public const string IpV4 = "ipv4"; + + /// Format for an IP v6 address. + public const string IpV6 = "ipv6"; + + /// Format for binary data encoded with Base64. + /// Should not be used. Prefer using Byte property of + [Obsolete("Now made redundant. Use \"byte\" instead.")] + public const string Base64 = "base64"; + + /// Format for a byte if used with numeric type or for base64 encoded value otherwise. + public const string Byte = "byte"; + + /// Format for a binary value. + public const string Binary = "binary"; + + /// Format for a hostname (DNS name). + public const string Hostname = "hostname"; + + /// Format for a phone number. + public const string Phone = "phone"; + + /// Format for a full date per RFC3339 Section 5.6. + public const string Date = "date"; + + /// Format for a full time per RFC3339 Section 5.6. + public const string Time = "time"; +} \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/JsonObjectType.cs b/src/NJsonSchema.Annotations/JsonObjectType.cs new file mode 100644 index 000000000..79ea33cc0 --- /dev/null +++ b/src/NJsonSchema.Annotations/JsonObjectType.cs @@ -0,0 +1,39 @@ +using System; + +namespace NJsonSchema.Annotations; + +/// +/// Enumeration of the possible object types. +/// +/// Keep in sync with NJsonSchema.JsonObjectType +/// +[Flags] +public enum JsonObjectType +{ + /// No object type. + None = 0, + + /// An array. + Array = 1, + + /// A boolean value. + Boolean = 2, + + /// An integer value. + Integer = 4, + + /// A null. + Null = 8, + + /// An number value. + Number = 16, + + /// An object. + Object = 32, + + /// A string. + String = 64, + + /// A file (used in Swagger specifications). + File = 128, +} \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/JsonSchemaAbstractAttribute.cs b/src/NJsonSchema.Annotations/JsonSchemaAbstractAttribute.cs new file mode 100644 index 000000000..b97f36f7e --- /dev/null +++ b/src/NJsonSchema.Annotations/JsonSchemaAbstractAttribute.cs @@ -0,0 +1,32 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; + +namespace NJsonSchema.Annotations; + +/// Annotation to merge all inherited properties into this class/schema. +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] +public class JsonSchemaAbstractAttribute : Attribute +{ + /// Initializes a new instance of the class. + public JsonSchemaAbstractAttribute() + { + IsAbstract = true; + } + + /// Initializes a new instance of the class. + /// The explicit flag to override the global setting (i.e. disable the generation for a type). + public JsonSchemaAbstractAttribute(bool isAbstract) + { + IsAbstract = isAbstract; + } + + /// Gets or sets a value indicating whether to set the x-abstract property for given type. + public bool IsAbstract { get; } +} \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/JsonSchemaAttribute.cs b/src/NJsonSchema.Annotations/JsonSchemaAttribute.cs new file mode 100644 index 000000000..50b4b61b6 --- /dev/null +++ b/src/NJsonSchema.Annotations/JsonSchemaAttribute.cs @@ -0,0 +1,49 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; + +namespace NJsonSchema.Annotations; + +/// Annotation to specify the JSON Schema type for the given class. +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Struct | + AttributeTargets.Parameter | AttributeTargets.ReturnValue)] +public class JsonSchemaAttribute : Attribute +{ + /// Initializes a new instance of the class. + public JsonSchemaAttribute() + { + Type = JsonObjectType.None; + } + + /// Initializes a new instance of the class. + /// The identifier of the schema which is used as key in the 'definitions' list. + public JsonSchemaAttribute(string name) : this() + { + Name = name; + } + + /// Initializes a new instance of the class. + /// The JSON Schema type. + public JsonSchemaAttribute(JsonObjectType type) + { + Type = type; + } + + /// Gets or sets the name identifier of the schema which is used as key in the 'definitions' list. + public string? Name { get; set; } + + /// Gets the JSON Schema type (default: , i.e. derived from ). + public JsonObjectType Type { get; private set; } + + /// Gets or sets the JSON format type (default: null, i.e. derived from ). + public string? Format { get; set; } + + /// Gets or sets the array item type. + public Type? ArrayItem { get; set; } +} \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/JsonSchemaDateAttribute.cs b/src/NJsonSchema.Annotations/JsonSchemaDateAttribute.cs new file mode 100644 index 000000000..3695ff6e9 --- /dev/null +++ b/src/NJsonSchema.Annotations/JsonSchemaDateAttribute.cs @@ -0,0 +1,20 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +namespace NJsonSchema.Annotations; + +/// Annotation to mark a property or class as string type with format 'date'. +public class JsonSchemaDateAttribute : JsonSchemaAttribute +{ + /// Initializes a new instance of the class. + public JsonSchemaDateAttribute() + : base(JsonObjectType.String) + { + Format = JsonFormatStrings.Date; + } +} \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/JsonSchemaExtensionDataAttribute.cs b/src/NJsonSchema.Annotations/JsonSchemaExtensionDataAttribute.cs new file mode 100644 index 000000000..9bfe8f6de --- /dev/null +++ b/src/NJsonSchema.Annotations/JsonSchemaExtensionDataAttribute.cs @@ -0,0 +1,41 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; +using System.Collections.Generic; + +namespace NJsonSchema.Annotations; + +/// Adds an extension data property to a class or property. +/// +[AttributeUsage( + AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue, + AllowMultiple = true)] +public class JsonSchemaExtensionDataAttribute : Attribute, IJsonSchemaExtensionDataAttribute +{ + /// Initializes a new instance of the class. + /// The key. + /// The value. + public JsonSchemaExtensionDataAttribute(string key, object value) + { + Key = key; + Value = value; + } + + /// Gets the property name. + public string Key { get; } + + /// Gets the value. + public object Value { get; } + + /// + public IReadOnlyDictionary ExtensionData => new Dictionary + { + { Key, Value } + }; +} \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/JsonSchemaFlattenAttribute.cs b/src/NJsonSchema.Annotations/JsonSchemaFlattenAttribute.cs new file mode 100644 index 000000000..c3d04fc02 --- /dev/null +++ b/src/NJsonSchema.Annotations/JsonSchemaFlattenAttribute.cs @@ -0,0 +1,32 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; + +namespace NJsonSchema.Annotations; + +/// Annotation to merge all inherited properties into this class/schema. +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] +public class JsonSchemaFlattenAttribute : Attribute +{ + /// Initializes a new instance of the class. + public JsonSchemaFlattenAttribute() + { + Flatten = true; + } + + /// Initializes a new instance of the class. + /// The explicit flag to override the global setting (i.e. disable the generation for a type). + public JsonSchemaFlattenAttribute(bool flatten) + { + Flatten = flatten; + } + + /// Gets or sets a value indicating whether to flatten the given type. + public bool Flatten { get; } +} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/JsonSchemaIgnoreAttribute.cs b/src/NJsonSchema.Annotations/JsonSchemaIgnoreAttribute.cs similarity index 61% rename from src/NJsonSchema/Annotations/JsonSchemaIgnoreAttribute.cs rename to src/NJsonSchema.Annotations/JsonSchemaIgnoreAttribute.cs index 929aa6648..c2eec7ef4 100644 --- a/src/NJsonSchema/Annotations/JsonSchemaIgnoreAttribute.cs +++ b/src/NJsonSchema.Annotations/JsonSchemaIgnoreAttribute.cs @@ -8,11 +8,10 @@ using System; -namespace NJsonSchema.Annotations +namespace NJsonSchema.Annotations; + +/// Indicates that the marked class is ignored during the JSON Schema generation. +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)] +public class JsonSchemaIgnoreAttribute : Attribute { - /// Indicates that the marked class is ignored during the JSON Schema generation. - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)] - public class JsonSchemaIgnoreAttribute : Attribute - { - } } \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/JsonSchemaPatternPropertiesAttribute.cs b/src/NJsonSchema.Annotations/JsonSchemaPatternPropertiesAttribute.cs new file mode 100644 index 000000000..60b48b42a --- /dev/null +++ b/src/NJsonSchema.Annotations/JsonSchemaPatternPropertiesAttribute.cs @@ -0,0 +1,38 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; + +namespace NJsonSchema.Annotations; + +/// Annotation to specify the JSON Schema pattern properties. +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)] +public class JsonSchemaPatternPropertiesAttribute : Attribute +{ + /// Initializes a new instance of the class. + /// The pattern property regular expression. + public JsonSchemaPatternPropertiesAttribute(string regularExpression) + : this(regularExpression, null) + { + } + + /// Initializes a new instance of the class. + /// The pattern property regular expression. + /// The pattern properties type. + public JsonSchemaPatternPropertiesAttribute(string regularExpression, Type? type) + { + RegularExpression = regularExpression; + Type = type; + } + + /// Gets the pattern properties regular expression. + public string RegularExpression { get; } + + /// Gets the pattern properties type. + public Type? Type { get; } +} \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/JsonSchemaProcessorAttribute.cs b/src/NJsonSchema.Annotations/JsonSchemaProcessorAttribute.cs new file mode 100644 index 000000000..2c9ec7ec0 --- /dev/null +++ b/src/NJsonSchema.Annotations/JsonSchemaProcessorAttribute.cs @@ -0,0 +1,32 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/NSwag/NSwag/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; + +namespace NJsonSchema.Annotations; + +/// Registers an schema processor for the given class. +/// +[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] +public class JsonSchemaProcessorAttribute : Attribute +{ + /// Initializes a new instance of the class. + /// The schema processor type (must implement ISchemaProcessor). + /// The parameters. + public JsonSchemaProcessorAttribute(Type type, params object[] parameters) + { + Type = type; + Parameters = parameters; + } + + /// Gets or sets the type of the operation processor (must implement ISchemaProcessor). + public Type Type { get; set; } + + /// Gets or sets the type of the constructor parameters. + public object[] Parameters { get; set; } +} \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/JsonSchemaTypeAttribute.cs b/src/NJsonSchema.Annotations/JsonSchemaTypeAttribute.cs new file mode 100644 index 000000000..4b6950ec0 --- /dev/null +++ b/src/NJsonSchema.Annotations/JsonSchemaTypeAttribute.cs @@ -0,0 +1,38 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/NSwag/NSwag/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; + +namespace NJsonSchema.Annotations; + +/// Specifies the type to use for JSON Schema generation. +[AttributeUsage( + AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Struct, + AllowMultiple = false)] +public class JsonSchemaTypeAttribute : Attribute +{ + /// Initializes a new instance of the class. + /// The type of the schema. + public JsonSchemaTypeAttribute(Type type) + { + Type = type; + } + + /// Gets or sets the response type. + public Type Type { get; } + + /// Gets or sets a value indicating whether the schema can be null (default: null = unchanged). + public bool IsNullable + { + get => IsNullableRaw ?? false; + set => IsNullableRaw = value; + } + + /// Gets the raw nullable information. + public bool? IsNullableRaw { get; internal set; } // required because attribute properties cannot be bool? +} \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/MultipleOfAttribute.cs b/src/NJsonSchema.Annotations/MultipleOfAttribute.cs new file mode 100644 index 000000000..6e81e9041 --- /dev/null +++ b/src/NJsonSchema.Annotations/MultipleOfAttribute.cs @@ -0,0 +1,33 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; + +namespace NJsonSchema.Annotations; + +/// Attribute to set the multipleOf parameter of a JSON Schema. +[AttributeUsage(AttributeTargets.Property)] +public class MultipleOfAttribute : Attribute +{ + /// Initializes a new instance of the class. + /// The multipleOf value. + public MultipleOfAttribute(double multipleOf) + { + MultipleOf = (decimal) multipleOf; + } + + /// Initializes a new instance of the class. + /// The multipleOf value. + public MultipleOfAttribute(decimal multipleOf) + { + MultipleOf = multipleOf; + } + + /// Gets the value whose modulo the the JSON value must be zero. + public decimal MultipleOf { get; private set; } +} \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/NJsonSchema.Annotations.csproj b/src/NJsonSchema.Annotations/NJsonSchema.Annotations.csproj new file mode 100644 index 000000000..3b266d9a0 --- /dev/null +++ b/src/NJsonSchema.Annotations/NJsonSchema.Annotations.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.0 + + + + + + + \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/NJsonSchema.Annotations.nuspec b/src/NJsonSchema.Annotations/NJsonSchema.Annotations.nuspec new file mode 100644 index 000000000..fb49f3ff2 --- /dev/null +++ b/src/NJsonSchema.Annotations/NJsonSchema.Annotations.nuspec @@ -0,0 +1,25 @@ + + + + $id$ + $version$ + $author$ + $description$ + json schema validation generator .net + http://NJsonSchema.org + MIT + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/NotNullAttribute.cs b/src/NJsonSchema.Annotations/NotNullAttribute.cs new file mode 100644 index 000000000..85e5f2ce8 --- /dev/null +++ b/src/NJsonSchema.Annotations/NotNullAttribute.cs @@ -0,0 +1,24 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +using System; + +namespace NJsonSchema.Annotations; + +/// Indicates that the value of the marked element could never be null. +[AttributeUsage( + AttributeTargets.Method | + AttributeTargets.Parameter | + AttributeTargets.Property | + AttributeTargets.ReturnValue | + AttributeTargets.Delegate | + AttributeTargets.Field | + AttributeTargets.Event)] +public class NotNullAttribute : Attribute +{ +} \ No newline at end of file diff --git a/src/NJsonSchema.Annotations/NuGetIcon.png b/src/NJsonSchema.Annotations/NuGetIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..bd2ae54163259976156fb6768d8286cf1499fbc3 GIT binary patch literal 1477 zcmV;$1v>hPP)0ssI2NT@F)00004XF*Lt006O% z3;baP00009a7bBm000XT000XT0n*)m`~Uy|8FWQhbW?9;ba!ELWdLwtX>N2bZe?^J zG%heMF*(%MvSa`N1wu(gK~!i%?V0;iRAm^)`wPsjlXjczGWml-&G9-}>5I_sj3?VF@=vr0s0ez;OS-a3ANthWlRF_6*x1;7C93 z#?BKEj7ppJ>v{8+?X`uUWG~ow4_oio0gz1?A<=Y6yh8>pZQf(z5{!)-#W_R*K$U&FS@TPewl+n2aKZu}5g5>VD&J=^Qken~(s zx9j=*$Js6!DZnS4sdibz(Q z7IEu>jAhTUDJc@0-Nt_xF1K?}{kyuNn7KONmqE^QB#}wCy4{mgBWP#sHbISu z#K^@XlV-w@k5MWZbNDW>JPCMTLmB3%Y-83CI_XI{0Fpm1^*(60Sx_S&d3}Neym{{i zjG{bog_Gx4US&Flsl+7k>6fg7n7*&D>D30Tq?hE6Nab+B=d|j?Bn=U z=NKP@lb$}P{M++{cX^G8TNJeX&G~FfK$1O`1pLKMhcQRhKlBIeCB)@P;L3{$Lf}c5 zO-Wf1NqkWZuIlf8A|`n{E7ai}!!%X-=O&2DlNd@?V1f{ZkBy2*l;7jUB;XH@7Gj#p z?yxP&m~A>JM{@P26`v#zC*M9W(vKBXRgE8-4rbs6RdA4i;YxrNRBpGcrl}G)sDg$B z#)VVo%~(XmzNrBP4GEZCM66&U0XMW(W1UEHBso`(B$j!%l2X2!O$9`x-~-s#p>jx0_FY$Hkql@d$b+ z^=m~W;I^|(+|r<~3!N}#!$}PBKuPAO+;MSq0IL~sBrri&G?aETYku2tjBS^(Qj&FV zKgCa9AgTAleo4UagpAdUIFg0w_u@7c9Ez)Q7qGoDR!Ra++p*4SAK^aZa!BYExdR&} zBqo7p=7i!EY@@sbAOXV(Q*h|jW3AQZ?iTKi$yYxg?drAgPEq!Mg1EwEaC`B(x8tk*LoaiTbROsLvXS`mB+t&l-vP ftdXeAx@rFaqU{9@s5B7z00000NkvXXu0mjf%b2>w literal 0 HcmV?d00001 diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs index fab7be3a6..592f75595 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs @@ -6,6 +6,7 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- +using NJsonSchema.Annotations; using System; using System.Linq; @@ -292,4 +293,4 @@ private string ResolveDictionary(JsonSchema schema) return $"{Settings.DictionaryType}<{keyType}, {valueType}>"; } } -} +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs index 4ae5598df..851724d8d 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs @@ -6,6 +6,7 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- +using NJsonSchema.Annotations; using System; using System.Collections.Generic; using System.Globalization; @@ -20,7 +21,7 @@ public class CSharpValueGenerator : ValueGeneratorBase { "System.Guid", "System.Uri" - }; + }; /// Initializes a new instance of the class. /// The settings. @@ -121,4 +122,4 @@ protected override string GetEnumDefaultValue(JsonSchema schema, JsonSchema actu return _settings.Namespace + "." + base.GetEnumDefaultValue(schema, actualSchema, typeNameHint, typeResolver); } } -} +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/EnumTemplateModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/EnumTemplateModel.cs index d61dba0d7..efc2698bb 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/EnumTemplateModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/EnumTemplateModel.cs @@ -6,6 +6,7 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- +using NJsonSchema.Annotations; using System.Collections.Generic; using System.Linq; using NJsonSchema.CodeGeneration.Models; diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs index 125a24c7b..771167aed 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs @@ -6,6 +6,7 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- +using NJsonSchema.Annotations; using System.Globalization; using NJsonSchema.CodeGeneration.Models; @@ -322,4 +323,4 @@ public bool RenderRegularExpressionAttribute return schema.Format; } } -} +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.Tests/DefaultValueGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.Tests/DefaultValueGeneratorTests.cs index c092a4a81..f50b27c76 100644 --- a/src/NJsonSchema.CodeGeneration.Tests/DefaultValueGeneratorTests.cs +++ b/src/NJsonSchema.CodeGeneration.Tests/DefaultValueGeneratorTests.cs @@ -1,4 +1,5 @@ -using NJsonSchema.CodeGeneration.CSharp; +using NJsonSchema.Annotations; +using NJsonSchema.CodeGeneration.CSharp; using NJsonSchema.CodeGeneration.TypeScript; using Xunit; @@ -275,4 +276,4 @@ public void When_schema_has_required_abstract_class_it_generates_no_default_valu Assert.Null(typescriptValue); } } -} +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/DataConversionGenerator.cs b/src/NJsonSchema.CodeGeneration.TypeScript/DataConversionGenerator.cs index f388a0b88..ce5923a35 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/DataConversionGenerator.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript/DataConversionGenerator.cs @@ -6,6 +6,7 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- +using NJsonSchema.Annotations; using System; namespace NJsonSchema.CodeGeneration.TypeScript @@ -76,17 +77,17 @@ private static object CreateModel(DataConversionParameters parameters) IsDictionaryValueNewableObject = typeSchema.AdditionalPropertiesSchema != null && IsNewableObject(typeSchema.AdditionalPropertiesSchema, parameters), IsDictionaryValueDate = IsDate(typeSchema.AdditionalPropertiesSchema?.ActualSchema?.Format, parameters.Settings.DateTimeType), IsDictionaryValueDateTime = IsDateTime(typeSchema.AdditionalPropertiesSchema?.ActualSchema?.Format, parameters.Settings.DateTimeType), - - IsDictionaryValueNewableArray = + + IsDictionaryValueNewableArray = typeSchema.AdditionalPropertiesSchema?.ActualSchema?.IsArray == true && typeSchema.AdditionalPropertiesSchema.Item != null && IsNewableObject(typeSchema.AdditionalPropertiesSchema.Item, parameters), - - DictionaryValueArrayItemType = + + DictionaryValueArrayItemType = typeSchema.AdditionalPropertiesSchema?.ActualSchema?.IsArray == true ? - parameters.Resolver.TryResolve(typeSchema.AdditionalPropertiesSchema.Item, "Anonymous") ?? "any" : + parameters.Resolver.TryResolve(typeSchema.AdditionalPropertiesSchema.Item, "Anonymous") ?? "any" : "any", - + // Array IsArray = typeSchema.IsArray, ArrayItemType = parameters.Resolver.TryResolve(typeSchema.Item, parameters.TypeNameHint) ?? "any", @@ -314,4 +315,4 @@ private static bool IsNewableObject(JsonSchema? schema, DataConversionParameters return false; } } -} +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs b/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs index b8e430a20..b1821edf1 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs @@ -6,6 +6,7 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- +using NJsonSchema.Annotations; using System; using System.Linq; @@ -348,4 +349,4 @@ private string GetNullableItemType(JsonSchema schema, string itemType) return itemType; } } -} +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptValueGenerator.cs b/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptValueGenerator.cs index d6408c9b6..41c87e80b 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptValueGenerator.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptValueGenerator.cs @@ -6,6 +6,7 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- +using NJsonSchema.Annotations; using System.Collections.Generic; namespace NJsonSchema.CodeGeneration.TypeScript @@ -72,7 +73,7 @@ schema.Format is not null && var isOptional = (schema as JsonSchemaProperty)?.IsRequired == false; if (schema != null && allowsNull == false && isOptional == false) { - if (typeResolver.GeneratesType(schema) && + if (typeResolver.GeneratesType(schema) && !schema.ActualTypeSchema.IsEnumeration && !schema.ActualTypeSchema.IsAbstract) { @@ -104,4 +105,4 @@ public override string GetNumericValue(JsonObjectType type, object value, string return ConvertNumberToString(value); } } -} +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration/ValueGeneratorBase.cs b/src/NJsonSchema.CodeGeneration/ValueGeneratorBase.cs index 6b632c951..5225df649 100644 --- a/src/NJsonSchema.CodeGeneration/ValueGeneratorBase.cs +++ b/src/NJsonSchema.CodeGeneration/ValueGeneratorBase.cs @@ -6,6 +6,7 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- +using NJsonSchema.Annotations; using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -182,4 +183,4 @@ protected string ConvertNumberToString(object value) return value.ToString(); } } -} +} \ No newline at end of file diff --git a/src/NJsonSchema.Tests/Generation/AnnotationsGenerationTests.cs b/src/NJsonSchema.Tests/Generation/AnnotationsGenerationTests.cs index 33012ea4d..50a228d7d 100644 --- a/src/NJsonSchema.Tests/Generation/AnnotationsGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/AnnotationsGenerationTests.cs @@ -15,10 +15,10 @@ public class AnnotationClass { public MyPoint Point { get; set; } - [JsonSchema(JsonObjectType.String, Format = "point")] + [JsonSchema(Annotations.JsonObjectType.String, Format = "point")] public AnnotationClass ClassAsString { get; set; } - [JsonSchema(JsonObjectType.String, Format = "point")] + [JsonSchema(Annotations.JsonObjectType.String, Format = "point")] public class MyPoint { public decimal X { get; set; } @@ -145,7 +145,7 @@ public async Task When_multipleOf_is_fraction_then_it_is_validated_correctly() Assert.Equal(0, errors.Count); } - [JsonSchema(JsonObjectType.Array, ArrayItem = typeof(string))] + [JsonSchema(Annotations.JsonObjectType.Array, ArrayItem = typeof(string))] public class ArrayModel : IEnumerable { public IEnumerator GetEnumerator() @@ -172,7 +172,7 @@ public async Task When_class_has_array_item_type_defined_then_schema_has_this_it Assert.Equal(JsonObjectType.String, schema.Item.Type); } - [JsonSchema(JsonObjectType.Array, ArrayItem = typeof(string))] + [JsonSchema(Annotations.JsonObjectType.Array, ArrayItem = typeof(string))] public class ArrayModel : List { } @@ -197,7 +197,7 @@ public class MyStructContainer public MyStruct? NullableStruct { get; set; } } - [JsonSchema(JsonObjectType.String)] + [JsonSchema(Annotations.JsonObjectType.String)] public struct MyStruct { } diff --git a/src/NJsonSchema.Tests/Generation/PrimitiveTypeGenerationTests.cs b/src/NJsonSchema.Tests/Generation/PrimitiveTypeGenerationTests.cs index 0d6d6b345..a3f7b7f02 100644 --- a/src/NJsonSchema.Tests/Generation/PrimitiveTypeGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/PrimitiveTypeGenerationTests.cs @@ -2,6 +2,7 @@ using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using Newtonsoft.Json; +using NJsonSchema.Annotations; using NJsonSchema.NewtonsoftJson.Generation; using NodaTime; using Xunit; diff --git a/src/NJsonSchema.Tests/Generation/SampleJsonSchemaGeneratorTests.cs b/src/NJsonSchema.Tests/Generation/SampleJsonSchemaGeneratorTests.cs index 2053e3a4f..8b73760db 100644 --- a/src/NJsonSchema.Tests/Generation/SampleJsonSchemaGeneratorTests.cs +++ b/src/NJsonSchema.Tests/Generation/SampleJsonSchemaGeneratorTests.cs @@ -1,4 +1,5 @@ -using Xunit; +using NJsonSchema.Annotations; +using Xunit; namespace NJsonSchema.Tests.Generation { diff --git a/src/NJsonSchema.Tests/Generation/SchemaGenerationTests.cs b/src/NJsonSchema.Tests/Generation/SchemaGenerationTests.cs index 77957e7bc..9dc899457 100644 --- a/src/NJsonSchema.Tests/Generation/SchemaGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/SchemaGenerationTests.cs @@ -1,4 +1,5 @@ -using NJsonSchema.NewtonsoftJson.Generation; +using NJsonSchema.Annotations; +using NJsonSchema.NewtonsoftJson.Generation; using System; using System.Collections.Generic; using System.ComponentModel; diff --git a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs index e7694e2a4..f35b93125 100644 --- a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs +++ b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs @@ -5,6 +5,21 @@ namespace NJsonSchema.Tests.Generation.SystemTextJson { +#if NETFRAMEWORK + file static class StringExtensions { + /// + /// Mimic .NET 6+ String.ReplaceLineEndings + /// + public static string ReplaceLineEndings(this string content, string lineSeparator = "\n") + { + return string.Join( + lineSeparator, + content.Replace("\r\n", "\n").Split('\r', '\n', '\f', '\u0085', '\u2028', '\u2029') + ); + } + } +#endif + public class SystemTextJsonInheritanceTests { public class Apple : Fruit @@ -30,19 +45,22 @@ public async Task When_using_JsonInheritanceAttribute_and_SystemTextJson_then_sc { //// Act var schema = JsonSchema.FromType(); - var data = schema.ToJson(); + var data = schema.ToJson().ReplaceLineEndings(); //// Assert Assert.NotNull(data); Assert.Contains(@"""a"": """, data); Assert.Contains(@"""o"": """, data); - Assert.Contains(@"""discriminator"": { - ""propertyName"": ""k"", - ""mapping"": { - ""a"": ""#/definitions/Apple"", - ""o"": ""#/definitions/Orange"" - } - },", data); + Assert.Contains( + """ + "discriminator": { + "propertyName": "k", + "mapping": { + "a": "#/definitions/Apple", + "o": "#/definitions/Orange" + } + }, + """.ReplaceLineEndings(), data); } #if !NETFRAMEWORK @@ -70,21 +88,24 @@ public async Task When_using_native_attributes_in_SystemTextJson_then_schema_is_ { //// Act var schema = JsonSchema.FromType(); - var data = schema.ToJson(); + var data = schema.ToJson().ReplaceLineEndings(); //// Assert Assert.NotNull(data); Assert.Contains(@"""a"": """, data); Assert.Contains(@"""o"": """, data); - Assert.Contains(@"""discriminator"": { - ""propertyName"": ""k"", - ""mapping"": { - ""a"": ""#/definitions/Apple2"", - ""o"": ""#/definitions/Orange2"" - } - },", data); + Assert.Contains( + """ + "discriminator": { + "propertyName": "k", + "mapping": { + "a": "#/definitions/Apple2", + "o": "#/definitions/Orange2" + } + }, + """.ReplaceLineEndings(), data); } #endif } -} +} \ No newline at end of file diff --git a/src/NJsonSchema.Tests/Validation/CustomValidationTests.cs b/src/NJsonSchema.Tests/Validation/CustomValidationTests.cs index 495375434..2aa32cf9e 100644 --- a/src/NJsonSchema.Tests/Validation/CustomValidationTests.cs +++ b/src/NJsonSchema.Tests/Validation/CustomValidationTests.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using NJsonSchema.Validation.FormatValidators; using System; diff --git a/src/NJsonSchema.Tests/Validation/FormatBase64Tests.cs b/src/NJsonSchema.Tests/Validation/FormatBase64Tests.cs index 78976a636..3366829e2 100644 --- a/src/NJsonSchema.Tests/Validation/FormatBase64Tests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatBase64Tests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.Tests/Validation/FormatDateTests.cs b/src/NJsonSchema.Tests/Validation/FormatDateTests.cs index b7d7586ae..7ffd9d1bf 100644 --- a/src/NJsonSchema.Tests/Validation/FormatDateTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatDateTests.cs @@ -1,5 +1,6 @@ using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs b/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs index a8377c38e..894b9a25b 100644 --- a/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs @@ -1,5 +1,6 @@ using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.Tests/Validation/FormatEmailTests.cs b/src/NJsonSchema.Tests/Validation/FormatEmailTests.cs index 1152a2976..8082d19d1 100644 --- a/src/NJsonSchema.Tests/Validation/FormatEmailTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatEmailTests.cs @@ -1,5 +1,6 @@ using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.Tests/Validation/FormatGuidTests.cs b/src/NJsonSchema.Tests/Validation/FormatGuidTests.cs index 8e9067c2a..ff22d0180 100644 --- a/src/NJsonSchema.Tests/Validation/FormatGuidTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatGuidTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.Tests/Validation/FormatHostnameTests.cs b/src/NJsonSchema.Tests/Validation/FormatHostnameTests.cs index ec2bf8301..9f5fdbb9d 100644 --- a/src/NJsonSchema.Tests/Validation/FormatHostnameTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatHostnameTests.cs @@ -1,5 +1,6 @@ using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.Tests/Validation/FormatIpV4Tests.cs b/src/NJsonSchema.Tests/Validation/FormatIpV4Tests.cs index c3070d032..6a764c371 100644 --- a/src/NJsonSchema.Tests/Validation/FormatIpV4Tests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatIpV4Tests.cs @@ -1,5 +1,6 @@ using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.Tests/Validation/FormatIpV6Tests.cs b/src/NJsonSchema.Tests/Validation/FormatIpV6Tests.cs index f143ab13e..01e7a37e7 100644 --- a/src/NJsonSchema.Tests/Validation/FormatIpV6Tests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatIpV6Tests.cs @@ -1,5 +1,6 @@ using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.Tests/Validation/FormatTimeSpanTests.cs b/src/NJsonSchema.Tests/Validation/FormatTimeSpanTests.cs index c6e1d43f4..21cf64406 100644 --- a/src/NJsonSchema.Tests/Validation/FormatTimeSpanTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatTimeSpanTests.cs @@ -1,5 +1,6 @@ using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.Tests/Validation/FormatTimeTests.cs b/src/NJsonSchema.Tests/Validation/FormatTimeTests.cs index 3e5c38412..f6741f00c 100644 --- a/src/NJsonSchema.Tests/Validation/FormatTimeTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatTimeTests.cs @@ -1,5 +1,6 @@ using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.Tests/Validation/FormatUriTests.cs b/src/NJsonSchema.Tests/Validation/FormatUriTests.cs index b3e056905..4e1d694b4 100644 --- a/src/NJsonSchema.Tests/Validation/FormatUriTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatUriTests.cs @@ -1,5 +1,6 @@ using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs b/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs index ae88bc9da..11fd9116f 100644 --- a/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatUuidTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Validation; using Xunit; diff --git a/src/NJsonSchema.sln b/src/NJsonSchema.sln index da8a1cf59..d1e1d59f0 100644 --- a/src/NJsonSchema.sln +++ b/src/NJsonSchema.sln @@ -46,6 +46,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "01 Core", "01 Core", "{CDBC EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "02 CodeGeneration", "02 CodeGeneration", "{62F296F1-8CE9-493C-9022-35F4C687460B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NJsonSchema.Annotations", "NJsonSchema.Annotations\NJsonSchema.Annotations.csproj", "{526020E1-D3B5-49F6-BE11-5F4402A02E92}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -274,6 +276,22 @@ Global {AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Release|x64.Build.0 = Release|Any CPU {AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Release|x86.ActiveCfg = Release|Any CPU {AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Release|x86.Build.0 = Release|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Debug|Any CPU.Build.0 = Debug|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Debug|ARM.ActiveCfg = Debug|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Debug|ARM.Build.0 = Debug|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Debug|x64.ActiveCfg = Debug|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Debug|x64.Build.0 = Debug|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Debug|x86.ActiveCfg = Debug|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Debug|x86.Build.0 = Debug|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Release|Any CPU.ActiveCfg = Release|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Release|Any CPU.Build.0 = Release|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Release|ARM.ActiveCfg = Release|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Release|ARM.Build.0 = Release|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Release|x64.ActiveCfg = Release|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Release|x64.Build.0 = Release|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Release|x86.ActiveCfg = Release|Any CPU + {526020E1-D3B5-49F6-BE11-5F4402A02E92}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -293,6 +311,7 @@ Global {3D8BA0A7-65AF-4328-82DC-8C131D9A9EA8} = {62F296F1-8CE9-493C-9022-35F4C687460B} {E767A007-6007-4898-B80A-FE4ACBF2C588} = {62F296F1-8CE9-493C-9022-35F4C687460B} {4F0A74F2-54BE-4747-8EDB-562ABB6E0B64} = {CDBC7AC4-9D3D-430D-86BF-CDAECBDD715A} + {526020E1-D3B5-49F6-BE11-5F4402A02E92} = {CDBC7AC4-9D3D-430D-86BF-CDAECBDD715A} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9D5EDC80-5611-493B-804B-8B364816952B} diff --git a/src/NJsonSchema/Annotations/CanBeNullAttribute.cs b/src/NJsonSchema/Annotations/CanBeNullAttribute.cs deleted file mode 100644 index fdb7396b8..000000000 --- a/src/NJsonSchema/Annotations/CanBeNullAttribute.cs +++ /dev/null @@ -1,25 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -using System; - -namespace NJsonSchema.Annotations -{ - /// Indicates that the value of the marked element is nullable. - [AttributeUsage( - AttributeTargets.Method | - AttributeTargets.Parameter | - AttributeTargets.Property | - AttributeTargets.ReturnValue | - AttributeTargets.Delegate | - AttributeTargets.Field | - AttributeTargets.Event)] - public class CanBeNullAttribute : Attribute - { - } -} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/JsonSchemaAbstractAttribute.cs b/src/NJsonSchema/Annotations/JsonSchemaAbstractAttribute.cs deleted file mode 100644 index 8cb762265..000000000 --- a/src/NJsonSchema/Annotations/JsonSchemaAbstractAttribute.cs +++ /dev/null @@ -1,33 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -using System; - -namespace NJsonSchema.Annotations -{ - /// Annotation to merge all inherited properties into this class/schema. - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] - public class JsonSchemaAbstractAttribute : Attribute - { - /// Initializes a new instance of the class. - public JsonSchemaAbstractAttribute() - { - IsAbstract = true; - } - - /// Initializes a new instance of the class. - /// The explicit flag to override the global setting (i.e. disable the generation for a type). - public JsonSchemaAbstractAttribute(bool isAbstract) - { - IsAbstract = isAbstract; - } - - /// Gets or sets a value indicating whether to set the x-abstract property for given type. - public bool IsAbstract { get; } - } -} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/JsonSchemaAttribute.cs b/src/NJsonSchema/Annotations/JsonSchemaAttribute.cs deleted file mode 100644 index 651b33099..000000000 --- a/src/NJsonSchema/Annotations/JsonSchemaAttribute.cs +++ /dev/null @@ -1,50 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -using System; - -namespace NJsonSchema.Annotations -{ - /// Annotation to specify the JSON Schema type for the given class. - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Struct | - AttributeTargets.Parameter | AttributeTargets.ReturnValue)] - public class JsonSchemaAttribute : Attribute - { - /// Initializes a new instance of the class. - public JsonSchemaAttribute() - { - Type = JsonObjectType.None; - } - - /// Initializes a new instance of the class. - /// The identifier of the schema which is used as key in the 'definitions' list. - public JsonSchemaAttribute(string name) : this() - { - Name = name; - } - - /// Initializes a new instance of the class. - /// The JSON Schema type. - public JsonSchemaAttribute(JsonObjectType type) - { - Type = type; - } - - /// Gets or sets the name identifier of the schema which is used as key in the 'definitions' list. - public string? Name { get; set; } - - /// Gets the JSON Schema type (default: , i.e. derived from ). - public JsonObjectType Type { get; private set; } - - /// Gets or sets the JSON format type (default: null, i.e. derived from ). - public string? Format { get; set; } - - /// Gets or sets the array item type. - public Type? ArrayItem { get; set; } - } -} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/JsonSchemaDateAttribute.cs b/src/NJsonSchema/Annotations/JsonSchemaDateAttribute.cs deleted file mode 100644 index f6632234a..000000000 --- a/src/NJsonSchema/Annotations/JsonSchemaDateAttribute.cs +++ /dev/null @@ -1,21 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -namespace NJsonSchema.Annotations -{ - /// Annotation to mark a property or class as string type with format 'date'. - public class JsonSchemaDateAttribute : JsonSchemaAttribute - { - /// Initializes a new instance of the class. - public JsonSchemaDateAttribute() - : base(JsonObjectType.String) - { - Format = JsonFormatStrings.Date; - } - } -} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/JsonSchemaExtensionDataAttribute.cs b/src/NJsonSchema/Annotations/JsonSchemaExtensionDataAttribute.cs deleted file mode 100644 index 2db8a781f..000000000 --- a/src/NJsonSchema/Annotations/JsonSchemaExtensionDataAttribute.cs +++ /dev/null @@ -1,42 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -using System; -using System.Collections.Generic; - -namespace NJsonSchema.Annotations -{ - /// Adds an extension data property to a class or property. - /// - [AttributeUsage( - AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue, - AllowMultiple = true)] - public class JsonSchemaExtensionDataAttribute : Attribute, IJsonSchemaExtensionDataAttribute - { - /// Initializes a new instance of the class. - /// The key. - /// The value. - public JsonSchemaExtensionDataAttribute(string key, object value) - { - Key = key; - Value = value; - } - - /// Gets the property name. - public string Key { get; } - - /// Gets the value. - public object Value { get; } - - /// - public IReadOnlyDictionary ExtensionData => new Dictionary - { - { Key, Value } - }; - } -} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/JsonSchemaFlattenAttribute.cs b/src/NJsonSchema/Annotations/JsonSchemaFlattenAttribute.cs deleted file mode 100644 index 2c9a4aad4..000000000 --- a/src/NJsonSchema/Annotations/JsonSchemaFlattenAttribute.cs +++ /dev/null @@ -1,33 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -using System; - -namespace NJsonSchema.Annotations -{ - /// Annotation to merge all inherited properties into this class/schema. - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] - public class JsonSchemaFlattenAttribute : Attribute - { - /// Initializes a new instance of the class. - public JsonSchemaFlattenAttribute() - { - Flatten = true; - } - - /// Initializes a new instance of the class. - /// The explicit flag to override the global setting (i.e. disable the generation for a type). - public JsonSchemaFlattenAttribute(bool flatten) - { - Flatten = flatten; - } - - /// Gets or sets a value indicating whether to flatten the given type. - public bool Flatten { get; } - } -} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/JsonSchemaPatternPropertiesAttribute.cs b/src/NJsonSchema/Annotations/JsonSchemaPatternPropertiesAttribute.cs deleted file mode 100644 index b2c4fc512..000000000 --- a/src/NJsonSchema/Annotations/JsonSchemaPatternPropertiesAttribute.cs +++ /dev/null @@ -1,39 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -using System; - -namespace NJsonSchema.Annotations -{ - /// Annotation to specify the JSON Schema pattern properties. - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)] - public class JsonSchemaPatternPropertiesAttribute : Attribute - { - /// Initializes a new instance of the class. - /// The pattern property regular expression. - public JsonSchemaPatternPropertiesAttribute(string regularExpression) - : this(regularExpression, null) - { - } - - /// Initializes a new instance of the class. - /// The pattern property regular expression. - /// The pattern properties type. - public JsonSchemaPatternPropertiesAttribute(string regularExpression, Type? type) - { - RegularExpression = regularExpression; - Type = type; - } - - /// Gets the pattern properties regular expression. - public string RegularExpression { get; } - - /// Gets the pattern properties type. - public Type? Type { get; } - } -} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/JsonSchemaProcessorAttribute.cs b/src/NJsonSchema/Annotations/JsonSchemaProcessorAttribute.cs deleted file mode 100644 index c436138b3..000000000 --- a/src/NJsonSchema/Annotations/JsonSchemaProcessorAttribute.cs +++ /dev/null @@ -1,33 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/NSwag/NSwag/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -using System; - -namespace NJsonSchema.Annotations -{ - /// Registers an schema processor for the given class. - /// - [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] - public class JsonSchemaProcessorAttribute : Attribute - { - /// Initializes a new instance of the class. - /// The schema processor type (must implement ISchemaProcessor). - /// The parameters. - public JsonSchemaProcessorAttribute(Type type, params object[] parameters) - { - Type = type; - Parameters = parameters; - } - - /// Gets or sets the type of the operation processor (must implement ISchemaProcessor). - public Type Type { get; set; } - - /// Gets or sets the type of the constructor parameters. - public object[] Parameters { get; set; } - } -} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/JsonSchemaTypeAttribute.cs b/src/NJsonSchema/Annotations/JsonSchemaTypeAttribute.cs deleted file mode 100644 index 6334f16bd..000000000 --- a/src/NJsonSchema/Annotations/JsonSchemaTypeAttribute.cs +++ /dev/null @@ -1,39 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/NSwag/NSwag/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -using System; - -namespace NJsonSchema.Annotations -{ - /// Specifies the type to use for JSON Schema generation. - [AttributeUsage( - AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Struct, - AllowMultiple = false)] - public class JsonSchemaTypeAttribute : Attribute - { - /// Initializes a new instance of the class. - /// The type of the schema. - public JsonSchemaTypeAttribute(Type type) - { - Type = type; - } - - /// Gets or sets the response type. - public Type Type { get; } - - /// Gets or sets a value indicating whether the schema can be null (default: null = unchanged). - public bool IsNullable - { - get => IsNullableRaw ?? false; - set => IsNullableRaw = value; - } - - /// Gets the raw nullable information. - public bool? IsNullableRaw { get; internal set; } // required because attribute properties cannot be bool? - } -} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/MultipleOfAttribute.cs b/src/NJsonSchema/Annotations/MultipleOfAttribute.cs deleted file mode 100644 index 393c0df8d..000000000 --- a/src/NJsonSchema/Annotations/MultipleOfAttribute.cs +++ /dev/null @@ -1,34 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -using System; - -namespace NJsonSchema.Annotations -{ - /// Attribute to set the multipleOf parameter of a JSON Schema. - [AttributeUsage(AttributeTargets.Property)] - public class MultipleOfAttribute : Attribute - { - /// Initializes a new instance of the class. - /// The multipleOf value. - public MultipleOfAttribute(double multipleOf) - { - MultipleOf = (decimal) multipleOf; - } - - /// Initializes a new instance of the class. - /// The multipleOf value. - public MultipleOfAttribute(decimal multipleOf) - { - MultipleOf = multipleOf; - } - - /// Gets the value whose modulo the the JSON value must be zero. - public decimal MultipleOf { get; private set; } - } -} \ No newline at end of file diff --git a/src/NJsonSchema/Annotations/NotNullAttribute.cs b/src/NJsonSchema/Annotations/NotNullAttribute.cs deleted file mode 100644 index 310a38b9e..000000000 --- a/src/NJsonSchema/Annotations/NotNullAttribute.cs +++ /dev/null @@ -1,25 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -using System; - -namespace NJsonSchema.Annotations -{ - /// Indicates that the value of the marked element could never be null. - [AttributeUsage( - AttributeTargets.Method | - AttributeTargets.Parameter | - AttributeTargets.Property | - AttributeTargets.ReturnValue | - AttributeTargets.Delegate | - AttributeTargets.Field | - AttributeTargets.Event)] - public class NotNullAttribute : Attribute - { - } -} \ No newline at end of file diff --git a/src/NJsonSchema/Generation/ReflectionServiceBase.cs b/src/NJsonSchema/Generation/ReflectionServiceBase.cs index 97798d619..d7ff0c000 100644 --- a/src/NJsonSchema/Generation/ReflectionServiceBase.cs +++ b/src/NJsonSchema/Generation/ReflectionServiceBase.cs @@ -53,7 +53,12 @@ public JsonTypeDescription GetDescription(ContextualType contextualType, Referen var jsonSchemaAttribute = contextualType.GetContextOrTypeAttribute(true); ; if (jsonSchemaAttribute != null) { - var classType = jsonSchemaAttribute.Type != JsonObjectType.None ? jsonSchemaAttribute.Type : JsonObjectType.Object; + var classType = ToJsonObjectType(jsonSchemaAttribute.Type); + if (classType == JsonObjectType.None) + { + classType = JsonObjectType.Object; + } + var format = !string.IsNullOrEmpty(jsonSchemaAttribute.Format) ? jsonSchemaAttribute.Format : null; return JsonTypeDescription.Create(contextualType, classType, isNullable, format); } @@ -61,6 +66,13 @@ public JsonTypeDescription GetDescription(ContextualType contextualType, Referen return GetDescription(contextualType, settings, type, isNullable, defaultReferenceTypeNullHandling); } + /// + /// Convert into + /// + /// + /// + protected JsonObjectType ToJsonObjectType(Annotations.JsonObjectType jsonObjectType) => (JsonObjectType) jsonObjectType; + /// Creates a from a . /// The type. /// The settings. @@ -381,4 +393,4 @@ void IReflectionService.GenerateProperties(JsonSchema schema, ContextualType con GenerateProperties(schema, contextualType, (TSettings)settings, schemaGenerator, schemaResolver); } } -} +} \ No newline at end of file diff --git a/src/NJsonSchema/Generation/SampleJsonDataGenerator.cs b/src/NJsonSchema/Generation/SampleJsonDataGenerator.cs index 5b519e131..e5155c75d 100644 --- a/src/NJsonSchema/Generation/SampleJsonDataGenerator.cs +++ b/src/NJsonSchema/Generation/SampleJsonDataGenerator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System; using System.Collections.Generic; using System.Linq; @@ -201,4 +202,4 @@ private IEnumerable> GetPropertiesToGen return schema.ActualProperties.Where(x => required.Contains(x.Key)); } } -} +} \ No newline at end of file diff --git a/src/NJsonSchema/JsonFormatStrings.cs b/src/NJsonSchema/JsonFormatStrings.cs deleted file mode 100644 index 219ccd377..000000000 --- a/src/NJsonSchema/JsonFormatStrings.cs +++ /dev/null @@ -1,85 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) Rico Suter. All rights reserved. -// -// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md -// Rico Suter, mail@rsuter.com -//----------------------------------------------------------------------- - -using System; - -namespace NJsonSchema -{ - /// Class containing the constants available as format string. - public static class JsonFormatStrings - { - /// Format for a . - public const string DateTime = "date-time"; - - /// Non-standard Format for a duration (time span). - public const string TimeSpan = "time-span"; - - /// Format for a duration (time span) as of 2019-09 . - public const string Duration = "duration"; - - /// Format for an email. - public const string Email = "email"; - - /// Format for an URI. - public const string Uri = "uri"; - - /// Format for an GUID. - public const string Guid = "guid"; - - /// Format for an UUID (same as GUID). - [Obsolete("Now made redundant. Use \"guid\" instead.")] - public const string Uuid = "uuid"; - - /// Format for an integer. - public const string Integer = "int32"; - - /// Format for a long integer. - public const string Long = "int64"; - - /// Format for a unsigned long integer. - public const string ULong = "uint64"; - - /// Format for a double number. - public const string Double = "double"; - - /// Format for a float number. - public const string Float = "float"; - - /// Format for a decimal number. - public const string Decimal = "decimal"; - - /// Format for an IP v4 address. - public const string IpV4 = "ipv4"; - - /// Format for an IP v6 address. - public const string IpV6 = "ipv6"; - - /// Format for binary data encoded with Base64. - /// Should not be used. Prefer using Byte property of - [Obsolete("Now made redundant. Use \"byte\" instead.")] - public const string Base64 = "base64"; - - /// Format for a byte if used with numeric type or for base64 encoded value otherwise. - public const string Byte = "byte"; - - /// Format for a binary value. - public const string Binary = "binary"; - - /// Format for a hostname (DNS name). - public const string Hostname = "hostname"; - - /// Format for a phone number. - public const string Phone = "phone"; - - /// Format for a full date per RFC3339 Section 5.6. - public const string Date = "date"; - - /// Format for a full time per RFC3339 Section 5.6. - public const string Time = "time"; - } -} \ No newline at end of file diff --git a/src/NJsonSchema/JsonObjectType.cs b/src/NJsonSchema/JsonObjectType.cs index 524993118..17dc0cc4e 100644 --- a/src/NJsonSchema/JsonObjectType.cs +++ b/src/NJsonSchema/JsonObjectType.cs @@ -11,7 +11,11 @@ namespace NJsonSchema { - /// Enumeration of the possible object types. + /// + /// Enumeration of the possible object types. + /// + /// Keep in sync with + /// [Flags] public enum JsonObjectType { @@ -33,7 +37,7 @@ public enum JsonObjectType /// A null. [JsonProperty("null")] - Null = 8, + Null = 8, /// An number value. [JsonProperty("number")] diff --git a/src/NJsonSchema/JsonSchema.cs b/src/NJsonSchema/JsonSchema.cs index 3eb472c2e..99d493d48 100644 --- a/src/NJsonSchema/JsonSchema.cs +++ b/src/NJsonSchema/JsonSchema.cs @@ -19,6 +19,7 @@ using Namotion.Reflection; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using NJsonSchema.Collections; using NJsonSchema.Generation; using NJsonSchema.Infrastructure; diff --git a/src/NJsonSchema/NJsonSchema.csproj b/src/NJsonSchema/NJsonSchema.csproj index e71dacd3c..fce7d7ac3 100644 --- a/src/NJsonSchema/NJsonSchema.csproj +++ b/src/NJsonSchema/NJsonSchema.csproj @@ -1,4 +1,4 @@ - + net6.0;netstandard2.0;net462 @@ -8,6 +8,10 @@ bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml + + + + diff --git a/src/NJsonSchema/SampleJsonSchemaGenerator.cs b/src/NJsonSchema/SampleJsonSchemaGenerator.cs index f17db036b..9abf21851 100644 --- a/src/NJsonSchema/SampleJsonSchemaGenerator.cs +++ b/src/NJsonSchema/SampleJsonSchemaGenerator.cs @@ -13,6 +13,7 @@ using System.Text.RegularExpressions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; namespace NJsonSchema { diff --git a/src/NJsonSchema/Validation/FormatValidators/Base64FormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/Base64FormatValidator.cs index e3d8cea3a..12d935011 100644 --- a/src/NJsonSchema/Validation/FormatValidators/Base64FormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/Base64FormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; namespace NJsonSchema.Validation.FormatValidators { diff --git a/src/NJsonSchema/Validation/FormatValidators/ByteFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/ByteFormatValidator.cs index 1405f393d..0c7b5bcd3 100644 --- a/src/NJsonSchema/Validation/FormatValidators/ByteFormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/ByteFormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System.Text.RegularExpressions; namespace NJsonSchema.Validation.FormatValidators diff --git a/src/NJsonSchema/Validation/FormatValidators/DateFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/DateFormatValidator.cs index d8fe41286..75aa14c62 100644 --- a/src/NJsonSchema/Validation/FormatValidators/DateFormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/DateFormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System; using System.Globalization; diff --git a/src/NJsonSchema/Validation/FormatValidators/DateTimeFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/DateTimeFormatValidator.cs index 93d154563..22f4add90 100644 --- a/src/NJsonSchema/Validation/FormatValidators/DateTimeFormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/DateTimeFormatValidator.cs @@ -6,6 +6,7 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System; using System.Globalization; diff --git a/src/NJsonSchema/Validation/FormatValidators/EmailFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/EmailFormatValidator.cs index 72c3eb3f7..a343350f4 100644 --- a/src/NJsonSchema/Validation/FormatValidators/EmailFormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/EmailFormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System.Text.RegularExpressions; namespace NJsonSchema.Validation.FormatValidators diff --git a/src/NJsonSchema/Validation/FormatValidators/GuidFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/GuidFormatValidator.cs index 55ffa3375..f1e742f5d 100644 --- a/src/NJsonSchema/Validation/FormatValidators/GuidFormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/GuidFormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System; namespace NJsonSchema.Validation.FormatValidators diff --git a/src/NJsonSchema/Validation/FormatValidators/HostnameFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/HostnameFormatValidator.cs index 611125b7e..76d421a33 100644 --- a/src/NJsonSchema/Validation/FormatValidators/HostnameFormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/HostnameFormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System.Text.RegularExpressions; namespace NJsonSchema.Validation.FormatValidators diff --git a/src/NJsonSchema/Validation/FormatValidators/IpV4FormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/IpV4FormatValidator.cs index 448b72954..06d25e308 100644 --- a/src/NJsonSchema/Validation/FormatValidators/IpV4FormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/IpV4FormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System.Text.RegularExpressions; namespace NJsonSchema.Validation.FormatValidators diff --git a/src/NJsonSchema/Validation/FormatValidators/IpV6FormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/IpV6FormatValidator.cs index 0915986ae..f52336e18 100644 --- a/src/NJsonSchema/Validation/FormatValidators/IpV6FormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/IpV6FormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System; namespace NJsonSchema.Validation.FormatValidators diff --git a/src/NJsonSchema/Validation/FormatValidators/TimeFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/TimeFormatValidator.cs index 772107d88..31fba2d63 100644 --- a/src/NJsonSchema/Validation/FormatValidators/TimeFormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/TimeFormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System; using System.Globalization; diff --git a/src/NJsonSchema/Validation/FormatValidators/TimeSpanFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/TimeSpanFormatValidator.cs index ae47a21d1..4c420c279 100644 --- a/src/NJsonSchema/Validation/FormatValidators/TimeSpanFormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/TimeSpanFormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System; namespace NJsonSchema.Validation.FormatValidators diff --git a/src/NJsonSchema/Validation/FormatValidators/UriFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/UriFormatValidator.cs index a118830af..8283ba568 100644 --- a/src/NJsonSchema/Validation/FormatValidators/UriFormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/UriFormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System; namespace NJsonSchema.Validation.FormatValidators diff --git a/src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs index f8ddf4da9..c906b2bfd 100644 --- a/src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/UuidFormatValidator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using Newtonsoft.Json.Linq; +using NJsonSchema.Annotations; using System; namespace NJsonSchema.Validation.FormatValidators