Skip to content

Commit

Permalink
Merge pull request #1192 from json-api-dotnet/multi-target-annotations
Browse files Browse the repository at this point in the history
Multi-target Annotations against .NET Standard 1.0
  • Loading branch information
bkoelman authored Oct 3, 2022
2 parents 2e2c123 + 70c06b9 commit ea3ab72
Show file tree
Hide file tree
Showing 19 changed files with 191 additions and 2 deletions.
15 changes: 15 additions & 0 deletions JsonApiDotNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCore.Annotatio
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DatabasePerTenantExample", "src\Examples\DatabasePerTenantExample\DatabasePerTenantExample.csproj", "{60334658-BE51-43B3-9C4D-F2BBF56C89CE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AnnotationTests", "test\AnnotationTests\AnnotationTests.csproj", "{24B0C12F-38CD-4245-8785-87BEFAD55B00}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -267,6 +269,18 @@ Global
{60334658-BE51-43B3-9C4D-F2BBF56C89CE}.Release|x64.Build.0 = Release|Any CPU
{60334658-BE51-43B3-9C4D-F2BBF56C89CE}.Release|x86.ActiveCfg = Release|Any CPU
{60334658-BE51-43B3-9C4D-F2BBF56C89CE}.Release|x86.Build.0 = Release|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Debug|x64.ActiveCfg = Debug|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Debug|x64.Build.0 = Debug|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Debug|x86.ActiveCfg = Debug|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Debug|x86.Build.0 = Debug|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Release|Any CPU.Build.0 = Release|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Release|x64.ActiveCfg = Release|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Release|x64.Build.0 = Release|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Release|x86.ActiveCfg = Release|Any CPU
{24B0C12F-38CD-4245-8785-87BEFAD55B00}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -289,6 +303,7 @@ Global
{0E0B5C51-F7E2-4F40-A4E4-DED0E9731DC9} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
{83FF097C-C8C6-477B-9FAB-DF99B84978B5} = {7A2B7ADD-ECB5-4D00-AA6A-D45BD11C97CF}
{60334658-BE51-43B3-9C4D-F2BBF56C89CE} = {026FBC6C-AF76-4568-9B87-EC73457899FD}
{24B0C12F-38CD-4245-8785-87BEFAD55B00} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A2421882-8F0A-4905-928F-B550B192F9A4}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFrameworkName)</TargetFramework>
<TargetFrameworks>$(TargetFrameworkName);netstandard1.0</TargetFrameworks>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<RootNamespace>JsonApiDotNetCore</RootNamespace>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -27,4 +28,21 @@
<PackagePath></PackagePath>
</None>
</ItemGroup>

<!-- We multi-target against NetStandard solely to enable consumers to share their models project with .NET Framework code. -->

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.0' ">
<Using Remove="System.Net.Http" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.0' ">
<Compile Remove="**/*.cs" />
<Compile Include="**/*.shared.cs" />
<Compile Include="**/*.netstandard.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.0' ">
<Compile Remove="**/*.netstandard.cs" />
<None Include="**/*.netstandard.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using JetBrains.Annotations;

namespace JsonApiDotNetCore.Resources.Annotations;

