Skip to content

Commit

Permalink
Add support for comment handling in launchSettings.json (#938)
Browse files Browse the repository at this point in the history
* Closes: #772

* Add test to assert LaunchSetttingsSerializerContext does not throw when json has comments in it.

* Update tests/Aspire.Hosting.Tests/LaunchSettingsSerializerContextTests.cs

Co-authored-by: Dan Moseley <[email protected]>

* Add blank line after the namespace

* fix: Remove excessive parens

---------

Co-authored-by: Dan Moseley <[email protected]>
  • Loading branch information
kiapanahi and danmoseley committed Nov 17, 2023
1 parent a7901cd commit 4269be4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Aspire.Hosting/Aspire.Hosting.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetCurrent)</TargetFramework>
Expand Down Expand Up @@ -42,4 +42,8 @@
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Aspire.Hosting.Tests"/>
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/Aspire.Hosting/Utils/LaunchProfileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private static bool TrySelectLaunchProfileFromAnnotation(ProjectResource project
internal delegate bool LaunchProfileSelector(ProjectResource project, out string? launchProfile);

[JsonSerializable(typeof(LaunchSettings))]
[JsonSourceGenerationOptions(ReadCommentHandling = JsonCommentHandling.Skip)]
internal sealed partial class LaunchSetttingsSerializerContext : JsonSerializerContext
{

Expand Down
35 changes: 35 additions & 0 deletions tests/Aspire.Hosting.Tests/LaunchSettingsSerializerContextTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using Xunit;

namespace Aspire.Hosting.Tests;

public class LaunchSettingsSerializerContextTests
{
[Fact]
public void CommentsInLaunchSettingsJsonDoesNotThrow()
{
const string launchSettingsJson = """
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
// comment
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5261",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
""";

// should not throw
JsonSerializer.Deserialize(launchSettingsJson, LaunchSetttingsSerializerContext.Default.LaunchSettings);
}
}

0 comments on commit 4269be4

Please sign in to comment.