Skip to content

Commit

Permalink
Extract annotations into own package (#1641)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
trejjam authored Oct 31, 2023
1 parent cce0e36 commit 8189415
Show file tree
Hide file tree
Showing 79 changed files with 698 additions and 538 deletions.
23 changes: 11 additions & 12 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 => _ => _
Expand Down Expand Up @@ -156,7 +155,7 @@ protected override void OnBuildInitialized()
nugetVersion += "-" + VersionSuffix;
}
EnsureCleanDirectory(ArtifactsDirectory);
ArtifactsDirectory.CreateOrCleanDirectory();
DotNetPack(s => s
.SetProcessWorkingDirectory(SourceDirectory)
Expand All @@ -170,4 +169,4 @@ protected override void OnBuildInitialized()
.SetContinuousIntegrationBuild(IsServerBuild)
);
});
}
}
24 changes: 24 additions & 0 deletions src/NJsonSchema.Annotations/CanBeNullAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//-----------------------------------------------------------------------
// <copyright file="CanBeNullAttribute.cs" company="NJsonSchema">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System;

namespace NJsonSchema.Annotations;

/// <summary>Indicates that the value of the marked element is nullable.</summary>
[AttributeUsage(
AttributeTargets.Method |
AttributeTargets.Parameter |
AttributeTargets.Property |
AttributeTargets.ReturnValue |
AttributeTargets.Delegate |
AttributeTargets.Field |
AttributeTargets.Event)]
public class CanBeNullAttribute : Attribute
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

using System.Collections.Generic;

namespace NJsonSchema.Annotations
namespace NJsonSchema.Annotations;

/// <summary>Interface to add an extension data property to a class or property, implementation needs to inherit from System.Attribute.</summary>
public interface IJsonSchemaExtensionDataAttribute
{
/// <summary>Interface to add an extension data property to a class or property, implementation needs to inherit from System.Attribute.</summary>
public interface IJsonSchemaExtensionDataAttribute
{
/// <summary>Gets the extension data.</summary>
IReadOnlyDictionary<string, object> ExtensionData { get; }
}
/// <summary>Gets the extension data.</summary>
IReadOnlyDictionary<string, object> ExtensionData { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

using System;

namespace NJsonSchema.Annotations
namespace NJsonSchema.Annotations;

/// <summary>Annotation to specify that array items or dictionary values are nullable.</summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue |
AttributeTargets.Field)]
public class ItemsCanBeNullAttribute : Attribute
{
/// <summary>Annotation to specify that array items or dictionary values are nullable.</summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue |
AttributeTargets.Field)]
public class ItemsCanBeNullAttribute : Attribute
{
}
}
84 changes: 84 additions & 0 deletions src/NJsonSchema.Annotations/JsonFormatStrings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//-----------------------------------------------------------------------
// <copyright file="JsonFormatStrings.cs" company="NJsonSchema">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System;

namespace NJsonSchema.Annotations;

/// <summary>Class containing the constants available as format string. </summary>
public static class JsonFormatStrings
{
/// <summary>Format for a <see cref="System.DateTime"/>. </summary>
public const string DateTime = "date-time";

/// <summary>Non-standard Format for a duration (time span)<see cref="TimeSpan"/>. </summary>
public const string TimeSpan = "time-span";

/// <summary>Format for a duration (time span) as of 2019-09 <see cref="TimeSpan"/>. </summary>
public const string Duration = "duration";

/// <summary>Format for an email. </summary>
public const string Email = "email";

/// <summary>Format for an URI. </summary>
public const string Uri = "uri";

/// <summary>Format for an GUID. </summary>
public const string Guid = "guid";

/// <summary>Format for an UUID (same as GUID). </summary>
[Obsolete("Now made redundant. Use \"guid\" instead.")]
public const string Uuid = "uuid";

/// <summary>Format for an integer. </summary>
public const string Integer = "int32";

/// <summary>Format for a long integer. </summary>
public const string Long = "int64";

/// <summary>Format for a unsigned long integer. </summary>
public const string ULong = "uint64";

/// <summary>Format for a double number. </summary>
public const string Double = "double";

/// <summary>Format for a float number. </summary>
public const string Float = "float";

/// <summary>Format for a decimal number. </summary>
public const string Decimal = "decimal";

/// <summary>Format for an IP v4 address. </summary>
public const string IpV4 = "ipv4";

/// <summary>Format for an IP v6 address. </summary>
public const string IpV6 = "ipv6";

/// <summary>Format for binary data encoded with Base64.</summary>
/// <remarks>Should not be used. Prefer using Byte property of <see cref="JsonFormatStrings"/></remarks>
[Obsolete("Now made redundant. Use \"byte\" instead.")]
public const string Base64 = "base64";

/// <summary>Format for a byte if used with numeric type or for base64 encoded value otherwise.</summary>
public const string Byte = "byte";

/// <summary>Format for a binary value.</summary>
public const string Binary = "binary";

/// <summary>Format for a hostname (DNS name).</summary>
public const string Hostname = "hostname";

/// <summary>Format for a phone number.</summary>
public const string Phone = "phone";

/// <summary>Format for a full date per RFC3339 Section 5.6.</summary>
public const string Date = "date";

/// <summary>Format for a full time per RFC3339 Section 5.6.</summary>
public const string Time = "time";
}
39 changes: 39 additions & 0 deletions src/NJsonSchema.Annotations/JsonObjectType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;

