Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract annotations into own package #1641

Merged
merged 8 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner

@RicoSuter RicoSuter Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we should put this in the NJsonSchema namespace but the NJsonSchema.Annotations pkg
and remove from NJsonSchema (and the same with JsonObjectType). What do you think? Might be confusing to have these two types twice... also I think it's not very nice/clean to have it twice

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the motive, but it feels strange to exclude one attribute from the annotations package.

The duplicity is there because of the Newtonsoft attribute. I can solve it instead with JsonConverter for that type. What do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry no my propsal:

JsonFormatStrings: Keep in NJsonSchema.Annotations but with namespace NJsonSchema (remove in NJsonSchema pkg)
JsonObjectType: Keep in NJsonSchema.Annotations but with namespace NJsonSchema (remove in NJsonSchema pkg)

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's ok for me. How do you prefer to handle these attributes?

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are from Newtonsoft package

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These? Link seems to be broken...

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These: JsonPropertyAttribute

https://github.com/RicoSuter/NJsonSchema/blob/8b30fcd633389a56c10ac23c9287348775f849a8/src/NJsonSchema/JsonObjectType.cs#L26C1-L28C19

using Newtonsoft.Json;

namespace NJsonSchema
{
    /// <summary>
    /// Enumeration of the possible object types.
    ///
    /// Keep in sync with <see cref="Annotations.JsonObjectType"/>
    /// </summary>
    [Flags]
    public enum JsonObjectType
    {
        /// <summary>No object type. </summary>
        [JsonProperty("none")] // <----------------------------- this ---------------<
        None = 0,

        /// <summary>An array. </summary>
        [JsonProperty("array")] // <----------------------------- and this ---------------<
        Array = 1,
...
    }
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems that these are not needed because of the magic here:

image

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me some time to find it :)

{
/// <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
Loading