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

Use JSON polymorphism with RC2 nightly. #1437

Closed
wants to merge 12 commits into from
16 changes: 8 additions & 8 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
<PackageVersion Include="JustEat.HttpClientInterception" Version="4.3.0" />
<PackageVersion Include="MartinCostello.Logging.XUnit" Version="0.4.0" />
<PackageVersion Include="MartinCostello.Testing.AwsLambdaTestServer" Version="0.8.0" />
<PackageVersion Include="Microsoft.AspNetCore.WebUtilities" Version="9.0.0-rc.1.24452.1" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.0-rc.1.24431.7" />
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.0-rc.1.24431.7" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0-rc.1.24431.7" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0-rc.1.24431.7" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="9.0.0-rc.1.24431.7" />
<PackageVersion Include="Microsoft.AspNetCore.WebUtilities" Version="9.0.0-rc.2.24462.10" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.0-rc.2.24462.5" />
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.0-rc.2.24462.5" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0-rc.2.24462.5" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0-rc.2.24462.5" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="9.0.0-rc.2.24462.5" />
<PackageVersion Include="Microsoft.Extensions.Http.Diagnostics" Version="9.0.0-preview.8.24460.1" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0-preview.8.24460.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.0-rc.1.24431.7" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.0-rc.2.24462.5" />
<PackageVersion Include="Microsoft.Extensions.Telemetry" Version="9.0.0-preview.8.24460.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
Expand All @@ -38,7 +38,7 @@
<PackageVersion Include="ReportGenerator" Version="5.3.9" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="System.Private.Uri" Version="4.3.2" />
<PackageVersion Include="System.Text.Json" Version="9.0.0-rc.1.24431.7" />
<PackageVersion Include="System.Text.Json" Version="9.0.0-rc.2.24462.5" />
<PackageVersion Include="xRetry" Version="1.9.0" />
<PackageVersion Include="xunit" Version="2.9.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
Expand Down
4 changes: 4 additions & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
<configuration>
<packageSources>
<clear />
<add key="dotnet9" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json" />
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<packageSourceMapping>
<packageSource key="dotnet9">
<package pattern="*" />
</packageSource>
<packageSource key="NuGet">
<package pattern="*" />
</packageSource>
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "9.0.100-rc.1.24452.12",
"version": "9.0.100-rc.2.24463.1",
"allowPrerelease": false,
"rollForward": "latestMajor"
},
Expand Down
4 changes: 2 additions & 2 deletions src/LondonTravel.Skill/AlexaSkill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal sealed class AlexaSkill(
/// <returns>
/// The <see cref="ResponseBody"/> to return from the skill.
/// </returns>
public SkillResponse OnError(ISystemExceptionRequest error, Session session)
public SkillResponse OnError(SystemExceptionRequest error, Session session)
{
Log.SystemError(
logger,
Expand Down Expand Up @@ -67,7 +67,7 @@ public SkillResponse OnError(Exception? exception, Session session, string reque
/// A <see cref="Task{TResult}"/> representing the asynchronous operation
/// which returns the <see cref="ResponseBody"/> to return from the skill.
/// </returns>
public async Task<SkillResponse> OnIntentAsync(IIntentRequest intent, Session session)
public async Task<SkillResponse> OnIntentAsync(IntentRequest intent, Session session)
{
var handler = intentFactory.Create(intent.Intent);
return await handler.RespondAsync(intent.Intent, session);
Expand Down
2 changes: 1 addition & 1 deletion src/LondonTravel.Skill/AppJsonSerializerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ namespace MartinCostello.LondonTravel.Skill;
[JsonSerializable(typeof(SkillResponse))]
[JsonSerializable(typeof(SkillUserPreferences))]
[JsonSerializable(typeof(StandardCard))]
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSourceGenerationOptions(AllowOutOfOrderMetadataProperties = true, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
public sealed partial class AppJsonSerializerContext : JsonSerializerContext;
10 changes: 5 additions & 5 deletions src/LondonTravel.Skill/FunctionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ private async Task<SkillResponse> HandleRequestAsync(SkillRequest request)
{
try
{
return request.Request.Type switch
return request.Request switch
{
RequestTypes.Intent => await skill.OnIntentAsync(request.Request, request.Session),
RequestTypes.Launch => skill.OnLaunch(request.Session),
RequestTypes.SessionEnded => skill.OnSessionEnded(request.Session),
RequestTypes.SystemException => skill.OnError(request.Request, request.Session),
IntentRequest intent => await skill.OnIntentAsync(intent, request.Session),
LaunchRequest => skill.OnLaunch(request.Session),
SessionEndedRequest => skill.OnSessionEnded(request.Session),
SystemExceptionRequest exception => skill.OnError(exception, request.Session),
_ => skill.OnError(null, request.Session, request.Request.Type),
};
}
Expand Down
15 changes: 0 additions & 15 deletions src/LondonTravel.Skill/Models/IRequest.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/LondonTravel.Skill/Models/ISessionEndedRequest.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/LondonTravel.Skill/Models/ISystemExceptionRequest.cs

This file was deleted.

18 changes: 18 additions & 0 deletions src/LondonTravel.Skill/Models/IntentRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Text.Json.Serialization;

namespace MartinCostello.LondonTravel.Skill.Models;

public sealed class IntentRequest : Request
{
[JsonIgnore]
public override string Type => "IntentRequest";

[JsonPropertyName("dialogState")]
public string DialogState { get; set; } = default!;

[JsonPropertyName("intent")]
public Intent Intent { get; set; } = default!;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Text.Json.Serialization;

namespace MartinCostello.LondonTravel.Skill.Models;

internal interface IIntentRequest : IRequest
public sealed class LaunchRequest : Request
{
string DialogState { get; }

Intent Intent { get; }
[JsonIgnore]
public override string Type => "LaunchRequest";
}
37 changes: 8 additions & 29 deletions src/LondonTravel.Skill/Models/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

namespace MartinCostello.LondonTravel.Skill.Models;

public sealed class Request : IRequest, IIntentRequest, ISessionEndedRequest, ISystemExceptionRequest
[JsonDerivedType(typeof(IntentRequest), RequestTypes.Intent)]
[JsonDerivedType(typeof(LaunchRequest), RequestTypes.Launch)]
[JsonDerivedType(typeof(SessionEndedRequest), RequestTypes.SessionEnded)]
[JsonDerivedType(typeof(SystemExceptionRequest), RequestTypes.SystemException)]
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
public abstract class Request
{
[JsonPropertyName("type")]
[JsonRequired]
public string Type { get; set; } = default!;
[JsonIgnore]
public abstract string Type { get; }

[JsonPropertyName("requestId")]
public string RequestId { get; set; } = default!;
Expand All @@ -20,29 +24,4 @@ public sealed class Request : IRequest, IIntentRequest, ISessionEndedRequest, IS
[JsonConverter(typeof(MixedDateTimeConverter))]
[JsonPropertyName("timestamp")]
public DateTime Timestamp { get; set; }

//// Properties for "IntentRequest"

[JsonPropertyName("dialogState")]
public string DialogState { get; set; } = default!;

[JsonPropertyName("intent")]
public Intent Intent { get; set; } = default!;

//// Properties for "SessionEndedRequest"

[JsonConverter(typeof(JsonStringEnumConverter<Reason>))]
[JsonPropertyName("reason")]
public Reason Reason { get; set; }

//// Properties for "System.ExceptionEncountered"

[JsonPropertyName("cause")]
public AlexaErrorCause ErrorCause { get; set; } = default!;

//// Properties for "SessionEndedRequest" and "System.ExceptionEncountered"

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("error")]
public AlexaError Error { get; set; } = default!;
}
20 changes: 20 additions & 0 deletions src/LondonTravel.Skill/Models/SessionEndedRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Text.Json.Serialization;

namespace MartinCostello.LondonTravel.Skill.Models;

public sealed class SessionEndedRequest : Request
{
[JsonIgnore]
public override string Type => "SessionEndedRequest";

[JsonConverter(typeof(JsonStringEnumConverter<Reason>))]
[JsonPropertyName("reason")]
public Reason Reason { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("error")]
public AlexaError Error { get; set; } = default!;
}
19 changes: 19 additions & 0 deletions src/LondonTravel.Skill/Models/SystemExceptionRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Text.Json.Serialization;

namespace MartinCostello.LondonTravel.Skill.Models;

public sealed class SystemExceptionRequest : Request
{
[JsonIgnore]
public override string Type => "System.ExceptionEncountered";

[JsonPropertyName("cause")]
public AlexaErrorCause ErrorCause { get; set; } = default!;

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("error")]
public AlexaError Error { get; set; } = default!;
}
20 changes: 10 additions & 10 deletions test/LondonTravel.Skill.NativeAotTests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public async Task Alexa_Function_Can_Process_Intent_Request_For_Line_Status()
public async Task Alexa_Function_Can_Process_Launch_Request()
{
// Arrange
var request = CreateRequest("LaunchRequest");
var request = CreateRequest<LaunchRequest>();

// Act
var actual = await ProcessRequestAsync(request);
Expand All @@ -153,7 +153,7 @@ public async Task Alexa_Function_Can_Process_Launch_Request()
public async Task Alexa_Function_Can_Process_Session_Ended_Request()
{
// Arrange
var session = new Request()
var session = new SessionEndedRequest()
{
Reason = Reason.ExceededMaxReprompts,
Error = new()
Expand All @@ -163,7 +163,7 @@ public async Task Alexa_Function_Can_Process_Session_Ended_Request()
},
};

var request = CreateRequest("SessionEndedRequest", session);
var request = CreateRequest(session);

// Act
var actual = await ProcessRequestAsync(request);
Expand All @@ -184,7 +184,7 @@ public async Task Alexa_Function_Can_Process_Session_Ended_Request()
public async Task Alexa_Function_Can_Process_System_Exception_Request()
{
// Arrange
var exception = new Request()
var exception = new SystemExceptionRequest()
{
Error = new()
{
Expand All @@ -197,7 +197,7 @@ public async Task Alexa_Function_Can_Process_System_Exception_Request()
},
};

var request = CreateRequest("System.ExceptionEncountered", exception);
var request = CreateRequest(exception);

// Act
var actual = await ProcessRequestAsync(request);
Expand All @@ -215,7 +215,7 @@ public async Task Alexa_Function_Can_Process_System_Exception_Request()

private static SkillRequest CreateIntentRequest(string name, params Slot[] slots)
{
var request = new Request()
var request = new IntentRequest()
{
Intent = new Intent()
{
Expand All @@ -233,10 +233,11 @@ private static SkillRequest CreateIntentRequest(string name, params Slot[] slots
}
}

return CreateRequest("IntentRequest", request);
return CreateRequest(request);
}

private static SkillRequest CreateRequest(string type, Request? request = null)
private static SkillRequest CreateRequest<T>(T? request = null)
where T : Request, new()
{
var application = new Application()
{
Expand Down Expand Up @@ -265,7 +266,7 @@ private static SkillRequest CreateRequest(string type, Request? request = null)
User = user,
},
},
Request = request ?? new(),
Request = request ?? new T(),
Session = new()
{
Application = application,
Expand All @@ -276,7 +277,6 @@ private static SkillRequest CreateRequest(string type, Request? request = null)
Version = "1.0",
};

result.Request.Type = type;
result.Request.Locale = "en-GB";

return result;
Expand Down
4 changes: 2 additions & 2 deletions test/LondonTravel.Skill.Tests/AlexaFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public async Task Cannot_Invoke_Function_With_System_Failure()
var function = await CreateFunctionAsync();
var context = new TestLambdaContext();

var error = new Request()
var error = new SystemExceptionRequest()
{
Error = new()
{
Expand All @@ -71,7 +71,7 @@ public async Task Cannot_Invoke_Function_With_System_Failure()
},
};

var request = CreateRequest("System.ExceptionEncountered", error);
var request = CreateRequest(error);

// Act
var actual = await function.HandlerAsync(request, context);
Expand Down
Loading