Skip to content

Commit

Permalink
Merge branch 'main' into feature/field-converter-string
Browse files Browse the repository at this point in the history
  • Loading branch information
jbreuer committed Sep 5, 2024
2 parents 33da575 + e0a4b12 commit b8c57e8
Show file tree
Hide file tree
Showing 20 changed files with 672 additions and 35 deletions.
74 changes: 74 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Contribution to the Sitecore ASP.NET Core SDK

Thank you for your interest in contributing to our project. You can contribute with issues and PRs. Simply filing issues for problems you encounter is a great way to contribute. Contributing implementations is greatly appreciated.

## Code of Conduct
Please read the [Code of Conduct](./CODE_OF_CONDUCT.md) before participating, all contributors are expected to uphold this code. Please report unacceptable behavior to [[email protected]](mailto:[email protected]).

## Reporting Issues

We always welcome bug reports, feature requests and overall feedback. Here are a few tips on how you can make reporting your issue as effective as possible.

### Finding Existing Issues

Before filing a new issue, please search our [open issues](https://github.com/Sitecore/ASP.NET-Core-SDK/issues) to check if it already exists.

If you do find an existing issue, please include your own feedback in the discussion. Do consider upvoting (reaction) the original post, as this helps us prioritize popular issues.

### Use the right template

When creating a new issue, please use the appropriate template. We have the following templates available:

| Template | Description |
| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| Bug Report | Use this template to report a bug. |
| Feature Request | Use this template to request a new feature. |
| Question | Use this template to ask a question. For implementation specific questions please use [StackExchange](https://sitecore.stackexchange.com/). |
| Report a security vulnerability | Use this template to report a security vulnerability. |

### Writing a Good Bug Report

Good bug reports make it easier for maintainers to verify and root cause the underlying problem. The better a bug report, the faster the problem will be resolved. Ideally, a bug report should contain the following information:

* A high-level description of the problem.
* A _minimal reproduction_, i.e. the smallest size of code/configuration required to reproduce the wrong behavior.
* A description of the _expected behavior_, contrasted with the _actual behavior_ observed.
* Information on the environment: Sitecore XM version, SDK version, etc.
* Additional information, e.g. is it a regression from previous versions? are there any known workarounds?

#### Why are Minimal Reproductions Important?

A reproduction lets maintainers verify the presence of a bug, and diagnose the issue using a debugger. A _minimal_ reproduction is the smallest possible application demonstrating that bug. Minimal reproductions are generally preferable since they:

1. Focus debugging efforts on a simple code snippet,
2. Ensure that the problem is not caused by unrelated dependencies/configuration,
3. Avoid the need to share production codebases.

#### Are Minimal Reproductions Required?

In certain cases, creating a minimal reproduction might not be practical (e.g. due to nondeterministic factors, external dependencies). In such cases you would be asked to provide as much information as possible, for example by sharing a memory dump of the failing application. If maintainers are unable to root cause the problem, they might still close the issue as not actionable. While not required, minimal reproductions are strongly encouraged and will significantly improve the chances of your issue being prioritized and fixed by the maintainers.

#### How to Create a Minimal Reproduction

The best way to create a minimal reproduction is gradually removing code and dependencies from a reproducing app, until the problem no longer occurs. A good minimal reproduction:

* Excludes all unnecessary types, methods, code blocks, source files, nuget dependencies and project configurations.
* Contains documentation or code comments illustrating expected vs actual behavior.
* If possible, avoids performing any unneeded IO or system calls.

## Contributiting Code
All code contributions must be submitted by the standard GitHub pull request flow, or by logging an issue.

### Accepting Contributions
Sitecore has a _"No commitment"_ approach to this repository. The required functionality of the SDK and related XM Cloud features will be the driver for acceptance decisions. We are open to contributions from the community, but make no commitment to accept or incorporate changes.

### Guidelines
* Create a [fork of the repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo)
* Create a branch for your changes. Use /feat/ for new features, /fix/ for bug fixes and /new/ for breaking changes.
* We observe strict code style rules, please ensure your changes follow the guidance provided by the analyzers.
* [StyleCop](https://github.com/DotNetAnalyzers/StyleCopAnalyzers)
* [File Scoped Namespaces](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/file-scoped-namespaces)
* Do not use `var`
* We require all changes to be covered by tests.
* When you are ready to submit your changes, [create a pull request from your fork](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork) into this repository.
* This repository is configured to use `Squash Merges` for all accepted contributions. This decision has been made to keep the commit history clean and concise.
4 changes: 4 additions & 0 deletions Sitecore.AspNetCore.SDK.sln
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sitecore.AspNetCore.SDK.Tra
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GitHub", "GitHub", "{5FE82369-DEF2-4136-B74F-6E86DB91050E}"
ProjectSection(SolutionItems) = preProject
CODE_OF_CONDUCT.md = CODE_OF_CONDUCT.md
CONTRIBUTING.md = CONTRIBUTING.md
LICENSE.md = LICENSE.md
.github\pull_request_template.md = .github\pull_request_template.md
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{1706E43D-AC19-4FBB-9BFB-18A8B195580A}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static ILayoutRequestHandlerBuilder<GraphQlLayoutServiceHandler> AddGraph
request.Language(defaultLanguage);
}
});
return builder.AddHandler(name, (sp)
return builder.AddHandler(name, sp
=> ActivatorUtilities.CreateInstance<GraphQlLayoutServiceHandler>(
sp, client, sp.GetRequiredService<ISitecoreLayoutSerializer>(), sp.GetRequiredService<ILogger<GraphQlLayoutServiceHandler>>()));
}
Expand Down Expand Up @@ -151,23 +151,6 @@ public static ISitecoreLayoutClientBuilder WithDefaultRequestOptions(this ISitec
return builder;
}