/// <summary>
/// A simplified version, provided for convenience to multi-target against NetStandard. Does not actually work with JsonApiDotNetCore.
/// </summary>
[PublicAPI]
[AttributeUsage(AttributeTargets.Property)]
public sealed class AttrAttribute : ResourceFieldAttribute
{
/// <summary />
public AttrCapabilities Capabilities { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using JetBrains.Annotations;

namespace JsonApiDotNetCore.Resources.Annotations;

/// <summary>
/// Indicates capabilities that can be performed on an <see cref="AttrAttribute" />.
/// </summary>
[PublicAPI]
[Flags]
public enum AttrCapabilities
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using JetBrains.Annotations;

namespace JsonApiDotNetCore.Resources.Annotations;

/// <summary>
/// A simplified version, provided for convenience to multi-target against NetStandard. Does not actually work with JsonApiDotNetCore.
/// </summary>
[PublicAPI]
[AttributeUsage(AttributeTargets.Property)]
public sealed class EagerLoadAttribute : Attribute
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using JetBrains.Annotations;

namespace JsonApiDotNetCore.Resources.Annotations;

/// <summary>
/// A simplified version, provided for convenience to multi-target against NetStandard. Does not actually work with JsonApiDotNetCore.
/// </summary>
[PublicAPI]
[AttributeUsage(AttributeTargets.Property)]
public sealed class HasManyAttribute : RelationshipAttribute
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using JetBrains.Annotations;

namespace JsonApiDotNetCore.Resources.Annotations;

/// <summary>
/// A simplified version, provided for convenience to multi-target against NetStandard. Does not actually work with JsonApiDotNetCore.
/// </summary>
[PublicAPI]
[AttributeUsage(AttributeTargets.Property)]
public sealed class HasOneAttribute : RelationshipAttribute
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using JetBrains.Annotations;

namespace JsonApiDotNetCore.Resources.Annotations;

/// <summary>
/// A simplified version, provided for convenience to multi-target against NetStandard. Does not actually work with JsonApiDotNetCore.
/// </summary>
[PublicAPI]
public abstract class RelationshipAttribute : ResourceFieldAttribute
{
/// <summary />
public LinkTypes Links { get; set; } = LinkTypes.NotConfigured;

/// <summary />
public bool CanInclude { get; set; } = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using JetBrains.Annotations;

namespace JsonApiDotNetCore.Resources.Annotations;

/// <summary>
/// A simplified version, provided for convenience to multi-target against NetStandard. Does not actually work with JsonApiDotNetCore.
/// </summary>
[PublicAPI]
public abstract class ResourceFieldAttribute : Attribute
{
/// <summary />
public string PublicName { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using JetBrains.Annotations;

namespace JsonApiDotNetCore.Resources;

/// <summary>
/// Defines the basic contract for a JSON:API resource. All resource classes must implement <see cref="IIdentifiable{TId}" />.
/// </summary>
[PublicAPI]
public interface IIdentifiable
{
/// <summary>
Expand All @@ -22,6 +25,7 @@ public interface IIdentifiable
/// <typeparam name="TId">
/// The resource identifier type.
/// </typeparam>
[PublicAPI]
public interface IIdentifiable<TId> : IIdentifiable
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace JsonApiDotNetCore.Resources;

/// <summary>
/// A simplified version, provided for convenience to multi-target against NetStandard. Does not actually work with JsonApiDotNetCore.
/// </summary>
public abstract class Identifiable<TId> : IIdentifiable<TId>
{
/// <summary />
public virtual TId Id { get; set; } = default!;

/// <summary />
public string? StringId { get; set; }

/// <summary />
public string? LocalId { get; set; }
}
20 changes: 20 additions & 0 deletions test/AnnotationTests/AnnotationTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(TargetFrameworkName);netstandard1.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.0' ">
<Using Remove="System.Net.Http" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\JsonApiDotNetCore.Annotations\JsonApiDotNetCore.Annotations.csproj" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions test/AnnotationTests/Models/HiddenNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using JetBrains.Annotations;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Resources.Annotations;

namespace AnnotationTests.Models;

[PublicAPI]
[NoResource]
[ResourceLinks(TopLevelLinks = LinkTypes.None, ResourceLinks = LinkTypes.None, RelationshipLinks = LinkTypes.None)]
public sealed class HiddenNode : Identifiable<Guid>
{
[EagerLoad]
public HiddenNode? Parent { get; set; }
}
20 changes: 20 additions & 0 deletions test/AnnotationTests/Models/TreeNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using JetBrains.Annotations;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Resources.Annotations;

namespace AnnotationTests.Models;

[PublicAPI]
[Resource(PublicName = "tree-node", ControllerNamespace = "Models", GenerateControllerEndpoints = JsonApiEndpoints.Query)]
public sealed class TreeNode : Identifiable<long>
{
[Attr(PublicName = "name", Capabilities = AttrCapabilities.AllowSort)]
public string? DisplayName { get; set; }

[HasOne(PublicName = "orders", CanInclude = true, Links = LinkTypes.All)]
public TreeNode? Parent { get; set; }

[HasMany(PublicName = "orders", CanInclude = true, Links = LinkTypes.All)]
public ISet<TreeNode> Children { get; set; } = new HashSet<TreeNode>();
}

0 comments on commit ea3ab72

Please sign in to comment.