Skip to content

Commit

Permalink
Fixed issue: HalDocument looks like a Content document, but isn't.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwadams committed Sep 19, 2019
1 parent 1c2869e commit e8bf6ea
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
17 changes: 1 addition & 16 deletions Solutions/Menes.Abstractions/Menes/Hal/HalDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Menes.Hal
using System.Linq;
using Corvus.ContentHandling;
using Corvus.Extensions.Json;
using Menes.Internal;
using Menes.Links;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -20,11 +21,6 @@ namespace Menes.Hal
/// </remarks>
public class HalDocument : ILinkCollection
{
/// <summary>
/// The standard content type for a HalDocument.
/// </summary>
public const string RegisteredContentType = "application/hal+json";

private Dictionary<string, List<WebLink>> links = new Dictionary<string, List<WebLink>>();
private Dictionary<string, List<HalDocument>> embeddedResources = new Dictionary<string, List<HalDocument>>();

Expand All @@ -37,12 +33,6 @@ public HalDocument(IJsonSerializerSettingsProvider serializerSettingsProvider)
this.SerializerSettings = serializerSettingsProvider.Instance;
}

/// <summary>
/// Gets the content type for the HalDocument.
/// </summary>
/// <remarks>This is set from the target object when you call <see cref="SetProperties{T}(T)"/> and defaults to <c>application/hal+json</c>.</remarks>
public string ContentType { get; private set; } = RegisteredContentType;

/// <summary>
/// Gets the serializer settings for the HAL document.
/// </summary>
Expand Down Expand Up @@ -113,11 +103,6 @@ public void RemoveEmbeddedResource(WebLink resourceLink)
public void SetProperties<T>(T properties)
{
this.Properties = JObject.FromObject(properties, JsonSerializer.Create(this.SerializerSettings));

if (ContentFactory.TryGetContentType(properties, out string contentType))
{
this.ContentType = contentType;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ namespace Menes.Internal
using Menes.Hal;
using Menes.Links;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

/// <summary>
/// A type converter for serializing a <see cref="HalDocument"/> as a standard
/// HAL document.
/// </summary>
public class HalDocumentJsonConverter : JsonConverter
public class HalDocumentJsonConverter : CustomCreationConverter<HalDocument>
{
private readonly IHalDocumentFactory halDocumentFactory;

Expand Down Expand Up @@ -71,6 +72,13 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
result.WriteTo(writer);
}

/// <inheritdoc/>
public override HalDocument Create(Type objectType)
{
// Do nothing
return null;
}

private static void SerializeLinks(HalDocument halDocument, JObject result)
{
var linksObject = new JObject();
Expand Down
4 changes: 2 additions & 2 deletions Solutions/Menes.PetStore/Menes/PetStore/PetStoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public OpenApiResult ListPets(int limit = 10, string continuationToken = null)
string nextContinuationToken = (skip + limit < this.pets.Count) ? BuildContinuationToken(limit + skip) : null;
HalDocument response = this.petListMapper.Map(new PetListResource { Pets = pets, TotalCount = this.pets.Count, PageSize = limit, CurrentContinuationToken = continuationToken, NextContinuationToken = nextContinuationToken });

OpenApiResult result = this.OkResult(response, HalDocument.RegisteredContentType);
OpenApiResult result = this.OkResult(response, "application/hal+hson");

// We also add the next page link to the header, just to demonstrate that it's possible
// to use WebLink items in this way.
Expand Down Expand Up @@ -291,7 +291,7 @@ private OpenApiResult MapAndReturnPet(PetResource result)

HalDocument response = this.petMapper.Map(result);

return this.OkResult(response, HalDocument.RegisteredContentType, auditData: new[] { ("id", (object)result.Id) });
return this.OkResult(response, "application/hal+json", auditData: new[] { ("id", (object)result.Id) });
}
}
}

0 comments on commit e8bf6ea

Please sign in to comment.