/// <summary>
/// Configures System.Text.Json specific features such as input and output formatters.
/// </summary>
/// <param name="builder">The <see cref="ISitecoreLayoutClientBuilder"/> being configured.</param>
/// <returns>The <see cref="ILayoutRequestHandlerBuilder{THandler}"/> so that additional calls can be chained.</returns>
public static ISitecoreLayoutClientBuilder AddSystemTextJson(this ISitecoreLayoutClientBuilder builder)
{
ServiceDescriptor descriptor = new(typeof(ISitecoreLayoutSerializer), typeof(JsonLayoutServiceSerializer), ServiceLifetime.Singleton);
builder.Services.Replace(descriptor);

builder.Services.AddSingleton<IFieldParser, FieldParser>();
builder.Services.AddSingleton<JsonConverter, FieldConverter>();
builder.Services.AddSingleton<JsonConverter, PlaceholderFeatureConverter>();

return builder;
}

/// <summary>
/// Registers a HTTP request handler for the Sitecore layout service client.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,44 @@ namespace Sitecore.AspNetCore.SDK.LayoutService.Client.Serialization.Converter;
/// <inheritdoc cref="IFieldParser"/>
public class FieldParser : IFieldParser
{
/// <summary>
/// Field key for custom content created by Custom Content Resolvers.
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global - Must be accessible for people using the SDK.
public const string CustomContentFieldKey = "CustomContent";

/// <inheritdoc cref="IFieldParser.ParseFields"/>
public Dictionary<string, IFieldReader> ParseFields(ref Utf8JsonReader reader)
{
if (reader.TokenType != JsonTokenType.StartObject)
Dictionary<string, IFieldReader> result = [];
switch (reader.TokenType)
{
throw new JsonException();
case JsonTokenType.StartObject:
result = ParseStandardFields(ref reader);
break;
default:
result.Add(CustomContentFieldKey, new JsonSerializedField(ParseField(ref reader)));
break;
}

Dictionary<string, IFieldReader> fields = [];
return result;
}

private static Dictionary<string, IFieldReader> ParseStandardFields(ref Utf8JsonReader reader)
{
Dictionary<string, IFieldReader> result = [];
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
string? key = reader.GetString();
reader.Read();
JsonDocument value = ParseField(ref reader);
if (key != null)
{
fields.Add(key, new JsonSerializedField(value));
result.Add(key, new JsonSerializedField(value));
}
}

return fields;
return result;
}

