Skip to content
44 changes: 44 additions & 0 deletions src/JsonApiDotNetCore.OpenApi.Client/ApiException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using JetBrains.Annotations;

// We cannot rely on generating ApiException as soon as we are generating multiple clients, see https://github.com/RicoSuter/NSwag/issues/2839#issuecomment-776647377.
// Instead, we take the generated code as is and use it for the various clients.
#nullable disable
// @formatter:off
// ReSharper disable All
namespace JsonApiDotNetCore.OpenApi.Client.Exceptions
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
internal class ApiException : Exception
{
public int StatusCode { get; }

public string Response { get; }

public IReadOnlyDictionary<string, IEnumerable<string>> Headers { get; }

public ApiException(string message, int statusCode, string response, IReadOnlyDictionary<string, IEnumerable<string>> headers, Exception innerException)
: base(
message + "\n\nStatus: " + statusCode + "\nResponse: \n" +
(response == null ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException)
{
StatusCode = statusCode;
Response = response;
Headers = headers;
}

public override string ToString()
{
return $"HTTP Response: \n\n{Response}\n\n{base.ToString()}";
}}

internal sealed class ApiException<TResult> : ApiException
{
public TResult Result { get; }

public ApiException(string message, int statusCode, string response, IReadOnlyDictionary<string, IEnumerable<string>> headers, TResult result,
Exception innerException)
: base(message, statusCode, response, headers, innerException)
{
Result = result;
}}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Links;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
using JsonApiDotNetCore.Resources;

namespace JsonApiDotNetCore.OpenApi.JsonApiObjects.Documents;

// Types in the current namespace are never touched by ASP.NET ModelState validation, therefore using a non-nullable reference type for a property does not
// imply this property is required. Instead, we use [Required] explicitly, because this is how Swashbuckle is instructed to mark properties as required.
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
internal sealed class NullableResourceIdentifierResponseDocument<TResource> : NullableSingleData<ResourceIdentifierObject<TResource>>
where TResource : IIdentifiable
{
[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;

[JsonPropertyName("jsonapi")]
public JsonapiObject Jsonapi { get; set; } = null!;

[Required]
[JsonPropertyName("links")]
public LinksInResourceIdentifierDocument Links { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Links;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
Expand All @@ -10,10 +11,13 @@ namespace JsonApiDotNetCore.OpenApi.JsonApiObjects.Documents;
internal sealed class NullableSecondaryResourceResponseDocument<TResource> : NullableSingleData<ResourceObjectInResponse<TResource>>
where TResource : IIdentifiable
{
[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;

[JsonPropertyName("jsonapi")]
public JsonapiObject Jsonapi { get; set; } = null!;

[Required]
[JsonPropertyName("links")]
public LinksInResourceDocument Links { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Links;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
using JsonApiDotNetCore.Resources;

namespace JsonApiDotNetCore.OpenApi.JsonApiObjects.Documents;

// Types in the current namespace are never touched by ASP.NET ModelState validation, therefore using a non-nullable reference type for a property does not
// imply this property is required. Instead, we use [Required] explicitly, because this is how Swashbuckle is instructed to mark properties as required.
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
internal sealed class PrimaryResourceResponseDocument<TResource> : SingleData<ResourceObjectInResponse<TResource>>
where TResource : IIdentifiable
{
[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;

[JsonPropertyName("jsonapi")]
public JsonapiObject Jsonapi { get; set; } = null!;

[Required]
[JsonPropertyName("links")]
public LinksInResourceDocument Links { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Links;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
Expand All @@ -10,10 +11,13 @@ namespace JsonApiDotNetCore.OpenApi.JsonApiObjects.Documents;
internal sealed class ResourceCollectionResponseDocument<TResource> : ManyData<ResourceObjectInResponse<TResource>>
where TResource : IIdentifiable
{
[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;

[JsonPropertyName("jsonapi")]
public JsonapiObject Jsonapi { get; set; } = null!;

[Required]
[JsonPropertyName("links")]
public LinksInResourceCollectionDocument Links { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Links;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
Expand All @@ -10,10 +11,13 @@ namespace JsonApiDotNetCore.OpenApi.JsonApiObjects.Documents;
internal sealed class ResourceIdentifierCollectionResponseDocument<TResource> : ManyData<ResourceIdentifierObject<TResource>>
where TResource : IIdentifiable
{
[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;

[JsonPropertyName("jsonapi")]
public JsonapiObject Jsonapi { get; set; } = null!;

[Required]
[JsonPropertyName("links")]
public LinksInResourceIdentifierCollectionDocument Links { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Links;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
Expand All @@ -10,10 +11,13 @@ namespace JsonApiDotNetCore.OpenApi.JsonApiObjects.Documents;
internal sealed class ResourceIdentifierResponseDocument<TResource> : SingleData<ResourceIdentifierObject<TResource>>
where TResource : IIdentifiable
{
[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;

[JsonPropertyName("jsonapi")]
public JsonapiObject Jsonapi { get; set; } = null!;

[Required]
[JsonPropertyName("links")]
public LinksInResourceIdentifierDocument Links { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Links;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
Expand All @@ -10,10 +11,13 @@ namespace JsonApiDotNetCore.OpenApi.JsonApiObjects.Documents;
internal sealed class SecondaryResourceResponseDocument<TResource> : SingleData<ResourceObjectInResponse<TResource>>
where TResource : IIdentifiable
{
[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;

[JsonPropertyName("jsonapi")]
public JsonapiObject Jsonapi { get; set; } = null!;

[Required]
[JsonPropertyName("links")]
public LinksInResourceDocument Links { get; set; } = null!;
}
5 changes: 5 additions & 0 deletions src/JsonApiDotNetCore.OpenApi/JsonApiObjects/JsonapiObject.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
using System.Text.Json.Serialization;
using JetBrains.Annotations;

namespace JsonApiDotNetCore.OpenApi.JsonApiObjects;

[UsedImplicitly(ImplicitUseTargetFlags.Members)]
internal sealed class JsonapiObject
{
[JsonPropertyName("version")]
public string Version { get; set; } = null!;

[JsonPropertyName("ext")]
public ICollection<string> Ext { get; set; } = null!;

[JsonPropertyName("profile")]
public ICollection<string> Profile { get; set; } = null!;

[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;
}
2 changes: 2 additions & 0 deletions src/JsonApiDotNetCore.OpenApi/JsonApiObjects/ManyData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;

Expand All @@ -9,5 +10,6 @@ internal abstract class ManyData<TData>
where TData : ResourceIdentifierObject
{
[Required]
[JsonPropertyName("data")]
public ICollection<TData> Data { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;

Expand All @@ -9,5 +10,6 @@ internal abstract class NullableSingleData<TData>
where TData : ResourceIdentifierObject
{
[Required]
[JsonPropertyName("data")]
public TData? Data { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Links;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
Expand All @@ -11,7 +12,9 @@ internal sealed class NullableToOneRelationshipInResponse<TResource> : NullableS
where TResource : IIdentifiable
{
[Required]
[JsonPropertyName("links")]
public LinksInRelationshipObject Links { get; set; } = null!;

[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Links;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
Expand All @@ -11,7 +12,9 @@ internal sealed class ToManyRelationshipInResponse<TResource> : ManyData<Resourc
where TResource : IIdentifiable
{
[Required]
[JsonPropertyName("links")]
public LinksInRelationshipObject Links { get; set; } = null!;

[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Links;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
Expand All @@ -11,7 +12,9 @@ internal sealed class ToOneRelationshipInResponse<TResource> : SingleData<Resour
where TResource : IIdentifiable
{
[Required]
[JsonPropertyName("links")]
public LinksInRelationshipObject Links { get; set; } = null!;

[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.Resources;

Expand All @@ -14,8 +15,10 @@ internal sealed class ResourceIdentifierObject<TResource> : ResourceIdentifierOb
internal class ResourceIdentifierObject
{
[Required]
[JsonPropertyName("type")]
public string Type { get; set; } = null!;

[Required]
[JsonPropertyName("id")]
public string Id { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.Resources;

Expand All @@ -7,7 +8,9 @@ namespace JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
internal sealed class ResourceObjectInPatchRequest<TResource> : ResourceIdentifierObject
where TResource : IIdentifiable
{
[JsonPropertyName("attributes")]
public AttributesInPatchRequest<TResource> Attributes { get; set; } = null!;

[JsonPropertyName("relationships")]
public RelationshipsInPatchRequest<TResource> Relationships { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.Resources;

Expand All @@ -7,7 +8,9 @@ namespace JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
internal sealed class ResourceObjectInPostRequest<TResource> : ResourceIdentifierObject
where TResource : IIdentifiable
{
[JsonPropertyName("attributes")]
public AttributesInPostRequest<TResource> Attributes { get; set; } = null!;

[JsonPropertyName("relationships")]
public RelationshipsInPostRequest<TResource> Relationships { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Links;
using JsonApiDotNetCore.Resources;
Expand All @@ -9,12 +10,16 @@ namespace JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;
internal sealed class ResourceObjectInResponse<TResource> : ResourceIdentifierObject
where TResource : IIdentifiable
{
[JsonPropertyName("attributes")]
public AttributesInResponse<TResource> Attributes { get; set; } = null!;

[JsonPropertyName("relationships")]
public RelationshipsInResponse<TResource> Relationships { get; set; } = null!;

[Required]
[JsonPropertyName("links")]
public LinksInResourceObject Links { get; set; } = null!;

[JsonPropertyName("meta")]
public IDictionary<string, object> Meta { get; set; } = null!;
}
2 changes: 2 additions & 0 deletions src/JsonApiDotNetCore.OpenApi/JsonApiObjects/SingleData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects;

Expand All @@ -9,5 +10,6 @@ internal abstract class SingleData<TData>
where TData : ResourceIdentifierObject
{
[Required]
[JsonPropertyName("data")]
public TData Data { get; set; } = null!;
}
Loading