namespace NJsonSchema.Annotations;

/// <summary>
/// Enumeration of the possible object types.
///
/// Keep in sync with NJsonSchema.JsonObjectType
/// </summary>
[Flags]
public enum JsonObjectType
{
/// <summary>No object type. </summary>
None = 0,

/// <summary>An array. </summary>
Array = 1,

/// <summary>A boolean value. </summary>
Boolean = 2,

/// <summary>An integer value. </summary>
Integer = 4,

/// <summary>A null. </summary>
Null = 8,

/// <summary>An number value. </summary>
Number = 16,

/// <summary>An object. </summary>
Object = 32,

/// <summary>A string. </summary>
String = 64,

/// <summary>A file (used in Swagger specifications). </summary>
File = 128,
}
32 changes: 32 additions & 0 deletions src/NJsonSchema.Annotations/JsonSchemaAbstractAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//-----------------------------------------------------------------------
// <copyright file="JsonSchemaDateAttribute.cs" company="NJsonSchema">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System;

namespace NJsonSchema.Annotations;

/// <summary>Annotation to merge all inherited properties into this class/schema.</summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public class JsonSchemaAbstractAttribute : Attribute
{
/// <summary>Initializes a new instance of the <see cref="JsonSchemaAbstractAttribute"/> class.</summary>
public JsonSchemaAbstractAttribute()
{
IsAbstract = true;
}

/// <summary>Initializes a new instance of the <see cref="JsonSchemaAbstractAttribute"/> class.</summary>
/// <param name="isAbstract">The explicit flag to override the global setting (i.e. disable the generation for a type).</param>
public JsonSchemaAbstractAttribute(bool isAbstract)
{
IsAbstract = isAbstract;
}

/// <summary>Gets or sets a value indicating whether to set the x-abstract property for given type.</summary>
public bool IsAbstract { get; }
}
49 changes: 49 additions & 0 deletions src/NJsonSchema.Annotations/JsonSchemaAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//-----------------------------------------------------------------------
// <copyright file="JsonSchemaAttribute.cs" company="NJsonSchema">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System;

namespace NJsonSchema.Annotations;

/// <summary>Annotation to specify the JSON Schema type for the given class.</summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Struct |
AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class JsonSchemaAttribute : Attribute
{
/// <summary>Initializes a new instance of the <see cref="JsonSchemaAttribute"/> class.</summary>
public JsonSchemaAttribute()
{
Type = JsonObjectType.None;
}

/// <summary>Initializes a new instance of the <see cref="JsonSchemaAttribute" /> class.</summary>
/// <param name="name">The identifier of the schema which is used as key in the 'definitions' list.</param>
public JsonSchemaAttribute(string name) : this()
{
Name = name;
}

/// <summary>Initializes a new instance of the <see cref="JsonSchemaAttribute"/> class.</summary>
/// <param name="type">The JSON Schema type.</param>
public JsonSchemaAttribute(JsonObjectType type)
{
Type = type;
}

/// <summary>Gets or sets the name identifier of the schema which is used as key in the 'definitions' list.</summary>
public string? Name { get; set; }

/// <summary>Gets the JSON Schema type (default: <see cref="JsonObjectType.None"/>, i.e. derived from <see cref="System.Type"/>).</summary>
public JsonObjectType Type { get; private set; }

/// <summary>Gets or sets the JSON format type (default: <c>null</c>, i.e. derived from <see cref="System.Type"/>).</summary>
public string? Format { get; set; }

/// <summary>Gets or sets the array item type.</summary>
public Type? ArrayItem { get; set; }
}
20 changes: 20 additions & 0 deletions src/NJsonSchema.Annotations/JsonSchemaDateAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//-----------------------------------------------------------------------
// <copyright file="JsonSchemaDateAttribute.cs" company="NJsonSchema">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

namespace NJsonSchema.Annotations;

/// <summary>Annotation to mark a property or class as string type with format 'date'.</summary>
public class JsonSchemaDateAttribute : JsonSchemaAttribute
{
/// <summary>Initializes a new instance of the <see cref="JsonSchemaAttribute"/> class.</summary>
public JsonSchemaDateAttribute()
: base(JsonObjectType.String)
{
Format = JsonFormatStrings.Date;
}
}
Loading

0 comments on commit 8189415

Please sign in to comment.