private static JsonDocument ParseField(ref Utf8JsonReader reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public class JsonSerializedField(JsonDocument doc)

private readonly string _json = doc != null ? doc.RootElement.GetRawText() : throw new ArgumentNullException(nameof(doc));

/// <summary>
/// Gets the raw JSON string representation of the field.
/// </summary>
/// <returns>A string containing the raw JSON data.</returns>
public string GetRawValue()
{
return _json;
}

/// <inheritdoc/>
protected override object? HandleRead(Type type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,30 @@ public void Component_RichTextField_CanBeRead(ISitecoreLayoutSerializer serializ
resultField!.Value.Should().Be(expectedField.value.Value);
resultField.EditableMarkup.Should().Be(expectedField.editable.Value);
}

[Theory]
[MemberData(nameof(Serializers))]
public void HeadlessSxa_CanBeRead(ISitecoreLayoutSerializer serializer)
{
// Arrange
string json = File.ReadAllText("./Json/headlessSxa.json");
dynamic jsonModel = JObject.Parse(json);

// Act
SitecoreLayoutResponseContent? result = serializer.Deserialize(json);

// Assert
TextField? resultField = result?.Sitecore?.Route?
.Placeholders["headless-header"].ComponentAt(0)?
.Placeholders["sxa-header"].ComponentAt(0)?
.Fields["Text"]
.Read<TextField>();

dynamic? expectedField = jsonModel.sitecore.route
.placeholders["headless-header"][0]
.placeholders["sxa-header"][0]
.fields.Text;

resultField!.Value.Should().Be(expectedField.value.Value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<Link>Json\edit-in-horizon-mode.json</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="..\data\json\headlessSxa.json">
<Link>Json\headlessSxa.json</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,22 @@ public class FieldParserTests
};

[Fact]
public void ParseFields_IncorrectJsonNotObject_ShouldThrowJsonException()
public void ParseFields_JsonNotObject_ShouldWrapAsCustomContent()
{
// Arrange
void Read()
{
const string json = "[]";
byte[] bytes = [.. Encoding.UTF8.GetBytes(json)];
Utf8JsonReader reader = new(bytes);
reader.Read();
_sut.ParseFields(ref reader);
}
const string json = "[]";
byte[] bytes = [.. Encoding.UTF8.GetBytes(json)];
Utf8JsonReader reader = new(bytes);
reader.Read();

// Act
Action result = Read;
Dictionary<string, IFieldReader> result = _sut.ParseFields(ref reader);

// Assert
result.Should().Throw<JsonException>();
result.Should().ContainSingle();
(string key, IFieldReader value) = result.First();
key.Should().Be(FieldParser.CustomContentFieldKey);
value.Should().BeOfType<JsonSerializedField>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ public void TryRead_InvalidJson_ThrowsException()
field.Should().BeNull();
}

[Fact]
public void GetRawValue_ReturnsExpectedJson()
{
// Arrange
const string json = "{\"value\": 100}";
JsonDocument token = JsonDocument.Parse(json);
JsonSerializedField sut = new(token);

// Act
string result = sut.GetRawValue();

// Assert
result.Should().Be(json);
}

private class ValueField<T> : IField
{
public required T Value { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Sitecore.AspNetCore.SDK.LayoutService.Client.Serialization.Converter;
using Sitecore.AspNetCore.SDK.RenderingEngine.Binding.Attributes;

namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.ComponentModels;

public class CustomResolver
{
[SitecoreComponentField(Name = FieldParser.CustomContentFieldKey)]
public CustomResolverModel[]? CustomContent { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Sitecore.AspNetCore.SDK.LayoutService.Client.Response.Model.Fields;

namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.ComponentModels
{
public class CustomResolverModel
{
public List<string>? Styles { get; set; } = [];

public List<CustomResolverModel>? Children { get; set; } = [];

public string? Href { get; set; }

public string? Querystring { get; set; }

public TextField? NavigationTitle { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Sitecore.AspNetCore.SDK.RenderingEngine.Binding.Attributes;

namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.ComponentModels;

public class PartialDesignDynamicPlaceholder
{
[SitecoreComponentParameter(Name ="sig")]
public string? Sig { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public IActionResult Index(Route route)
{
TestConstants.NestedPlaceholderPageLayoutId => "NestedPlaceholderPageLayout",
TestConstants.VisitorIdentificationPageLayoutId => "VisitorIdentificationLayout",
_ => nameof(Index),
TestConstants.HeadlessSxaLayoutId => "HeadlessSxaLayout",
_ => nameof(Index)
};

return View(view);
Expand Down
Loading

0 comments on commit b8c57e8

Please sign in to comment.