Skip to content

leverage Verify.SystemJson to support verify for JsonNode #9663

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

SimonCropp
Copy link
Contributor

@SimonCropp SimonCropp commented Jun 3, 2025

Description

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?

@Copilot Copilot AI review requested due to automatic review settings June 3, 2025 11:47
@github-actions github-actions bot added the area-integrations Issues pertaining to Aspire Integrations packages label Jun 3, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jun 3, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces support for verifying JsonNode instances by leveraging the new Verify.SystemJson package and updating test verification calls. Key changes include:

  • Adding VerifierSettings.UseStrictJson() and VerifierSettings.InitializePlugins() during test initialization.
  • Updating project files to include the Verify.SystemJson package.
  • Replacing calls to Verify(manifest.ToString(), "json") with Verify(manifest) in tests.

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 6 comments.

File Description
tests/Shared/TestModuleInitializer.cs Added configuration calls for strict JSON and plugin initialization.
tests/Directory.Packages.props Added Verify.SystemJson package reference.
tests/Aspire.Hosting.Kubernetes.Tests/*.csproj Added package references for Verify.SystemJson.
tests/Aspire.Hosting.Azure.Tests/*.cs Updated verification calls to pass JsonNode directly.

@SimonCropp
Copy link
Contributor Author

can someone unlock the Verify.SystemJson nuget usage

@eerhardt
Copy link
Member

eerhardt commented Jun 4, 2025

can someone unlock the Verify.SystemJson nuget usage

I've kicked off the package mirror. When it is done I will re-kick this PR.

@eerhardt eerhardt closed this Jun 4, 2025
@eerhardt eerhardt reopened this Jun 4, 2025
@eerhardt
Copy link
Member

eerhardt commented Jun 4, 2025

Looks like this is causing \" to be replaced with \u0022

Received: ExistingAzureResourceTests.SupportsExistingAzureSqlServerWithResourceGroup.received.json
{
  "type": "azure.bicep.v1",
  "connectionString": "Server=tcp:{sqlServer.outputs.sqlServerFqdn},1433;Encrypt=True;Authentication=\"Active Directory Default\"",
  "path": "sqlServer.module.bicep",
  "params": {
    "existingResourceName": "{existingResourceName.value}"
  },
  "scope": {
    "resourceGroup": "{existingResourceGroupName.value}"
  }
}
Verified: ExistingAzureResourceTests.SupportsExistingAzureSqlServerWithResourceGroup.verified.json
{
  "type": "azure.bicep.v1",
  "connectionString": "Server=tcp:{sqlServer.outputs.sqlServerFqdn},1433;Encrypt=True;Authentication=\u0022Active Directory Default\u0022",
  "path": "sqlServer.module.bicep",
  "params": {
    "existingResourceName": "{existingResourceName.value}"
  },
  "scope": {
    "resourceGroup": "{existingResourceGroupName.value}"
  }
}
``

@SimonCropp
Copy link
Contributor Author

@eerhardt

it is the other way around causing \u0022 to be replaced with \"

would you like to keep \u0022? if so i can look at how to enable that.

if u r ok with \" i will accept the changes and update the PR

@SimonCropp SimonCropp marked this pull request as draft June 4, 2025 23:05
@SimonCropp
Copy link
Contributor Author

actually. i will put this in draft for now. let me have think on it

@SimonCropp
Copy link
Contributor Author

@eerhardt i look into this

the building of that portion of the string is done by AzureSqlServerResource

return result ??
                ReferenceExpression.Create($"Server=tcp:{FullyQualifiedDomainName},1433;Encrypt=True;Authentication=\"Active Directory Default\"");

the reason this \u0022 ends up in the json is because of the default serialization behaviour

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/character-encoding#serialize-language-character-sets

So in ManifestPublishingContext.WriteConnectionString

public void WriteConnectionString(IResource resource)
{
    if (resource is IResourceWithConnectionString resourceWithConnectionString &&
        resourceWithConnectionString.ConnectionStringExpression is { } connectionString)
    {
        Writer.WriteString("connectionString", connectionString.ValueExpression);
    }
}

the writer has no encoder config so we get \u0022

Test showing the behavior

[Test]
public void EncodingTest()
{
    var value = new
    {
        value = "\""
    };
    json = JsonSerializer.Serialize(value);
    ClassicAssert.AreEqual(
        """
        {
          "value": "\u0022"
        }
        """,
        json);
}

to get a non-unicode escaped quote we would need to use an Encoder

[Test]
public void EncodingWithRelaxedTest()
{
    var options = new JsonSerializerOptions
    {
        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
        WriteIndented = true
    };
    var value = new
    {
        value = "\""
    };
    json = JsonSerializer.Serialize(value, options);
    ClassicAssert.AreEqual(
        """
        {
          "value": "\""
        }
        """,
        json);
}

The reason verified file change from \u0022 to \" is that Verify.SystemJson round trips the json to support Scrubbering. And when those encoded values are read using System.Text.Json then it gives back the un-encoded quote. When verify writes it, it uses the raw value it received. ie it escapes but doesnt encode

so IMO having the verified file change from \u0022 to \" is actually more correct.

I am not sure how u want to proceed here. I would prefer manifests are written to disk with the more readable \", but i dont know larger implications of that

The value of this is, aside from less code, that Verify.SystemJson supports the larger feature set of Verify. I am not sure of the encoding qwerk is a problem (that dilutes that value) or not?

thoughts?

@eerhardt
Copy link
Member

I'm fine with the \u0022 getting changed to \". I just wanted to make sure it was a conscious change and not a mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-integrations Issues pertaining to Aspire Integrations packages community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants